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; } }
In reply to Re: Help needed in reading a very large file line by line
by tobyink
in thread Help needed in reading a very large file line by line
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |