Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Re: Re: split and sysread()

by Limbic~Region (Chancellor)
on Apr 19, 2003 at 00:19 UTC ( [id://251589]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: split and sysread()
in thread split and sysread()

The issue with the above is it still reads in the file one line at a time, right?.

Wrong! Perl reads from the file in a buffer and only goes back out to disk when the buffer is empty. What <FILEHANDLE> does is read from the buffer up to the newline. I think you are falling prey to premature optimization.

while (<INPUT>) { my @fields = split /\|/ , $_; #Do stuff with particular field }

Cheers - L~R

Update: See this node by chromatic as he was setting me straight on the very same matter

Replies are listed 'Best First'.
Re: Re: Re: Re: split and sysread()
by relaxed137 (Acolyte) on Apr 19, 2003 at 00:32 UTC
    Hmmm... That's what I had before I started to use sysread... I've noticed that sysread will read a file much faster though... Maybe I can make the read buffer bigger instead to tune "while <input>" ?
    open (INFILE, "/sentry/ecisock/ecisock.rpt"); while (<INFILE>) { chomp; ($serverhostname, $execdate, $cicstranid, $sentryapi, $elapsedtime, $errorcode, $corphdr, $channelcode, $clientprodcode, $clientipaddr, $sentrytermid, $signonid, $accountnum, $branch, $campaign, $segment, $custid, $localsysid, $remotesysid, $sentrytarget, $hfb, $sentryver, $corphdrfailreason, $corphdrfailtype, $corphdrbussvc, $corphdrclientcode, $mfcicstasknum, $sentrycicstasknum, $foo1, $foo2, $cpsm)=split(/\|/,$_); ########################################### # Counts total calls ########################################### $totalsentrycalls++; (.. Etcetera ..)
      Just a note: rather than split into a zillion variables, IMO it's more maintainable to list your field names in an array, then split into a hash variable:
      my @fields = qw( serverhostname execdate etc. ); while (<INFILE>) { chomp; my %stuff; @stuff{@fields} = split /\|/; # Easy iteration over fields (e.g. for debugging) print "[$_][$stuff{$_}]\n" for @fields; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-28 23:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found