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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of Hashes

Status
Not open for further replies.

axman505

Technical User
Jun 20, 2001
489
US
I have a array of hashes. Each hash needs to be first sorted by the numerical key value, and then printed out. I am having trouble making this work. Can anyone offer any hints? Thanks.
 
Show your code as it will be easier to help you find where you're hung up at.

- Rieekan
 
When printing out the hash using DataDumper, i get the following: print Dumper($cleanedData[1]);
Code:
$VAR1 = {
          '4' => {
                   '4_9_1' => 1,
                   '4_5_2' => 2,
                   '4_12_3' => '-',
                   '4_10_2' => '-',
                   '4_15_0' => '-',
                   '4_13_3' => 3,
                   '4_8_2' => 2,
                   '4_6_2' => 2,
                   '4_4_1' => '-',
                   '4_5_3' => '-',
                   '4_9_0' => '-',
                   '4_11_1' => 1,
                   '4_15_1' => 1,
                   '4_11_2' => '-',
                   '4_11_3' => '-',
                   '4_5_1' => 1,
                   '4_6_3' => 3,
                   '4_14_2' => 2,
                   '4_7_2' => '-',
                   '4_11_0' => '-',
                   '4_4_2' => 2,
                   '4_6_1' => '-',
                   '4_13_2' => 2,
                   '4_10_3' => '-',
                   '4_9_2' => 2,
                   '4_7_1' => '-',
                   '4_6_0' => '-',
                   '4_14_0' => '-',
                   '4_14_1' => '-',
                   '4_14_3' => '-',
                   '4_15_3' => '-',
                   '4_12_1' => '-',
                   '4_4_0' => 0,
                   '4_9_3' => '-',
                   '4_7_0' => '-',
                   '4_13_0' => '-',
                   '4_12_0' => 0,
                   '4_15_2' => 2,
                   '4_8_0' => '-',
                   '4_10_0' => 0,
                   '4_7_3' => 3,
                   '4_8_1' => 1,
                   '4_5_0' => '-',
                   '4_10_1' => 1,
                   '4_8_3' => '-',
                   '4_13_1' => '-',
                   '4_4_3' => '-',
                   '4_12_2' => 2
                 },
          '1' => {
                   '0' => 'True'
                 },
          '3' => {
                   '2' => 'not well equipped and too small for class size.'
                 },
          '2' => {
                   '1' => 'supplemented with small amounts of additional materia
l.'
                 }
        };

I am having trouble trying to loop throught the array and the hash in each array. The ultimate goal is to export this data back out to file in a comma seperated manner.

Thanks for the assistance!
 
looks like a hash of hashes, not an array of hashes. Thats the data, but how about the code you have been trying to use to sort the data?
 
You'll also have to give an example of the desired output. There's no point in people guessing, only to be told that you're looking for something different.

Kevin - he says that that's a dump of ``Dumper($cleanedData[1])''. It seems to me that @cleanedData is an array of hashes of hashes, where each array element is a hash of hashes like the one that's posted. I could be wrong though.
 
an array of hashes of hashes - you guys blow me away, your complex data structure handling is mind boggling!
 
well, i changed it to have a 2 demonsionalarray and the a hash. I can get through the arrays with the following code:
Code:
for $i ( 0 .. $#cleanedData ) {
        $aref = $cleanedData[$i];
        $n = @$aref - 1;
        for $j ( 0 .. $n ) {
                print "$cleanedData[$i][$j]\n";
        }
}

When that prints out, i get hash references .. How do i edit this to follow loop through the hash contents?

Thanks.
 
If your array elems are hash references

1. Dereference the element as a hash
2. Use the keys function to get a list of hash keys
3. Itererate over the keys and use the keys to look up the values.

This should do it.
Code:
for $i ( 0 .. $#cleanedData ) {
        $aref = $cleanedData[$i];
        $n = @$aref - 1;
        for $j ( 0 .. $n ) {
            [b]for my $k (keys %{$cleanedData[$i][$j]}) {
                print $cleanedData[$i][$j]->{$k}, "\n";
            }[/b]
        }
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top