> -----Original Message-----
> From: [email protected] [mailto:[email protected]]
> Sent: Tuesday, August 23, 2011 5:57 AM
> To: [email protected]
> Subject: Hash of Hashes - Error
>
> Hi All,
>
> I am working on the below code to traverse through a hash, but it
> throws an error which states "Can't coerce array into hash at temp.pl
> line 6."
>
> Code:
> ===============================================================
> sub hash_walk {
Why do you have a shift here? $self usually indicates you are calling an
object method which you are not doing here. Get rid of this shift.
Looks like you were cutting and pasting some object-oriented code.
> my $self = shift;
>
> my ($hash, $key_list, $callback) = @_;
>
> while (my ($k, $v) = each (%$hash)) {
> push @$key_list, $k;
>
> if (ref($v) eq 'HASH') {
Why do you use $self? Again hash_walk is not an object method. Just call
hash_walk.
> $self->hash_walk($v, $key_list, $callback);
>
> }
> else {
> $callback->($k, \$v, $key_list);
>
> } pop @$key_list;
>
> $hash->{$k} = $v;
> }
> }
> my %data = (
> a => {
> ab => 1,
> ac => 2,
> ad => {
> ada => 3,
> adb => 4,
> adc => {
> adca => 5,
> adcb => 6,
> },
> },
> },
> b => 7,
> c => {
> ca => 8,
> cb => {
> cba => 9,
> cbb => 10,
> },
> },
> );
> hash_walk(\%data, [], \&replace_all_val_strings);
> sub replace_all_val_strings {
> my ($k, $v, $key_list) = @_;
> printf "k = %-8s v = %-4s key_list = [%s]\n", $k, $$v,
> "@$key_list";
> $$v =~ s/oldstr/newstr/;
> printf "k = %-8s v = %-4s key_list = [%s]\n", $k, $$v,
> "@$key_list";
> }
> ===============================================================
> Could anyone please help me out.
>
> Thanks in Advance
> Anand
HTH, Ken
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/