in reply to Re: Assign (key, value) to a hash w/o clobbering hash
in thread Assign (key, value) to a hash w/o clobbering hash
depends how far you want to go. Full example as follows...%hash = split /X/, 'fooXbar' # added the missing comma ;)
(BTW I did try to do this with Tie::StdHash, but couldn't figure it out)
#!/usr/bin/perl use strict; use Data::Dumper; tie my %hash, 'ExpandingHash'; $hash{'still'} = 'here'; %hash = split /X/, 'fooXbar'; print Dumper(\%hash); # I also added a new clear method, which you could use like this. # tied(%hash)->FORCE_CLEAR; __OUTPUT__ $VAR1 = { 'foo' => 'bar', 'still' => 'here' }; #================================================================== package ExpandingHash; use strict; sub TIEHASH { my %self; bless \%self, shift } sub FETCH { $_[0]->{$_[1]} } sub STORE { $_[0]->{$_[1]} = $_[2] } sub FIRSTKEY { each %{$_[0]} } sub NEXTKEY { each %{$_[0]} } sub CLEAR { } # NOT IMPLEMENTED sub DELETE { delete $_[0]->{$_[1]} } sub EXISTS { exists $_[0]->{$_[1]} } sub FORCE_CLEAR { %{$_[0]} = () }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Assign (key, value) to a hash w/o clobbering hash
by davorg (Chancellor) on Sep 28, 2006 at 11:21 UTC | |
by reasonablekeith (Deacon) on Sep 28, 2006 at 11:47 UTC |