inserting dot

C

cooldaddy

How to insert a dot character in a string at each 10th postition ? For
example: I wanna place a dot at the 10th, 20th and 30th position in the
string named $global
 
C

cooldaddy

well, I havent got a clue which command to use for this. But if you can
give me the name of the command, i can google for the answer.
 
J

Josef Moellers

cooldaddy said:
How to insert a dot character in a string at each 10th postition ? For
example: I wanna place a dot at the 10th, 20th and 30th position in the
string named $global

As in "please write a program for me that does what I need"?

What have you tried so far and where did it not produce the desired result?
 
A

Anno Siegel

cooldaddy said:
How to insert a dot character in a string at each 10th postition ? For
example: I wanna place a dot at the 10th, 20th and 30th position in the
string named $global

There must be at least a half-dozen ways. What have you tried and how
did it fail?

Anno
 
C

cooldaddy

well, I couldnt try anything yet, cause I don't know what commands i
could use. I've got a little perl book here at home, but nothing in
there on how to insert something at a given position. Also if i google
something like:
perl insert, it doesnt show me what i need.. All i need to know from
you guys is what command i should look at..
 
A

Anno Siegel

cooldaddy said:
well, I havent got a clue which command to use for this. But if you can
give me the name of the command, i can google for the answer.

There isn't a single "command" to use. Solutions could involve join,
unpack, substr, regular expressions and substitution operations, and
more.

Fishing for hints and googling for answers is no way to solve a programming
problem. Learn the language you're using.

Anno
 
B

Babacio

cooldaddy said:
well, I couldnt try anything yet, cause I don't know what commands i
could use. I've got a little perl book here at home, but nothing in
there on how to insert something at a given position. Also if i google
something like:
perl insert, it doesnt show me what i need.. All i need to know from
you guys is what command i should look at..

I think you have the perldoc in your perl installation. Try to have a
look at the perlfunc section, it may help. The first thing would be to
look for 'string' in its text. You would find easily something like:

Functions for SCALARs or strings
"chomp", "chop", "chr", "crypt", "hex", "index", "lc", "lcfirst",
"length", "oct", "ord", "pack", "q/STRING/", "qq/STRING/",
"reverse", "rindex", "sprintf", "substr", "tr///", "uc", "ucfirst",
"y///"
 
J

Jürgen Exner

cooldaddy said:
How to insert a dot character in a string at each 10th postition ? For
example: I wanna place a dot at the 10th, 20th and 30th position in
the string named $global

See "perldoc -q comma":

How can I output my numbers with commas added?

jue
 
J

Jürgen Exner

cooldaddy said:
well, I havent got a clue which command to use for this. But if you
can give me the name of the command, i can google for the answer.

Why on earth would you do that instead of using the readily provided Perl
manual?

jue
 
J

Josef Moellers

cooldaddy said:
well, I havent got a clue which command to use for this. But if you can
give me the name of the command, i can google for the answer.

If _I_ cannot find a ready-made command, I'll write one myself, using
whatever skills I already have. Often, I later find a much better
solution and, if necessary, I can adapt my program to use the better ideas.
Even with a limited knowledge of Perl, you should be able to come up
with a program that attempts to solve your problem using whatever
elements of Perl you do know. If you stumble across a specific problem,
consult the documentation or come back to ask. The answer you'll get,
however, may be a concise

"perldoc -f substr"

or the like.
 
C

cooldaddy

thanks for your replies, I looked into the substr thingy, and made this
little script.. but i'm getting this strange error message: useless use
of addition (+) in void context at test.pl line 7, and then it hangs...
how come ?

#!/usr/bin/perl -w
use CGI;
print "Content-type: text/html\n\n";
$tekst="abcdefghijklmnopqrstuvwxyz";

for ($count=1; $count<=length($tekst); $count+3){
substr($tekst,$count,0)=".";
}
print $tekst;
 
P

Paul Lalli

cooldaddy said:
thanks for your replies, I looked into the substr thingy, and made this
little script.. but i'm getting this strange error message: useless use
of addition (+) in void context at test.pl line 7, and then it hangs...
how come ?

Because you're uselessly using addition in a void context on or about
line 7.
#!/usr/bin/perl -w

You're missing "use strict;". Have you read the posting guidelines for
this group?
use CGI;
print "Content-type: text/html\n\n";

These lines are irrelevant to any test of inserting a period into a
string. Why did you include them?
$tekst="abcdefghijklmnopqrstuvwxyz";

for ($count=1; $count<=length($tekst); $count+3){
substr($tekst,$count,0)=".";
}
print $tekst;

What exactly do you think the statement "$count+3" is doing? Were you
expecting that to in some way update $count?

Paul Lalli
 
J

Josef Moellers

cooldaddy said:
thanks for your replies, I looked into the substr thingy, and made this
little script.. but i'm getting this strange error message: useless use
of addition (+) in void context at test.pl line 7, and then it hangs...
how come ?

#!/usr/bin/perl -w
use CGI;

use warnings;
use strict;
print "Content-type: text/html\n\n";
$tekst="abcdefghijklmnopqrstuvwxyz";

Dutchman? B-{)
for ($count=1; $count<=length($tekst); $count+3){

This "$count+3" is just a sum, but there should be a side-effect in that
last part. It hangs, because $count will eternally be "1".
Better use "$count += 3".
substr($tekst,$count,0)=".";
}
print $tekst;

You're almost there!

Josef
 
R

Robert Chug

How to insert a dot character in a string at each 10th postition ? For
example: I wanna place a dot at the 10th, 20th and 30th position in the
string named $global

use warnings;
use strict;
my $global="abcdefghijklmnopqrstuvwxyz";
my @globe=split//,$global;
my $final;
my $len=scalar @globe;
my $r;
print "$global\n";
foreach my $i (@globe)
{
if ((($r % 10)==0) & ($r>0))
{
$final=$final.".".$i;
$r++
}
else
{
$final="$final"."$i";
$r++;
}
}
print "\n$final";

this is a very crude example , but you are still adviced to follow what
other have mentioned,
 
P

Paul Lalli

Robert said:
use warnings;
use strict;
my $global="abcdefghijklmnopqrstuvwxyz";
my @globe=split//,$global;
my $final;
my $len=scalar @globe;
my $r;
print "$global\n";
foreach my $i (@globe)
{
if ((($r % 10)==0) & ($r>0))
{
$final=$final.".".$i;
$r++
}
else
{
$final="$final"."$i";

Why did you correctly not use useless quotes in the above block, but
use them here?
This could better be written any of three ways:
$final = $final . $i;
$final = "$final$i";
$final .= $i;
$r++;
}
}
print "\n$final";

Gah. What's that quote in either the Camel or Llama? Something about
a Stravarious violin pounding in nails? This might be a decent
algorithm if Perl was limited to, say, C's function set. It's not.

Please look at the functions that directly manipulate Perl strings.
Specifically substr()
perldoc perlfunc
perldoc -f substr

Paul Lalli
 
D

Dr.Ruud

cooldaddy schreef:
thanks for your replies, I looked into the substr thingy, and made
this little script.. but i'm getting this strange error message:
useless use of addition (+) in void context at test.pl line 7, and
then it hangs... how come ?

#!/usr/bin/perl -w
use CGI;
print "Content-type: text/html\n\n";
$tekst="abcdefghijklmnopqrstuvwxyz";

for ($count=1; $count<=length($tekst); $count+3){
substr($tekst,$count,0)=".";
}
print $tekst;

Not a bad attempt.

1. You obviously didn't read (or remember) the posting guidelines:
always code 'use strict;'.

2. Some lines in your code are not needed for your test and some are
missing, as Paul Lalli already said.

3. You are inserting at every 2nd position (so not at every 10th as you
first said) but start too soon, maybe because you don't know that the
index starts at 0, see also your '<='.

4. You didn't indent nor space your code properly.

Some suggestions:

#!/usr/bin/perl

use strict;
use warnings;

my $tekst = "abcdefghijklmnopqrstuvwxyz";
my $step = 5;

for (my $count = $step; $count < length($tekst); ($count += $step)++)
{
substr($tekst, $count, 0) = ".";
}

print $step, ':', $tekst, "\n";


Variant (that changes $step):

for (my $count = $step++; $count < length($tekst); $count += $step) {
 
D

Dave Weaver

use warnings;
use strict;
my $global="abcdefghijklmnopqrstuvwxyz";
my @globe=split//,$global;

Having had to maintain other people's code, I'm a big fan of sensible
names for variables. A "globe" is a sphere - what does this have to do
with the contents of this variable?
@chars or @letters would be a bit more sensible.
But then you only use this array once, so it's not really needed (see
below)
my $final;
my $len=scalar @globe;

Note that the "scalar" is redundant here - the right hand side of the
assignment is already in scalar context. $len is never used anyway,
so really the whole statement is redundant.
my $r;
print "$global\n";
foreach my $i (@globe)
{
if ((($r % 10)==0) & ($r>0)) ^
oops!
{
$final=$final.".".$i;
$r++
}
else
{
$final="$final"."$i";
$r++;
}
}

More neatly written as:

my $count = 0;
my $final = '';
for my $char ( split //, $global ) {
if ( $count and $count % 10 == 0 ) {
$final .= '.';
}
$final .= $char;
++$count;
}

(untested)
but see below for a better way of doing it.
print "\n$final";

this is a very crude example , but you are still adviced to follow what
other have mentioned,

I know you said it's crude, but there's not much point in enabling
warnings if you're going to ignore them;

Possible precedence problem on bitwise & operator at - line 12.
Use of uninitialized value in modulus (%) at - line 11.
Use of uninitialized value in numeric gt (>) at - line 11.
Use of uninitialized value in string at - line 18.

Warnings show that things aren't working as you intend, even if the
end result is right.

Why split the string into individual characters? Perl is much better
operating on full strings than individual characters.

For example:

#!/usr/bin/perl
use warnings;
use strict;

my $string = join '', 'A' .. 'Z', 'a' .. 'z';

for ( my $pos = 9; $pos < length $string; $pos += 10 ) {
$string = substr( $string, 0, $pos )
. "."
. substr( $string, $pos );
}


NB: the "spec" is ambiguous; is a "." wanted after every 10 characters
of the input, or at every 10th location in the output? My code does
the latter.
 
M

Matija Papec

X-Ftn-To: cooldaddy

cooldaddy said:
well, I couldnt try anything yet, cause I don't know what commands i
could use. I've got a little perl book here at home, but nothing in
there on how to insert something at a given position. Also if i google
something like:
perl insert, it doesnt show me what i need.. All i need to know from
you guys is what command i should look at..

Read "perldoc perlretut" and try to understand how s/// works,

my $text = join '', 10..50;
$text =~ s/ (.{10}) /$1 /xgs;


there is also very good cookbook if you want some perl recipes,
http://www.oreilly.com/catalog/perlckbk2/toc.html
 
R

Robert Chug

Having had to maintain other people's code, I'm a big fan of sensible
names for variables. A "globe" is a sphere - what does this have to do
with the contents of this variable?
@chars or @letters would be a bit more sensible.
But then you only use this array once, so it's not really needed (see
below)


Note that the "scalar" is redundant here - the right hand side of the
assignment is already in scalar context. $len is never used anyway,
so really the whole statement is redundant.


More neatly written as:

my $count = 0;
my $final = '';
for my $char ( split //, $global ) {
if ( $count and $count % 10 == 0 ) {
$final .= '.';
}
$final .= $char;
++$count;
}

(untested)
but see below for a better way of doing it.


I know you said it's crude, but there's not much point in enabling
warnings if you're going to ignore them;

Possible precedence problem on bitwise & operator at - line 12.
Use of uninitialized value in modulus (%) at - line 11.
Use of uninitialized value in numeric gt (>) at - line 11.
Use of uninitialized value in string at - line 18.

Warnings show that things aren't working as you intend, even if the
end result is right.

Why split the string into individual characters? Perl is much better
operating on full strings than individual characters.

For example:

#!/usr/bin/perl
use warnings;
use strict;

my $string = join '', 'A' .. 'Z', 'a' .. 'z';

for ( my $pos = 9; $pos < length $string; $pos += 10 ) {
$string = substr( $string, 0, $pos )
. "."
. substr( $string, $pos );
}


NB: the "spec" is ambiguous; is a "." wanted after every 10 characters
of the input, or at every 10th location in the output? My code does
the latter.

thanks for your comments,
BTW I did not got any warnings like you mentioned, what could I have been
missing , I use perIDE for writing and compiling and version is v5.8.3
 
D

Dave Weaver

Robert Chug said:
thanks for your comments,
BTW I did not got any warnings like you mentioned, what could I have been
missing , I use perIDE for writing and compiling and version is v5.8.3

Unfortunately I am unfamiliar with perlIDE; I suggest you check
whatever configuration options it has to see if anything is preventing
you seeing warnings.

I saw the warnings using perl v5.8.0 on Linux, using the good old
command-line.

Try compiling your program from the command line and see what you get:

perl -c yourprog.pl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top