in reply to Default Hash Key
{ package Hash::WithDefault; use Tie::Hash qw( ); BEGIN { our @ISA = 'Tie::ExtraHash'; } use constant IDX_HASH => 0; use constant IDX_DEFAULT => 1; use constant NEXT_IDX => 2; sub FETCH { my ($self, $key) = @_; return ( exists( $self->[IDX_HASH]{$key} ) ? $self->[IDX_HASH]{$key} : $self->[IDX_DEFAULT] ); } } { tie my %hash, 'Hash::WithDefault', 'not in the alphabet'; %hash = ( a => 'a vowel', b => 'a consonant', ); print("$_: $hash{$_}\n") for qw( a b ~ ); }
Output:
a: a vowel b: a consonant ~: not in the alphabet
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Default Hash Key
by alexm (Chaplain) on May 02, 2008 at 10:21 UTC | |
by ikegami (Patriarch) on May 02, 2008 at 21:48 UTC | |
by alexm (Chaplain) on May 03, 2008 at 09:35 UTC | |
by Anonymous Monk on May 04, 2008 at 16:37 UTC | |
by alexm (Chaplain) on May 06, 2008 at 00:20 UTC | |
by Anonymous Monk on May 04, 2008 at 16:52 UTC | |
by alexm (Chaplain) on May 06, 2008 at 00:33 UTC | |
|
Re^2: Default Hash Key
by jethro (Monsignor) on May 02, 2008 at 14:04 UTC | |
by ikegami (Patriarch) on May 02, 2008 at 21:52 UTC |