Hash of Hashes - Error

Hash of Hashes - Error

am 23.08.2011 11:57:02 von anand.jawaji

--_000_EED4F8BFC625F04894989BD7A6904D2E3499F2E54FMX03Acorpem 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_EED4F8BFC625F04894989BD7A6904D2E3499F2E54FMX03Acorpem cc_--

RE: Hash of Hashes - Error

am 23.08.2011 13:20:20 von Ken Slater

> -----Original Message-----
> From: anand.jawaji@emc.com [mailto:anand.jawaji@emc.com]
> Sent: Tuesday, August 23, 2011 5:57 AM
> To: beginners@perl.org
> 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: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Hash of Hashes - Error

am 23.08.2011 13:39:09 von Shlomi Fish

Hi Anand,

On Tue, 23 Aug 2011 05:57:02 -0400
wrote:

> Hi All,
>=20
> 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."
>=20

First of all, let me note that the indentation on that code is inconsistent=
and
erratic. But I was able to fix it by importing it into Vim and using the
"=3D" key (=3D "format") and some manual tweaks. Here is the correct versio=
n:

[CODE]
#!/usr/bin/perl

use strict;
use warnings;

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_l=
ist";
$$v =3D~ s/oldstr/newstr/;
printf "k =3D %-8s v =3D %-4s key_list =3D [%s]\n", $k, $$v, "@$key_l=
ist";
}
[/CODE]

Now after I did that, I used the perl debugger ("perl -d") to debug the code
(see http://perl-begin.org/topics/debugging/ ), and realised that the probl=
em
was that hash_walk expects 4 arguments (including "$self" which is the obje=
ct
handle), while it is only given 3 in the initial call, which fails.=20

Do you need the "$self" or is there a good reason for that? If you do, read=
the
Object Oriented programming resources at
http://perl-begin.org/topics/object-oriented/ , because otherwise it's just
cargo-cult programming.

Also see:

http://perl-begin.org/tutorials/bad-elements/

Regards,

Shlomi Fish

--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
First stop for Perl beginners - http://perl-begin.org/

Trying to block Internet pornography is like climbing a waterfall and trying
to stay dry. (â€=94 one of Shlomi Fishâ€=99s Internet Friends)

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

RE: Hash of Hashes - Error

am 23.08.2011 13:52:00 von anand.jawaji

VGhhbmtzIGEgbG90IGZvciB0aGUgc3VnZ2VzdGlvbnMgYW5kIHNvbHV0aW9u Lg0KDQpSZWdhcmRz
LA0KQW5hbmQNCg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ID0gc2hpZnQ7DQoN
CiAgICBteSAoJGhhc2gsICRrZXlfbGlzdCwgJGNhbGxiYWNrKSA9IEBfOw0K DQogICAgd2hpbGUg
KG15ICgkaywgJHYpID0gZWFjaCAoJSRoYXNoKSkgew0KICAgICAgICBwdXNo IEAka2V5X2xpc3Qs
ICRrOw0KDQogICAgICAgIGlmIChyZWYoJHYpIGVxICdIQVNIJykgew0KICAg ICAgICAgICAgJHNl
bGYtPmhhc2hfd2FsaygkdiwgJGtleV9saXN0LCAkY2FsbGJhY2spOw0KICAg ICAgICB9DQogICAg
ICAgIGVsc2Ugew0KICAgICAgICAgICAgJGNhbGxiYWNrLT4oJGssIFwkdiwg JGtleV9saXN0KTsN
CiAgICAgICAgfQ0KDQogICAgICAgIHBvcCBAJGtleV9saXN0Ow0KDQogICAg ICAgICRoYXNoLT57
JGt9ID0gJHY7DQogICAgfQ0KfQ0KDQpteSAlZGF0YSA9ICgNCiAgICBhID0+ IHsNCiAgICAgICAg
YWIgPT4gMSwNCiAgICAgICAgYWMgPT4gMiwNCiAgICAgICAgYWQgPT4gew0K ICAgICAgICAgICAg
YWRhID0+IDMsDQogICAgICAgICAgICBhZGIgPT4gNCwNCiAgICAgICAgICAg IGFkYyA9PiB7DQog
ICAgICAgICAgICAgICAgYWRjYSA9PiA1LA0KICAgICAgICAgICAgICAgIGFk Y2IgPT4gNiwNCiAg
ICAgICAgICAgIH0sDQogICAgICAgIH0sDQogICAgfSwNCiAgICBiID0+IDcs DQogICAgYyA9PiB7
DQogICAgICAgIGNhID0+IDgsDQogICAgICAgIGNiID0+IHsNCiAgICAgICAg ICAgIGNiYSA9PiA5
LA0KICAgICAgICAgICAgY2JiID0+IDEwLA0KICAgICAgICB9LA0KICAgIH0s DQopOw0KDQpoYXNo
X3dhbGsoXCVkYXRhLCBbXSwgXCZyZXBsYWNlX2FsbF92YWxfc3RyaW5ncyk7 DQoNCnN1YiByZXBs
YWNlX2FsbF92YWxfc3RyaW5ncyB7DQogICAgbXkg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