Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Print contents of text file line by line

by usemodperl (Beadle)
on Jun 23, 2018 at 19:05 UTC ( [id://1217299]=note: print w/replies, xml ) Need Help??


in reply to Print contents of text file line by line

ALWAYS START WITH THESE TWO LINES, FOR HELPFUL ERROR MESSAGES:
use strict; use warnings;
Edit: Wrongish answer deleted...
Edit: This was my mistake, oops:
my @line = <$fh>
STOP REINVENTING WHEELS, START BUILDING SPACE ROCKETS!CPAN 🐪

Replies are listed 'Best First'.
Re^2: Print contents of text file line by line
by stevieb (Canon) on Jun 23, 2018 at 19:15 UTC

    That will slurp the entire file into memory all at once, which is almost always what isn't desired.

    Use the scalar, but do so in proper context. The OP is assuming that the return from each file handle read is an array, which it isn't. In a while() loop like the OP has, the file handle is read a line at a time, so the scalar holds everything up to end-of-line (including the newline character(s)). To use an array as you suggested would mean that you'd have to eliminate said while() loop.

    use warnings; use strict; open my $fh, '<', "file.txt" or die "can't open the flippin' flabergastin' file!: $!"; while (my $line = <$fh>){ chomp $line; print "something here $line\n"; }

    Note that if the while() block is very small, a well-known Perl idiom is to use the built-in special default variable:

    use warnings; use strict; open my $fh, '<', "file.txt" or die "can't open the flippin' flabergastin' file!: $!"; while (<$fh>){ chomp; print "something here $_\n"; }
Re^2: Print contents of text file line by line
by AnomalousMonk (Archbishop) on Jun 24, 2018 at 15:01 UTC
    Edit: Wrongish answer deleted...

    Still wrongish, unfortunately, because it makes stevieb's previously pertinent reply seem incoherent. Please see How do I change/delete my post?. The Golden Rule: Don't destroy context.


    Give a man a fish:  <%-{-{-{-<

      Still wrongish, unfortunately, because it makes stevieb's previously pertinent reply seem incoherent.

      I see what you mean. Sorry about that, it was not my intent, stevieb was given credit after all. I put a reference to my mistake back in the node...

      STOP REINVENTING WHEELS, START BUILDING SPACE ROCKETS!CPAN 🐪
        I put a reference to my mistake back in the node...

        Thank you very much. Some monks seem to make no effort at all to fix missteps and mistakes, leaving it to the janitors to clean up really egregious messes.


        Give a man a fish:  <%-{-{-{-<

Re^2: Print contents of text file line by line
by TonyNY (Beadle) on Jun 24, 2018 at 21:48 UTC
    Thanks modperl...I see my mistake in trying to print the elements of an array without first declaring the array...
      Unless you really want to print only selected lines of the file, you probably should use the line-by-line approach that you currently (unintentionally) wrote, and add a counter of the number of times this loop has executed, and end the loop after you have printed enough. This will work for any file regardless of size.

        So I corrected my code and added this to create an array: my @line = <$fh>;

        how can I add an if statement to check if the 7th element of the array does exist?

        I've tried something like this but it did not work:

        if ($line[6]){ print "Support Group: $line[6]"; }else print "Support Group:UNKNOWN $line[6]";

        Thanks,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1217299]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-19 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found