Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

for loop syntax ...

by cilid (Initiate)
on Mar 21, 2007 at 10:11 UTC ( #605814=perlquestion: print w/replies, xml ) Need Help??
cilid has asked for the wisdom of the Perl Monks concerning the following question:

Can somebody explain, why this code is working:
ls -o --full-time|perl -pe "s/\s{2,}/ /g; for (my $i = 1; $i <= 9; $i++) {s/\s/;/;}"
and this code is not working:
ls -o --full-time|perl -pe "s/\s{2,}/ /g; for (1..9) {s/\s/;/;}"

what it does: replace first 9 spaces with ";"
spec: perl, v5.8.8, OS WinXP SP2
execute in cmd prompt.

Thanks in advance ...


Replies are listed 'Best First'.
Re: for loop syntax ...
by davorg (Chancellor) on Mar 21, 2007 at 10:25 UTC

    There is a difference between your two loops.

    for (my $i = 1; $i <= 9; $i++) { s/\s/;/; }

    In this loop, the variable $i goes from 1 to 9 and each time round the loop the variable $_ has a substitution applied to it.

    for (1..9) { s/\s/;/; }

    In this loop, you don't have $i so $_ is used as the iterator variable. So when you apply the substitution to $_ you are applying it to the numbers 1 to 9.

    I think that you were looking for this:

    for my $i (1..9) { s/\s/;/; }

    which does the same thing as your first loop.

    I have a nagging feeling that this may be an XY Question and that perhaps you should explain what you're really trying to do.


    See the Copyright notice on my home node.

    "The first rule of Perl club is you do not talk about Perl club." -- Chip Salzenberg

Re: for loop syntax ...
by GrandFather (Sage) on Mar 21, 2007 at 10:28 UTC

    In the first case the loop iterates 9 times and the substitution operates on $_, which is not the loop variable because $i is supplied.

    In the second case the loop iterates 9 times and the substitutions operates on $_ which is the loop variable and takes the values 1 - 9 on successive passes through the loop (no loop variable supplied).

    The behaviour of the second form can be made the same as the first by providing a loop variable:

    for my $i (1..9) {s/\s/;/;}

    DWIM is Perl's answer to Gödel
Re: for loop syntax ...
by cilid (Initiate) on Mar 21, 2007 at 12:41 UTC
    Thank you, for providing me with the solution so quickly. To explain my self a little bit more… Some time ago I made a small Perl program for labeling my CD/DVDs and it was working well. I didn’t use the program for a few months, until couple days ago. But then program stopped working. I noticed that the problem was in the for loop syntax. The original line of code “for (1 .. 9) {s/\s/;/;}” didn’t work anymore. I replaced the syntax with “for (my $i = 1; $i <= 9; $i++) {s/\s/;/;}” and program started working again. To find that I lost half a day, because like I said, program worked before and I’m certain that I didn’t change anything in code. Except maybe I installed new version of Perl (Activestate) and some new Perl modules.

    So, thank you all for explaining me what’s the catch in “for (1 .. 9) …” syntax.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://605814]
Approved by marto
[NodeReaper]: Nice try kid

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2018-02-23 17:05 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (303 votes). Check out past polls.