Iteration over Hash of Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BFischer
    New Member
    • Mar 2008
    • 1

    Iteration over Hash of Arrays

    Greetings!

    Please forgive my newness to Perl, but I was hoping some of you perl data mungers would be able to help me!

    given an array of hashes as follows:


    [CODE=perl]my %adj = (
    1 => [1,2,4],
    2 => [1,2,3,5],
    3 => [2,3,6],
    4 => [1,4,5,7],
    5 => [2,4,5,6,8],
    6 => [3,5,6,9],
    7 => [4,7,8],
    8 => [5,7,8,9],
    9 => [6,8,9]
    );[/CODE]

    how does one go about iterating over all of the array elements?

    Thanks in advance
    Last edited by eWish; Mar 9 '08, 11:54 PM. Reason: Please Use Code Tags
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Have a look at perldsc. If you have problems post back and we will try and help you further.

    --Kevin

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Definetly read the link eWish posted for you. One way to loop through the data:

      [CODE=perl]
      foreach my $keys (keys %adj) {
      print "$keys\n";
      foreach my $index ( @{$adj{$keys}} ) {
      print "\t$index\n ";
      }
      }[/CODE]

      Comment

      Working...