Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re: Different behaviors between "while" and "map"by friedo (Prior) |
on Dec 06, 2008 at 00:08 UTC ( [id://728437]=note: print w/replies, xml ) | Need Help?? |
The reason this happens is because the while loop is designed to operate on <> like an iterator. Thus, it reads one line at a time until it can't read anymore. By contrast, using <> as the list for your map block reads the entire file in all at once, incrementing $. along the way. The entire file then gets passed to your map block as one big list, and by this time, $. has already been incremented to the number of the last line, so the same $. gets printed each time through the map block.
In Section
Seekers of Perl Wisdom
|
|