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 | |
by tkil (Monk) on May 28, 2004 at 16:33 UTC |