Multi-dimensional hashes, I can put data in, but I can"t get it back out as easily.

Multi-dimensional hashes, I can put data in, but I can"t get it back out as easily.

am 28.06.2002 20:55:27 von kkittler

------_=_NextPart_001_01C21ED5.5E22FA34
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I've worked with multi-dimensional arrays, which work quite well using
the method I'm attempting to use for hashes. No such luck. I'm sure I'm
missing something really simple, any help is appreciated.
=20
What I have is a tab delimited file with various categories of
businesses and text business names. There are about 180 categories and
and several business names per category.The program reads the file, line
by line and increments the value of a list when a business name for the
category appears, if the data were:
$category =3D "fav_auto_dealer";
$business =3D "jones";
=20
$votes{$category}{$business}++; #increments value for business name for
specified category
=20
I know that the above list value is being incremented properly and if I
use a command like:
print "$votes{fav_auto_dealer}{jones}";
=20
it returns the appropriate numeric value.
=20
But if I try to do it using a for loop, it doesn't:
for $category (sort keys %votes){
print "

$category"; #return the category OK.
for $business (sort keys %category){ #I think the problem's
here!!!!!
print "
$business: $votes{$category}{$business}
"; #returns
nothing
}
print "";
}
=20
This loop does return the value of $category, however it doesn't return
values for $business. What am I doing wrong?
I've tried using %$category and a few other variations in the second
level loop. The best I can get out of second tier list is a
HASH(hexadecimal) type reference, the worst I get are errors.
=20
Thanks,
Karl Kittler
=20

------_=_NextPart_001_01C21ED5.5E22FA34--

RE: Multi-dimensional hashes, I can put data in, but I can"t get it back out as easily.

am 28.06.2002 21:00:00 von sksingh

for $business (sort keys %category){ ## Wrong

Put =3D> for $business (sort keys %{$vote{category}}){



-----------------------

I've worked with multi-dimensional arrays, which work quite well using
the method I'm attempting to use for hashes. No such luck. I'm sure I'm
missing something really simple, any help is appreciated.
=20
What I have is a tab delimited file with various categories of
businesses and text business names. There are about 180 categories and
and several business names per category.The program reads the file, line
by line and increments the value of a list when a business name for the
category appears, if the data were:
$category =3D "fav_auto_dealer";
$business =3D "jones";
=20
$votes{$category}{$business}++; #increments value for business name for
specified category
=20
I know that the above list value is being incremented properly and if I
use a command like:
print "$votes{fav_auto_dealer}{jones}";
=20
it returns the appropriate numeric value.
=20
But if I try to do it using a for loop, it doesn't:
for $category (sort keys %votes){
print "

$category"; #return the category OK.
for $business (sort keys %category){ #I think the problem's
here!!!!!
print "
$business: $votes{$category}{$business}
"; #returns
nothing
}
print "";
}
=20
This loop does return the value of $category, however it doesn't return
values for $business. What am I doing wrong?
I've tried using %$category and a few other variations in the second
level loop. The best I can get out of second tier list is a
HASH(hexadecimal) type reference, the worst I get are errors.
=20
Thanks,
Karl Kittler
=20
=20

RE: Multi-dimensional hashes, I can put data in, but I can"t get it back out as easily.

am 28.06.2002 21:01:15 von Mark Anderson

----- original message content

But if I try to do it using a for loop, it doesn't:
for $category (sort keys %votes){
print "

$category"; #return the category OK.
for $business (sort keys %category){ #I think the problem's
here!!!!!
print "
$business: $votes{$category}{$business}
"; #returns
nothing
}
print "";
}

This loop does return the value of $category, however it doesn't return
values for $business. What am I doing wrong?
I've tried using %$category and a few other variations in the second
level loop. The best I can get out of second tier list is a
HASH(hexadecimal) type reference, the worst I get are errors.

----- end original message

I believe the second level loop should be:
for $business (sort keys %votes{$category}){


/\/\ark

RE: Multi-dimensional hashes, I can put data in, but I can"t get it back out as easily.

am 28.06.2002 21:02:32 von sksingh

Opps forgot the $

for $business (sort keys %category){ ## Wrong
------------------------------------------------
for $category (sort keys %votes){
print "

$category";
for $business (sort keys %{$votes{$category}}){ =20
print "
$business: $votes{$category}{$business}
"; =20
}
print "";
}

-----------------------

I've worked with multi-dimensional arrays, which work quite well using
the method I'm attempting to use for hashes. No such luck. I'm sure I'm
missing something really simple, any help is appreciated.
=20
What I have is a tab delimited file with various categories of
businesses and text business names. There are about 180 categories and
and several business names per category.The program reads the file, line
by line and increments the value of a list when a business name for the
category appears, if the data were:
$category =3D "fav_auto_dealer";
$business =3D "jones";
=20
$votes{$category}{$business}++; #increments value for business name for
specified category
=20
I know that the above list value is being incremented properly and if I
use a command like:
print "$votes{fav_auto_dealer}{jones}";
=20
it returns the appropriate numeric value.
=20
But if I try to do it using a for loop, it doesn't:
for $category (sort keys %votes){
print "
$category"; #return the category OK.
for $business (sort keys %category){ #I think the problem's
here!!!!!
print "
$business: $votes{$category}{$business}
"; #returns
nothing
}
print "";
}
=20
This loop does return the value of $category, however it doesn't return
values for $business. What am I doing wrong?
I've tried using %$category and a few other variations in the second
level loop. The best I can get out of second tier list is a
HASH(hexadecimal) type reference, the worst I get are errors.
=20
Thanks,
Karl Kittler
=20
=20

--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

RE: Multi-dimensional hashes, I can put data in, but I can"t get it back out as easily.

am 28.06.2002 21:06:38 von kkittler

Thanks,

That did the trick!

Karl Kittler



Opps forgot the $ (and the 's' in votes, but I got the idea)

for $business (sort keys %category){ ## Wrong
------------------------------------------------
for $category (sort keys %votes){
print "

$category";
for $business (sort keys %{$votes{$category}}){ =20
print "
$business: $votes{$category}{$business}
"; =20
}
print "";
}

-----Original Message-----
From: Kittler, Karl=20
Sent: Friday, June 28, 2002 2:55 PM
To: beginners@perl.org
Subject: Multi-dimensional hashes, I can put data in, but I can't get it
back out as easily.


I've worked with multi-dimensional arrays, which work quite well using
the method I'm attempting to use for hashes. No such luck. I'm sure I'm
missing something really simple, any help is appreciated.
=20
What I have is a tab delimited file with various categories of
businesses and text business names. There are about 180 categories and
and several business names per category.The program reads the file, line
by line and increments the value of a list when a business name for the
category appears, if the data were: $category =3D "fav_auto_dealer";
$business =3D "jones";
=20
$votes{$category}{$business}++; #increments value for business name for
specified category
=20
I know that the above list value is being incremented properly and if I
use a command like: print "$votes{fav_auto_dealer}{jones}";
=20
it returns the appropriate numeric value.
=20
But if I try to do it using a for loop, it doesn't:
for $category (sort keys %votes){
print "
$category"; #return the category OK.
for $business (sort keys %category){ #I think the problem's
here!!!!!
print "
$business: $votes{$category}{$business}
"; #returns
nothing
}
print "";
}
=20
This loop does return the value of $category, however it doesn't return
values for $business. What am I doing wrong? I've tried using %$category
and a few other variations in the second level loop. The best I can get
out of second tier list is a
HASH(hexadecimal) type reference, the worst I get are errors.
=20
Thanks,
Karl Kittler
=20