Perl Array of Hashes Data Structure

From GM-RKB
Jump to navigation Jump to search

A Perl Array of Hashes Data Structure is a Perl Array Data Structure whose Array Elements are composed of Perl Hash Data Structures.



References

2010

  • (Melli, 2010) ⇒ Gabor Melli. (2010). “Perl Array of Hashes - Examples." GM-RKB ; Populate manually
Manually define an array of hashes.
   my @file_attachments = (
       {file ⇒ 'test1.zip', price  ⇒ '10.00', desc  ⇒ 'the 1st test'},
       {file ⇒ 'test2.zip', price  ⇒ '12.00', desc  ⇒ 'the 2nd test'},
       {file ⇒ 'test3.zip', price  ⇒ '13.00', desc  ⇒ 'the 3rd test'},
       {file ⇒ 'test4.zip', price  ⇒ '14.00', desc  ⇒ 'the 4th test'}
  );
Get the number of items (hashes) in the array.
   my $file_no = scalar (@file_attachments);
   # $file_no is now: 4 in this instance as there is 4 hashes in the array.
Loop through the hash and printing out all the hash "file" elements.
   for (my $i=0; $i < $file_no; $i++) {
      print '$file_attachments [$i]{'file'}  is:'. $file_attachments [$i]{'file'}."\n";
   }
Loop through the hash and printing out all the hash "price" elements.
   for (my $i=0; $i < $file_no; $i++) {
      print '$file_attachments [$i]{'price'} is:'. $file_attachments [$i]{'price'}."\n";
   }
Loop through the hash and printing out all the hash "desc" elements.
   for (my $i=0; $i < $file_no; $i++) {
      print '$file_attachments [$i]{'desc'} is:'. $file_attachments [$i]{'desc'}."\n";
   }
Sample output of the looks above
$file_attachments [0]{'file'} is: test1.zip
$file_attachments [1]{'file'} is: test2.zip
$file_attachments [2]{'file'} is: test3.zip
$file_attachments [3]{'file'} is: test4.zip

$file_attachments [0]{'price'} is: 10.00
$file_attachments [1]{'price'} is: 12.00
$file_attachments [2]{'price'} is: 13.00
$file_attachments [3]{'price'} is: 14.00

$file_attachments [0]{'desc'} is: the 1st test
$file_attachments [1]{'desc'} is: the 2nd test
$file_attachments [2]{'desc'} is: the 3rd test
$file_attachments [3]{'desc'} is: the 4th test


@LoH = (
    {
       lead     ⇒ "fred",
       friend ⇒ "barney",
    },
    {
       lead    ⇒ "george",
       wife    ⇒ "jane",
       son     ⇒ "elroy",
    },
    {
       lead    ⇒ "homer",
       wife    ⇒ "marge",
       son     ⇒ "bart",
    },
  );

# reading from file
# format: lead=fred friend=barney
while (<> ) {
    $rec = {};
    for $field (split ) {
        ($key, $value) = split /=/, $field;
        $rec->{$key} = $value;
    }
    push @LoH, $rec;
}

# reading from file
# format: lead=fred friend=barney
# no temp
while (<> ) {
    push @LoH, { split /[\s=]+/ };
}

# calling a function that returns a key,value array, like
# "lead","fred","daughter","pebbles"
while (%fields = getnextpairset() ) {
    push @LoH, { %fields };
}

# likewise, but using no temp vars
while (<>) {
    push @LoH, { parsepairs($_) };
}

# add key/value to an element
$LoH[0]{pet} = "dino";
$LoH[2]{pet} = "santa's little helper";

# ACCESS AND PRINT

# one element
$LoH[0]{lead} = "fred";

# another element
$LoH[1]{lead} =~ s/(\w)/\u$1/;

# print the whole thing with refs
for $href (@LoH ) {
    print "{ ";
    for $role (keys %$href ) {
         print "$role=$href->{$role} ";
    }
    print "}\n";
}

# print the whole thing with indices
for $i (0 .. $#LoH ) {
    print "$i is { ";
    for $role (keys %{ $LoH[$i] } ) {
         print "$role=$LoH[$i]{$role} ";
    }
    print "}\n";
}

# print the whole thing one at a time
for $i (0 .. $#LoH ) {
    for $role (keys %{ $LoH[$i] } ) {
         print "element $i $role is $LoH[$i]{$role}\n";
    }
}

2001

  • (Cross, 2001) ⇒ David Cross. (2001). “Data Munging with Perl.” Manning Publications. ISBN:1930110006