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

In reply to iterating a hash of (complex?) arrays by tcf03

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.