Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: millions of records in a Hash

by Speedy (Monk)
on Feb 24, 2002 at 17:25 UTC ( [id://147183]=note: print w/replies, xml ) Need Help??


in reply to millions of records in a Hash

If I understand what you are attempting, you want to check the values of the entire hash in the event that you start to enter a new key $a and discover that a key $a already exists in %Hash. Right now you are trying to store the values for each hash key in an array, then run through this long array.

Would it work to in advance create an "inverted" hash, stored under another variable name, like %Hash_values, and store the results in an appropriate file. You don't seem to care what key goes with the values, but rather want a quick way to see if the value exists. You could for example use Recipe 5.8 in the Perl Cookbook to initially create the %Hash_values, which has as its KEYS the Values of %Hash, then when doing the checks tie BOTH %Hash and %Hash_values.

The code could look like:

tie %Hash, 'DB_File', $path_to_hash, O_RDWR|O_CREAT, 0666 or die "Can' +t tie $path_to_hash: $!"; tie %Hash_values, 'DB_File', $path_to_hash_values, O_RDWR|O_CREAT, 066 +6 or die "Can't tie $path_to_hash_values: $!"; while (($key, $value) = each (%Hash)) { $Hash_values{$value} = $key; # Or any value for $key } untie %Hash; untie %Hash_values;

Then rather than doing a long foreach loop through an array, you would simply ask "if (exists $Hash_values{$b}) { -- do stuff -- }

If you find $b does not exist in the list of values (which now are the KEYS to %Hash_values), you can simply add it to %Hash_values to keep the running list of values (perhaps like $Hash_values{$b} = 1; -- since you don't care what the "value" of the value is, but just need a quick way to look up the values).

Log In?
Username:
Password:

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

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

    No recent polls found