Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

iterating a hash of (complex?) arrays

by tcf03 (Deacon)
on Apr 27, 2005 at 03:38 UTC ( #451838=perlquestion: print w/replies, xml ) Need Help??

tcf03 has asked for the wisdom of the Perl Monks concerning the following question:

I threw together the following code while reviewing perldsc and perlref.
#!/usr/bin/perl -w #use strict; my %family = ( 'parents' => ['Barbie', 'Ken'], 'siblings' => ['John', 'Robert'], 'wife' => ["Heather"], 'pets' => ["Miles", "Vanilla", ["Dallas", "some cat +"], ["snoopy", "woodstock"] ], 'inlaws' => [ ["Luke", "Leigh"], ["Carol", "Jack"] ], 'sisinlaw' => ["Lucy"], 'broinlaw' => ["Linus", "Pig Pen"], 'friends' => ["Jack", "Jill", "Peter", "Mac"], 'coworkers'=> ["sammy", "colin", "dave"] ); for my $label ( sort keys(%family) ) { my @names = $family{$label}; print "$label\n"; for my $name ( 0 .. $#names ) { for my $names ( 0 .. $#{ $family{$label} } ) { print "\t$family{$label}->[$names]\n"; for my $x ( 0 .. $#{ $family{$label}->[$names] } ) { print "\t$family{$label}->[$names][$x]\n "; #this is where Im stuck. Id like to # just print out the names in the # arrays } } } }
and it does what I want. Wellllll, almost. Plus I cant use strict; My output looks like this:
broinlaw
	Linus
	Pig Pen
coworkers
	sammy
	colin
	dave
friends
	Jack
	Jill
	Peter
	Mac
inlaws
	ARRAY(0x8069d88)
	Luke
 	Leigh
 	ARRAY(0x8069db8)
	Carol
 	Jack
 parents
	Barbie
	Ken
pets
	Miles
	Vanilla
	ARRAY(0x8069ce0)
	Dallas
 	some cat
 	ARRAY(0x8069d10)
	snoopy
 	woodstock
 siblings
	John
	Robert
sisinlaw
	Lucy
wife
	Heather
if I  use strict; the following code must be removed
for my $x ( 0 .. $#{ $family{$label}->[$names] } ) { print "\t$family{$label}->[$names][$x]\n "; }
and Ill get this output
broinlaw
	Linus
	Pig Pen
friends
	Jack
	Jill
	Peter
	Mac
inlaws
	ARRAY(0x806a030)
	ARRAY(0x806a060)
parents
	Barbie
	Ken
pets
	Miles
	Vanilla
	ARRAY(0x8069fc4)
siblings
	John
	Robert
sisinlaw
	Lucy
wife
	Heather
Which is obviously not my intention either. Any pointers to some help would be greatly appreciated.


Ted

--
"Men have become the tools of their tools."
  --Henry David Thoreau

Replies are listed 'Best First'.
Re: iterating a hash of (complex?) arrays
by ikegami (Pope) on Apr 27, 2005 at 03:53 UTC

    You were using $family{$label}[$names_idx] as an array ref whether it was an array ref or not. I added an if to avoid the problem.

    @names always had one and only one member: a reference to an array of names. You probably meant to do @names = @{$family{$label}};. That you had four nested loops, but only three levels should have been a clue.

    0..$#array is harder to read than doing a foreach on the array.

    Here are the changes:

    foreach my $label ( sort keys(%family) ) { print "$label\n"; foreach my $name ( @{ $family{$label} } ) { print "\t$name\n"; if (ref($name)) { foreach my $inner_name ( @{ $name } ) { print "\t\t$inner_name\n"; } } } }
    if I use strict, the following code must be removed

    That's not a good way of thinking. The code needed to be fixed, not removed. The problem existed whether use strict was used or not. The error was just silent when you removed the use strict.

Re: iterating a hash of (complex?) arrays
by Roy Johnson (Monsignor) on Apr 27, 2005 at 03:56 UTC
    Is this about what you're looking for?
    for my $label (sort keys %family) { my @names = @{$family{$label}}; print "$label\n"; for my $name (@names) { print ref($name) ? "\t@$name\n" : "\t$name\n"; } }
    Notice that you don't generally have to iterate through the index of an array. Note also that you were fetching an array ref into an array (@names). You need to dereference it, as I have done here.

    Caution: Contents may have been coded under pressure.
      Thanks, that worked well.
      Ted
      --
      "Men have become the tools of their tools."
        --Henry David Thoreau
Re: iterating a hash of (complex?) arrays
by tcf03 (Deacon) on Apr 27, 2005 at 03:39 UTC
    for the record I did use readmore tags - but they don't seem to be working. my apologies, if I messed up.


    Ted

    --
    "Men have become the tools of their tools."
      --Henry David Thoreau
      The readmore tags are working. You only see their effect when you're looking at a parent node (such as Seekers of Perl Wisdom, in this case), not when you view the node directly.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://451838]
Approved by moot
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2020-06-06 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (41 votes). Check out past polls.

    Notices?