Here's what I would do:
Things to note:Code:my($i) = 0; my($file) = "readme.txt"; open(INFO, $file) || die "Error opening $file."; while (<INFO>) { printf("%.6d: %s\n", $i, $_); $i++; } close(INFO);
1.) Always use "my" to define the scope of your variables. You should be using "use strict;" anyway.
2.) Always check that your file open has worked.
3.) Using a while eliminate the need to use an array and is generally a better way of reading the file. @lines = <INFO>; is not a good way of doing this.
4.) printf will let you print place markers so that the columns for the source print out all line up verticaly. Looks nicer.
5.) You can put anything you want inside a foreach so you could have done:
But why use extra variables when you don't need to.Code:foreach (@lines) { print "$i $_"; $i++; }
Hope this helps and may the source be with you.




.
Reply With Quote