[prev in list] [next in list] [prev in thread] [next in thread]
List: cgi-list
Subject: Re: [CGI] Parsing Large Files
From: Michael Dabrowski <michael () dabrowski ! org>
Date: 1998-11-24 17:47:37
[Download RAW message or body]
At 07:01 PM 11/23/98 -0800, you wrote:
>I have the same problem and WOULD LOVE to know the answer!
Here's a simple script to solve the problem:
#!/usr/bin/perl
=head1
Program reads a specified $file, a character at a time using sysread()
with allows for sequential reading, without neding to have the whole
file stored in memory. Function fd_read() accepts as its arguments a
pointer to a file handle, and to an output variable which will store the
result. fd_read() cuts off reading after sysread finds a '\n' or a '\r'
character. This would account for the behavior of the normal functioning
of the <> operator but without the memory overhead. fd_read() returns
the line read by reference.
=cut1
$file = "/home/user/file";
open(IN, "$file");
while(&fd_read(\*IN, \$buf)) {
print "$buf\n";
}
sub fd_read {
local($fd) = shift; ## Accept File Handle
local($line) = shift; ## Accept output line
local($c); ## character place holder
local($loop) = 0; ## looping flag
$$line = ""; ## Reset line to null
while(!($loop)) {
sysread($fd, $c, 1); ## Read a character from
filehandle
if($c eq "") ## If none was read, error.
{ return 0; } ## Return nothing if error or
eof
elsif(($c eq "\n") || ($c eq "\r"))
{ $loop = 1; } ## If character a \n|\r, end
loop
else { $$line .= $c; } ## Append character to line
}
return 1;
}
------
Hope it helps.
- Michael Dabrowski
-----------------------------------------------------------------
To unsubscribe, send mail to "majordomo@jann.com" with "unsubscribe cgi-list"
in the body.
The CGI Tips & Tricks website (and archive of the list) is located
at http://www.jann.com/Perl/
cgi-list is hosted by Summit Communication Design (http://www.summitdesign.net).
Summit Communication Design is a full-service web hosting firm
providing web design, cgi programming and inter/intra-net support.
-----------------------------------------------------------------
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic