Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^4: search a large text file

by perl_lover_always (Acolyte)
on Feb 08, 2011 at 14:41 UTC ( [id://886976]=note: print w/replies, xml ) Need Help??


in reply to Re^3: search a large text file
in thread search a large text file

my hash file will run out of memory! that was the main problem that I could not generate the hash at the first stage! any sample code as a clue?

Replies are listed 'Best First'.
Re^5: search a large text file
by jethro (Monsignor) on Feb 08, 2011 at 17:34 UTC

    If you use a disk based hash, for example a DBM::Deep hash, you won't run out of memory. The main reason to have a disk-based hash is that you can create hashes that are bigger than your memory (another reason to use it is that the hash is permanent).

    For sample code just look at the perldoc page of DBM::Deep, under 'Synopsis'. Basically you just call DBM::Deep to link a hashname with a file on disk and after that you use that hash like any other hash, only that everything you store in there is (behind the scenes) transfered directly to disk.

      I tried to use it! why when I use in this way, the results are not correct.
      sub to_hash { my $file = shift; my $db = DBM::Deep->new( "$file.db" ); open(FILE, "<$file"); foreach $l (<FILE>) { my ($ngram,$line) = split /\t/, $l; push(@{ $db->{$ngram} }, $line); } close FILE; return $db; }
      for example when I search for a key, I'll get the correct value few times instead of for example one or two times!

        Do you mean "many times instead of one or two times" ? (one or two) == (few) in the english language? I don't see the rest of your script, so I can only guess:

        1) Did you call the sub "to_hash" more than once ? "to_hash" should be executed once and then never again. And with "once" I mean once in your lifetime and not once per execution of the script. Whenever you want to search, just use "my $db = DBM::Deep->new( "$file.db" );" and start to search. Remember that the file $file.db is permanent on your disk and keeps the info between invocations of your script. Call "to_hash" twice and you also get twice the values.

        Additionally you might want to add "$db->clear()" to your "to_hash" subroutine so that even if you have to call it twice (because the source file changed), you get an empty hash before filling it.

        2) Maybe your search routine prints out more than you want

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found