in reply to Help needed in reading a very large file line by line
The example you linked to deals with files with fixed-length records - i.e. generally binary files. As you're talking about "line by line", I assume you're talking about a text-based file - e.g. plain text, HTML, CSV, etc.
# Set the character which will be used to indicate the end of a line. # This defaults to the system's end of line character, but it doesn't # hurt to set it explicitly, just in case some other part of your code # has altered it from the default. local $/ = "\n"; # Open the file for read access: open my $filehandle, '<', 'myfile.txt'; my $line_number = 0; # Loop through each line: while (defined($line = <$filehandle>)) { # The text of the line, including the linebreak # is now in the variable $line. # Keep track of line numbers $line_number++; # Strip the linebreak character at the end. chomp $line; # Do something with the line. do_something($line); # Perhaps bail out of the loop if ($line =~ m/^ERROR/) { warn "Error on line $line_number - skipping rest of file"; last; } }
But we can make the above more concise, because Perl usefully defines a variable called $_ which is used as a default variable in many cases; and a variable called $. which keeps track of the current line number.
# Set the character which will be used to indicate the end of a line. local $/ = "\n"; # Open the file for read access: open my $filehandle, '<', 'myfile.txt'; # Loop through each line: while (<$filehandle>) { # The text of the line, including the linebreak # is now in the variable $_. # Strip the linebreak character at the end. chomp; # Do something with the line. do_something($_); # Perhaps bail out of the loop if (m/^ERROR/) { warn "Error on line $. - skipping rest of file"; last; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Help needed in reading a very large file line by line
by AnomalousMonk (Archbishop) on Feb 28, 2012 at 22:07 UTC | |
|
Re^2: Help needed in reading a very large file line by line
by Anonymous Monk on Feb 28, 2012 at 10:59 UTC | |
by jethro (Monsignor) on Feb 28, 2012 at 11:43 UTC | |
by tobyink (Canon) on Feb 28, 2012 at 13:26 UTC | |
by Anonymous Monk on Feb 28, 2012 at 11:10 UTC | |
by MegART (Novice) on Dec 01, 2014 at 17:22 UTC | |
by choroba (Cardinal) on Dec 01, 2014 at 17:29 UTC |