generic increment of number/string

Discussion in 'Perl Misc' started by alexxx.magni@gmail.com, Nov 21, 2006.

  1. Guest

    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
    , Nov 21, 2006
    #1
    1. Advertising

  2. Paul Lalli Guest

    wrote:
    > 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
    Paul Lalli, Nov 21, 2006
    #2
    1. Advertising

  3. Guest

    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:

    > wrote:
    > > 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
    , Nov 22, 2006
    #3
  4. Paul Lalli Guest

    wrote:
    > 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
    Paul Lalli, Nov 22, 2006
    #4
  5. Guest

    that was tricky!

    thanks...............


    Alessandro

    Paul Lalli ha scritto:

    > wrote:
    > > 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
    , Nov 22, 2006
    #5
  6. Ben Morrow Guest

    Quoth "Paul Lalli" <>:
    > wrote:
    > > 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.


    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

    --
    Musica Dei donum optimi, trahit homines, trahit deos. |
    Musica truces mollit animos, tristesque mentes erigit.|
    Musica vel ipsas arbores et horridas movet feras. |
    Ben Morrow, Nov 22, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. john brown
    Replies:
    6
    Views:
    562
    Jim Gibson
    Oct 22, 2003
  2. Replies:
    104
    Views:
    10,905
    Jordan Abel
    Oct 28, 2005
  3. Replies:
    99
    Views:
    2,469
    eliza81
    Jun 11, 2010
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    875
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    500
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page