Your skill will accomplish what the force of many cannot |
|
PerlMonks |
Re: iterative foreach loopby zarath (Beadle) |
on Nov 15, 2017 at 11:09 UTC ( [id://1203459]=note: print w/replies, xml ) | Need Help?? |
Hello! For the task you are describing I would use the following code:
Note: when I need to open files for any reason, I tend to use autodie; so I can forget to add or die $!; to each open. This code literally just opens your .txt-file and prints the following: "Line " number of the line ": " content of the line. It does not do much more. But you did mention you would like to store the lines into an array. For later use perhaps? In this case it would become this code:
The result of both codes is the same, but in the second version all lines (with linecount) are stored inside the array @lines so you can easily use it for whatever you want after the while-block is closed without needing to read from $file again. This would be more difficult in the first version I think. You can of course format the lines of your array however you like it by editing the push: push (@array,'fill','this','however','you','want'); Hope this is helpful.
In Section
Seekers of Perl Wisdom
|
|