Hi hchana,

One possible solution relatively close to yours:

#!\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;
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; open my $FILE, "<", "Text2.txt" or die; while (my $line = <$FILE>) { print $line; } close $FILE;
The while loop could be boiled down to one line with a statement modifier:
open my $FILE, "<", "Text2.txt" or die; print while <$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 <$FILE>;

In reply to Re: iterative foreach loop by Laurent_R
in thread iterative foreach loop by hchana

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.