Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newb Question about Nested Hashes/Arrays

Status
Not open for further replies.

WebSitter

Programmer
Oct 7, 2006
3
US
Hello, new to Perl and have a little issue about printing and using references with regards to nested hashes and arrays.

I currently have a database that's an array inside several hashes (references) all inside a "real" hash. It looks like this:


my %hash = (
'paramterLevelOne' => {
'paramaterLevelTwo' => {
'parameterLevelThree'
=>'Item1', 'Item2', 'Item3', 'Item4'],

What I'm trying to do is print the whole array at the end like this:

@names = $hash{$parameterLevelOne}{$parameterLevelTwo}{$parameterLevelThree};

foreach (@names) {
print @$_;
}

This prints out all the items fine EXCEPT as one single string, not four.

In order to print all four items as separate list elements I have to write all this junk:

@names = ($hash{$parameterLevelOne}->{$parameterLevelTwo}->{$parameterLevelThree}->[0], $hash{$parameterLevelOne}->{$parameterLevelTwo}->{$parameterLevelThree}->[1], and so on for each item I want to assign.

Only then can I iterate over each one individually, doing this:

foreach (@names) {
print $_;
}

I hope I made this clear. Is this the way Perl is supposed to deal with nested arrays? Can you access each nested list as an array without reassigning EACH and EVERY list element? Thanks.
 
wrap double-quotes around an array if you want to print it with a space between each element of the array:

Code:
foreach (@names) {
   print "@$_";
}

or maybe this is what you wanted:

Code:
foreach my $arrays (@names) {
   print "$_" for @$arrays;
   print "\n";
}

- Kevin, perl coder unexceptional!
 
Thanks for the try, but after trying both of your examples I still can't get the results I was looking for. I can get spaces between the items but I can't get perl to iterate each item separately. What I'm trying to do is wrap each list item in the array with its own HTML.

Like this example:

Code:
foreach (@names) {
	print "<p>@$_</p>";
	}

Instead of:

Code:
<p>Name 1</p>
<p>Name 2</p>
<p>Name 3</p>

I get:
Code:
<p>Name 1 Name 2 Name 3</p>

And even before I assign an @names, when I look to see how many elements are actually in that long $hash reference with a little "foreach count++" test, it keeps saying I only have one list item in there, yet at the same time I can still access individual list items by their [] index. Weird.
 
my second example should do what you want:

Code:
foreach my $arrays (@names) {
   print "<p>$_</p>\n" for @$arrays;
}

- Kevin, perl coder unexceptional!
 
Thanks. It worked this time. I think I just forgot to declare the my $arrays.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top