in reply to Immense hashes

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' }

Replies are listed 'Best First'.
Re: Re: Immense hashes
by CountZero (Bishop) on May 28, 2004 at 16:21 UTC
    It is entirely possible that the regex might not match, in which case $1 will be undef.

    Or worse, the $1 variable will have the value of the previous time it matched and it will overwrite the existing entry in the hash. In this case it wil overwrite it with the same value it already had as all values of the hash are set to "blah" (which is utterly strange).

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Or worse, the $1 variable will have the value of the previous time it matched and it will overwrite the existing entry in the hash.

      Hm. For some reason, I was under the impression that $1 and friends would be undef after any unsuccessful match. Oh, wow, it doesn't; gross:

      $ perl -lwe '$_="foo"; /(\w+)/ && print "match: $1"; /(\W+)/ || print "no match: $1";' match: foo no match: foo
      In this case it wil overwrite it with the same value it already had as all values of the hash are set to "blah" (which is utterly strange).

      Well, I just assumed that the original poster was using that "blah" as a placeholder for something else, for the purposes of posting. The presence of quite a few other syntactic errors makes it clear that this was not the code that actually ran...