in reply to How best to tell when my hash is "full" (all values defined)?
Perhaps overkill for this application, but useful where the gathering of a predetermined set of fields is spread over a more complex gathering process.
Or more simply, 0 == grep !defined, values %hash;
use Internals qw[ SetReadOnly ]; my %hash; undef @hash{ qw[ the quick brown fox ] }; SetReadOnly( \%hash ); ... while( <$fh> ) { if( ... some capturing regex ... ) { $hash{ $1 } = $2; } elsif( ... regex ... ) { $hash{ $1 } = $2; } else { $hash{ fox } == FOX_DEFAULT; } last if keys %hash == grep defined, values( %hash ); ## Updated, s +ee below. }
In the event that one of your regex accidentally matches the wrong key/value pairing, setting (say) $1 = 'bill', then you'll get a fatal error:
Attempt to access disallowed key 'bill' in a restricted hash at yoursc +ript.pl line nn, <$fh> line mm
Telling you not only which line of code the error occured, but also which line of the data file (and the value) that was involved.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How best to tell when my hash is "full" (all values defined)?
by Hofmator (Curate) on Dec 18, 2006 at 15:31 UTC | |
by BrowserUk (Patriarch) on Dec 18, 2006 at 16:25 UTC | |
by ikegami (Patriarch) on Dec 18, 2006 at 15:50 UTC |