Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Loading file into memory

by monarch (Priest)
on Aug 05, 2008 at 18:32 UTC ( #702441=note: print w/replies, xml ) Need Help??


in reply to Loading file into memory

Another method: store a hash with the key being the name and the value being the position in the file of the desired record.

This will generally be slower when accessing records because the file itself must be interrogated, but it could potentially be faster as it minimises the risk of the file turning into swap memory (which is expensive to access at any rate).

Your specification was rather unclear, so for the purposes of example code I will assume that you want a record given an $id (e.g. "daamaya") and a $name (e.g. "Daniel R. Amaya"). So I will construct a hash of ids that point to a hash of names that point to a position in the file containing the desired record.

E.g.

my %ids = (); my $curpos = 0; # position within the file while ( defined( my $line = <HANDLE> ) ) { # we want my ( $part_left, $part_right ) = split( /\s*,\s*/, $line ); my ( $id, $name ) = split( /:/, $part_left ); $ids{$id}->{$name} = $curpos; $curpos += length( $line ); } # later when we want to look up a record sub get_record( $ $ ) { my ( $id, $name ) = @_; # arguments my $filepos = $ids{$id}->{$name}; seek( HANDLE, $filepos, 0 ); my $line = <HANDLE>; my ( $part_left, $part_right ) = split( /\s*,\s*/, $line ); return( $part_right ); } # e.g. get_record( "daamaya", "Daniel R. Amaya" ); # process $line

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://702441]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2017-06-27 06:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (600 votes). Check out past polls.