I agree with most of the other posters so far: this is very incomplete (and thus very confusing) code.

I think you want to read a flat file in, use the first word on each line as a key, then assign something as the value in a hash. If so, maybe you're looking for something like this?

my %hash; while ( <FILE> ) { my ( $key ) = ( m/^(\w+)/ ) or next; $hash{$key} = 'whatever'; }

If you are doing something with the rest of the line, maybe you want to do a limited split on whitespace instead ... ah, no, you want a literal vertical bar:

my %hash; while ( <FILE> ) { my ( $key, $value ) = split /\|/, $_, 2; next unless $key; $hash{$key} = $value; }

If neither of the above are what you're trying to do, consider restating your original question. (For the record, I consider hashes with hundreds of thousands of elements to be "immense" on current machines — a few hundred should be trivial.)

Finally, never ever ever use $1 and friends without first checking for the success of a match. In your code, you do:

/^(\w+)\|/; $hash{$1)='blah';

It is entirely possible that the regex might not match, in which case $1 will be undef. Make the assignment conditional:

if ( /^(\w+)\|/ ) { $hash{$1} = 'blah' }

In reply to Re: Immense hashes by tkil
in thread Immense hashes by michthedrizzard

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.