------------------------------------------------
On Fri, 26 Sep 2003 20:13:21 +0530, Ramprasad A Padmanabhan <[EMAIL PROTECTED]> wrote:
> Juris wrote:
> > I have one small problem!
> > HowTo read from large text file text in binary mode?
> >
> > if i want read all file, i use this code:
> >
> > my (@LOG_FILE);
> > open (FL, "/var/log/maillog");
> > @LOG_FILE=<FL>;
> > close (FL);
> > #After this code execution file contents stored in array @LOG_FILE
> > @LOG_FILE=grep /$something/, @LOG_FILE
> >
> > I not want read all file string by struing - it's so slowly, if file is
> > large and contains more than 1000000 records!
> > I need read each 500000 bytes, but how?
> > Please, help!
> >
> >
>
>
> When you are opening big files never do
> @array = <FILE>
> This essentially reads the entire file into an array and is very
> expensive on memory.
>
> you could do something like
> while(<FILE>){
> push @arr , $_ if(/$something/);
> }
>
> But IMHO this still that may not be the best way.
>
> What I would do is
>
>
> system("grep $something $filename > $tempfile");
> # *Nothing* beats gnu grep when you parse large file
>
> open(FILE,$tempfile);
> # Now if you really want the lines in an array
> @lines = <FILE>
This is NOT production sufficient code, either please handle it fully or disclaim it
by stating so. You don't use a full path (or suggest to), you don't check the return
value of system, there are all kinds of issues with respect to using '>' to write to a
file (permissions, disk space, etc.) Security issues, naturally checking 'open' for
success, removing core files if they are generated, "cleaning" the variables for taint
safety, etc, etc, etc. Shelling out in this manner is quick and dirty (and has its
places I admit) but on a beginners list please disclaim it as so.
The OP may want to check out the read function, use it carefully....
perldoc -f read
http://danconia.org
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]