Hash of Hashes - Error
am 23.08.2011 11:23:04 von anand.jawaji
--_000_EED4F8BFC625F04894989BD7A6904D2E3499F2E53CMX03Acorpem cc_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
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:
==================== =====3D=
==================== =====3D=
=============3D
sub hash_walk {
my $self =3D shift;
my ($hash, $key_list, $callback) =3D @_;
while (my ($k, $v) =3D each (%$hash)) {
push @$key_list, $k;
if (ref($v) eq 'HASH') {
$self->hash_walk($v, $key_list, $callback);
}
else {
$callback->($k, \$v, $key_list);
} pop @$key_list;
$hash->{$k} =3D $v;
}
}
my %data =3D (
a =3D> {
ab =3D> 1,
ac =3D> 2,
ad =3D> {
ada =3D> 3,
adb =3D> 4,
adc =3D> {
adca =3D> 5,
adcb =3D> 6,
},
},
},
b =3D> 7,
c =3D> {
ca =3D> 8,
cb =3D> {
cba =3D> 9,
cbb =3D> 10,
},
},
);
hash_walk(\%data, [], \&replace_all_val_strings);
sub replace_all_val_strings {
my ($k, $v, $key_list) =3D @_;
printf "k =3D %-8s v =3D %-4s key_list =3D [%s]\n", $k, $$v, "@$key_=
list";
$$v =3D~ s/oldstr/newstr/;
printf "k =3D %-8s v =3D %-4s key_list =3D [%s]\n", $k, $$v, "@$key_=
list";
}
==================== =====3D=
==================== =====3D=
=============3D
Could anyone please help me out.
Thanks in Advance
Anand
--_000_EED4F8BFC625F04894989BD7A6904D2E3499F2E53CMX03Acorpem cc_--
Re: Hash of Hashes - Error
am 24.08.2011 13:27:52 von AKINLEYE
--90e6ba2121a7589e4904ab3e9a22
Content-Type: text/plain; charset=ISO-8859-1
When you shift off a variable you pop it out of the argument . Just try and
get the first variable of the argument another way by copying to an array
first or something .
from perldoc
Shift :
Shifts the first value of the array off and returns it, shortening the array
by 1 and moving everything down. If there are no elements in the array,
returns the undefined value. If ARRAY is omitted, shifts the @_ array within
the lexical scope of subroutines and formats,
On Tue, Aug 23, 2011 at 10:23 AM, wrote:
> 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 {
> my $self = shift;
>
> my ($hash, $key_list, $callback) = @_;
>
> while (my ($k, $v) = each (%$hash)) {
> push @$key_list, $k;
>
> if (ref($v) eq 'HASH') {
> $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
>
--
Akinleye Adedamola
--90e6ba2121a7589e4904ab3e9a22--
Re: Hash of Hashes - Error
am 25.08.2011 19:40:54 von Mike McClain
> On Tue, Aug 23, 2011 at 10:23 AM, wrote:
> 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 {
> my $self = shift;
> my ($hash, $key_list, $callback) = @_;
> while (my ($k, $v) = each (%$hash)) {
> push @$key_list, $k;
> if (ref($v) eq 'HASH') {
> $self->hash_walk($v, $key_list, $callback);
> }
> else {
> $callback->($k, \$v, $key_list);
> } pop @$key_list;
> $hash->{$k} = $v;
> }
> }
> 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";
> }
I couldn't see any point in '@key_list' and am less comfortable with OO
than I would like to be but unless I missed something I think this does
what you're asking after you add '$self' back in.
my %foods = ( fruit => { qw / apple 1 banana 2 pear 3 / },
nuts => { pecan => 10, walnut => 20 },
);
sub hash_walk
{ my ($hash, $callback) = @_;
while (my ($k, $v) = each (%$hash))
{ if (ref($v) eq 'HASH')
{ hash_walk($v, $callback);
}
else
{ $callback->($k, \$v);
}
$hash->{$k} = $v;
}
}
sub replace_all_val_strings
{ my ($k, $v) = @_;
my $new = ++$$v;
$$v =~ s/$$v/$new/;
}
use Data::Dumper;
print Dumper \%foods;
hash_walk(\%foods, \&replace_all_val_strings);
print Dumper \%foods;
HTH,
Mike
--
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/