generic increment of number/string

A

alexxx.magni

hi everybody,
there is some way to easily loop a padded variable, getting:
0001
0002
0003
0004
....

?

I'd like to do it in a simple for() loop, and I wanted to avoid
checking the total string length...

thanks!


Alessandro Magni
 
P

Paul Lalli

hi everybody,
there is some way to easily loop a padded variable, getting:
0001
0002
0003
0004
...

?

I'd like to do it in a simple for() loop, and I wanted to avoid
checking the total string length...

You've made it more difficult than it is, probably by thinking too much
like a C programmer. :)

for my $num ('0001' .. '0100') {
print "$num\n";
}

Paul Lalli
 
A

alexxx.magni

thanks!

What I had problem with, was that at first I checked on the command
line,
and there I discovered what still puzzles me:

1) output of your program in a script:
0001
0002
0003
....

2) while instead, perl -e 'for my $num ('0001' .. '0100') {print
"$num\n";}' gives:
1
2
3
....


I really wonder why............


Alessandro

Paul Lalli ha scritto:
 
P

Paul Lalli

What I had problem with, was that at first I checked on the command line,
and there I discovered what still puzzles me:

1) output of your program in a script:
0001
0002
0003
...

2) while instead, perl -e 'for my $num ('0001' .. '0100') {print
"$num\n";}' gives:
1
2
3
...

Did you actually look at the *entire* list, not just the first three?
It only goes up to 64, not 100. That's because you're using single
quotes within the program, and as delimeters to the perl -e.
Therefore, Perl is not seeing single quoted strings. Instead, Perl is
seeing the shell's interpretation of 0001 and 0100, which are octal 1
and octal 64, respectively.

Change the inner single quotes to double quotes, and everything will
work out...

Paul Lalli
 
B

Ben Morrow

Quoth "Paul Lalli said:
Did you actually look at the *entire* list, not just the first three?
It only goes up to 64, not 100. That's because you're using single
quotes within the program, and as delimeters to the perl -e.
Therefore, Perl is not seeing single quoted strings. Instead, Perl is
seeing the shell's interpretation of 0001 and 0100, which are octal 1
and octal 64, respectively.

Minor nit: this has nothing to do with the shell. 0100 is not special to
the shell. The important thing is that perl sees

for my $num (0001 .. 0100) {

instead of

for my $num ('0001' .. '0100') {

, and *perl* interprets 0100 as '100 octal' = 64.
Change the inner single quotes to double quotes, and everything will
work out...

Or escape the quotes, or use q//. That's why it's there, after all.

Ben
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top