in reply to iterative foreach loop
One possible solution relatively close to yours:
But if all you want to do is to print the lines, then you don't need to store the file in an array: you can read the lines one by one and print them.#!\usr\bin perl use warnings; use strict; use diagnostics; use feature 'say'; open my $FILE, "<", "Text2.txt" or die; # Three-argument syntax +for open my @lines = <$FILE>; # actually reading the file into the a +rray foreach my $line (@lines) { # iterating directly over the @lines a +rray items say $line; # note that your lines already have an + end of line, you would probably prefer to use print rather than say } close $FILE;
The while loop could be boiled down to one line with a statement modifier:#!\usr\bin perl use warnings; use strict; use diagnostics; open my $FILE, "<", "Text2.txt" or die; while (my $line = <$FILE>) { print $line; } close $FILE;
In fact it can be even simpler, using the fact that print provides a list context to the <...> operator:open my $FILE, "<", "Text2.txt" or die; print while <$FILE>;
open my $FILE, "<", "Text2.txt" or die; print <$FILE>;
|
|---|