hash arrays

Folks,

I am reusing a code for some enhancements. According to my
understanding, it is a record with
some unique string as key and then hash array as the value.

I iterate through the array and print as follows:

foreach my $key (keys %{$abc})
{
print "$key ${$abc}{$key} \n";
}

I get values such like:
val_0001 HASH(0x186c0060)
val_0002 HASH(0x187ea490)
val_0003 HASH(0x18655bc0)
val_0004 HASH(0x1880fc60)

Can someone tell me how to get the actual value instead of HASH* as above?

Regards,
Sharan

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Sharan Basappa [ Mo, 26 Juli 2010 14:09 ] [ ID #2045137 ]

Re: hash arrays

--001485f5cd7ce5ae59048c498ca5
Content-Type: text/plain; charset=UTF-8

On Mon, Jul 26, 2010 at 2:09 PM, Sharan Basappa <sharan.basappa [at] gmail.com>wrote:

> Folks,
>
> I am reusing a code for some enhancements. According to my
> understanding, it is a record with
> some unique string as key and then hash array as the value.
>
> I iterate through the array and print as follows:
>
> foreach my $key (keys %{$abc})
> {
> print "$key ${$abc}{$key} \n";
> }
>
> I get values such like:
> val_0001 HASH(0x186c0060)
> val_0002 HASH(0x187ea490)
> val_0003 HASH(0x18655bc0)
> val_0004 HASH(0x1880fc60)
>
> Can someone tell me how to get the actual value instead of HASH* as above?
>
> Regards,
> Sharan
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
> For additional commands, e-mail: beginners-help [at] perl.org
> http://learn.perl.org/
>
>
>
Hi Sharan,

When you do: print "$key ${$abc}{$key} you get this output: val_0001
HASH(0x186c0060) so that basically means that the value associated with:
val_0001 is a hash.
So if we do something along the lines of:

foreach my $key (keys %{$abc})
{
my %hash = %{$abc}{$key};
foreach my $key2 ( keys %value ) {
print "$key\t$key2\t$value{$key2}\n";
}
}

You should get something like this:
val_0001 key_0001 value_0001
val_0001 key_0002 value_0002
val_0001 key_0003 value_0003
val_0002 key_0001 value_0001
etc....

Of course you should decide if this is really needed if you are only
interested in finding out what is inside $abc for debugging purposes for
instance you might just want to make your life easy and simply use
Data::Dumper to push content to the screen.
For all you know that HASH(0x186c0060) might contain a value that is it self
an array which contains a list of hashes with arrays as values etc... for
just seeing what is in there it is often better to simply use Data::Dumper
as it has been written to deal with all those possibilities so you don't
have to worry about them.

Regards,

Rob

--001485f5cd7ce5ae59048c498ca5--
Rob Coops [ Mo, 26 Juli 2010 14:30 ] [ ID #2045138 ]

Re: hash arrays

On Mon, Jul 26, 2010 at 08:09, Sharan Basappa <sharan.basappa [at] gmail.com> wr=
ote:
> Folks,
>
> I am reusing a code for some enhancements. According to my
> understanding, it is a record with
> some unique string as key and then hash array as the value.
>
> I iterate through the array and print as follows:
>
> foreach my $key (keys %{$abc})
> {
> =C2=A0print "$key ${$abc}{$key} \n";
> }
>
> I get values such like:
> val_0001 HASH(0x186c0060)
> val_0002 HASH(0x187ea490)
> val_0003 HASH(0x18655bc0)
> val_0004 HASH(0x1880fc60)
>
> Can someone tell me how to get the actual value instead of HASH* as above=
?
snip

You are getting HASH(hex number) because the values in the hashref
referred to by $abc are themselves hashrefs. Depending on how the
data is structured, you may be able to say:

for my $outer (keys %$abc)
for my $inner (keys %{$abc->{$outer}})
print "$outer $inner $abc->{$outer}{$inner}\n";
}
}

To find out how the data is structured, try this instead:

use Data::Dumper;
print Dumper $abc;

That will print out the Perl code needed to recreate the structure.
You may also find [perldoc perldsc][0] useful.

[0]: http://perldoc.perl.org/perldsc.html

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ Mo, 26 Juli 2010 14:29 ] [ ID #2045139 ]

AW: hash arrays

Hi,

Sharan Basappa <sharan.basappa [at] gmail.com> asked:
> I am reusing a code for some enhancements. According to my
> understanding, it is a record with
> some unique string as key and then hash array as the value.
>
> I iterate through the array and print as follows:
>
> foreach my $key (keys %{$abc})
> {
> print "$key ${$abc}{$key} \n";
> }
>
> I get values such like:
> val_0001 HASH(0x186c0060)
> val_0002 HASH(0x187ea490)
> val_0003 HASH(0x18655bc0)
> val_0004 HASH(0x1880fc60)
>
> Can someone tell me how to get the actual value instead of HASH* as
> above?

Assuming that $abc is a hash reference, it works like this:

#!/usr/bin/perl -w

use strict;

# $abc is a hash reference
my $abc =3D {
'foo' =3D> 1,
'baz' =3D> 2,
'bar' =3D> 3,
};


foreach my $key (keys %$abc)
{
print "$key $abc->{$key}\n";
}

__END__

HTH,
Thomas

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
t.baetzler [ Mo, 26 Juli 2010 15:21 ] [ ID #2045140 ]

Re: hash arrays

Thank you, Bob & others. I think I now understand better. I have the
source code. So now I should be able to figure this out.

Regards,
Sharan

On Mon, Jul 26, 2010 at 6:00 PM, Rob Coops <rcoops [at] gmail.com> wrote:
>
>
> On Mon, Jul 26, 2010 at 2:09 PM, Sharan Basappa <sharan.basappa [at] gmail.com=
>
> wrote:
>>
>> Folks,
>>
>> I am reusing a code for some enhancements. According to my
>> understanding, it is a record with
>> some unique string as key and then hash array as the value.
>>
>> I iterate through the array and print as follows:
>>
>> foreach my $key (keys %{$abc})
>> {
>> =A0print "$key ${$abc}{$key} \n";
>> }
>>
>> I get values such like:
>> val_0001 HASH(0x186c0060)
>> val_0002 HASH(0x187ea490)
>> val_0003 HASH(0x18655bc0)
>> val_0004 HASH(0x1880fc60)
>>
>> Can someone tell me how to get the actual value instead of HASH* as abov=
e?
>>
>> Regards,
>> Sharan
>>
>> --
>> To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
>> For additional commands, e-mail: beginners-help [at] perl.org
>> http://learn.perl.org/
>>
>>
>
> Hi Sharan,
> When you do:=A0print "$key ${$abc}{$key} you get this output:=A0val_0001
> HASH(0x186c0060) so that basically means that the value associated with:
> val_0001 is a hash.
> So if we do something along the lines of:
> foreach my $key (keys %{$abc})
> {
> =A0my %hash =3D %{$abc}{$key};
> =A0foreach my $key2 ( keys %value ) {
> =A0=A0print "$key\t$key2\t$value{$key2}\n";
> =A0}
> }
> You should get something like this:
> val_0001 =A0 key_0001 =A0value_0001
> val_0001 =A0 key_0002 =A0value_0002
> val_0001 =A0 key_0003 =A0value_0003
> val_0002 =A0 key_0001 =A0value_0001
> etc....
> Of course you should decide if this is really needed if you are only
> interested in finding out what is inside $abc for=A0debugging=A0purposes =
for
> instance you might just want to make your life easy and simply use
> Data::Dumper to push content to the screen.
> For all you know that=A0HASH(0x186c0060) might contain a value that is it=
self
> an array which contains a list of hashes with arrays as values etc... for
> just seeing what is in there it is often better to simply use Data::Dumpe=
r
> as it has been written to deal with all those possibilities so you don't
> have to worry about them.
> Regards,
> Rob

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Sharan Basappa [ Mo, 26 Juli 2010 15:38 ] [ ID #2045141 ]

Re: hash arrays

On Mon, Jul 26, 2010 at 09:21, Thomas B=C3=A4tzler <t.baetzler [at] bringe.com> =
wrote:
snip
> Assuming that $abc is a hash reference, it works like this:
>
> #!/usr/bin/perl -w
>
> use strict;
snip

Don't use -w. Use the [warnings pragma][0] instead. When you say -w
you turn on warnings for everything, even modules that are not
expecting them. Read more in [perldoc perllexwarn][1].

snip
> # $abc is a hash reference
> my $abc =3D {
> =C2=A0'foo' =3D> 1,
> =C2=A0'baz' =3D> 2,
> =C2=A0'bar' =3D> 3,
> };
snip

This is not an accurate model of what the data looks like. You may be
confused by the archaic ${$abc}{key} form of dereferencing; it has the
same results as $abc->{key}. The data looks more like

my $abc =3D {
foo =3D> { a =3D> 1, b =3D> 2, c =3D> 3 },
bar =3D> { d =3D> 4, e =3D> 5, f =3D> 6 },
};


[0]: http://perldoc.perl.org/warnings.html
[1]: http://perldoc.perl.org/perllexwarn.html

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
chas.owens [ Mo, 26 Juli 2010 15:56 ] [ ID #2045142 ]

Re: hash arrays

Chas. Owens wrote:
> Thomas Bätzler:

>> #!/usr/bin/perl -w
>> use strict;
>
> Don't use -w. Use the [warnings pragma][0] instead. When you say -w
> you turn on warnings for everything, even modules that are not
> expecting them. Read more in [perldoc perllexwarn][1].

And that is why I prefer -w.

(Maybe warnings needs a :global.)


Do "modules that are not expecting them" still exist?
Where I code, I haven't seen any for many, many years.

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
rvtol+usenet [ Sa, 14 August 2010 14:00 ] [ ID #2046037 ]
Perl » gmane.comp.lang.perl.beginners » hash arrays

Vorheriges Thema: Accessing array from Perl & DBI using Template Toolkit
Nächstes Thema: Server check