SPUG: Another hash/array question from Duane

Yitzchak Scott-Thoennes sthoenna at efn.org
Mon Aug 1 12:47:52 PDT 2005

On Mon, Aug 01, 2005 at 12:34:24PM -0700, Duane Blanchard wrote:
> Thanks to everyone who has helped me out already, I'm nearly done
> (haha). I have looked at data::dumper and it will be very useful for
> part of this project, but I am also using this to teach myself (with
> great assistance from the group) more about data structures and
> references.
> Right now, I'm trying to print the zeroeth element of the zeroeth row
> of the hash in the embedded hash. I've had to sanitze the data for my
> client. On line 45 below, I want to print the first element of the
> first row of my multidimensional array.
> I've been through the perldoc for ref and reference. I've tried the
> arrow operator and doubling up my dollar signs. Nothing seems to work.
> Any further help would be greatly appreciated.

I suggest reading the short but very helpful:
before you go much further, and consulting it at every difficulty
until you internalize the rules it presents.

> while (my ($key, $val) = each %outer_hash)

Ah, my fault; I was trying to show the above as an alternative to the
  for my $key (keys %outer_hash) {

At each iteration, each() returns the key *and it's associated value*
(in list context).  That means instead of

   print $outer_hash{$key}{$val}[...

you should be doing just:

   print $outer_hash{$key}[...


   print $val->[...

($val *is* $outer_hash{$key}, but you need the -> to show you are derefing
the scalar reference in $val, as opposed to $val[... which is looking up
an element in the array @val.

> {	if (ref($val) eq "HASH")
> 	{	print "KEY - $key\n" .
> 				"VAL - $val\n\n";
> 		for ($i = 0; $i < 3; $i++)
> 		{	for ($j = 0; $j < 9; $j++)
> 			{	print $outer_hash{$key}{$val}[$i][$j] . "\t";	# HERE IS THE STICKING POINT
> 			}
> 			print "\n";
> 		}
> 	} 
> 	print "\n";
> }

More information about the spug-list mailing list