Passes a Hash by reference (function b)
Adds some key/value pairs to the hash
Prints the hash using a recursive function (so the hash can be in any form, i think)
Passes (in effect) the hashes within the main hash to the recursive function.
It's taken me a while to work out references in perl. I hope it is useful to someone.
#!/opt/bin/perl5 my %c; b(\%c); #print as normal if(0) { for $i (keys %c) { print "$i = $c{$i}\n"; for $a (keys %{$c{$i}}) { print "$i = $c{$i} = $c{$i}{$a}\n"; } } } sub b { my($c)=@_; $$c{b} = 'b'; $$c{c} = 'c'; $$c{b}{a} = 'ba'; $$c{b}{v} = 'bv'; $$c{b}{v}{g} = 'bvg'; print "printing c{b}{v} = $$c{b}{v}\n"; recref($c,""); } #pass this function a reference to a hash sub recref { my($cref,$str)=@_; my $i; my $prefix; for $i (keys %{$cref}) { $prefix = $str . "." . $i; print "$prefix = $$cref{$i}\n"; recref($cref->{$i},$prefix); } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing Hashes by Reference
by belg4mit (Prior) on Jan 10, 2002 at 00:06 UTC | |
by jamesdmorris (Sexton) on Jan 10, 2002 at 16:10 UTC |