laziness, impatience, and hubris | |
PerlMonks |
Re: processing a file once within a nest for loopby ferrency (Deacon) |
on Feb 15, 2002 at 14:55 UTC ( [id://145685]=note: print w/replies, xml ) | Need Help?? |
The reason why your code works as it does, is because when you use
for or map, the loop variable is essentially a pointer to the
value, not a copy of the value. So, if you change the value of
the loop value, it changes the corresponding array element. This
is the same behavior whether your loop variable is $_
or a lexical created with my. The easiest way to patch this to make it work is to copy the loop variable into another temporary variable before doing the substitution:
Some people don't like temporary variables, especially in cases like this, where it looks like you could remove the variable with no ill effect to the code (but you can't). But, since this is perl, there are other ways to do it. One would be to match $j and then do the replacement in the print statement instead of using s///:
I hope these help. Alan
In Section
Seekers of Perl Wisdom
|
|