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).


In reply to Re: millions of records in a Hash by Speedy
in thread millions of records in a Hash by johnkj

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.