Perl Array of Arrays

From GM-RKB
(Redirected from Perl Arrays of Arrays)
Jump to navigation Jump to search

A Perl Array of Arrays is a Perl array data structure that is an Array of Arrays Data Structure (whose array elements are all Perl Array Data Structures).



References

2010

  • (Melli, 2010) ⇒ Gabor Melli. (2010). “Perl Array of Arrays - Examples." GM-RKB
Populate manually
   my @AoA1 = ([3, 11, 4], [2, 5], [1, 72, 4, 8], );
   my @AoA2 = (["A", \@Vector1], ["B", \@Vector2], ["C", \@Vector3] );
Populate from a file.
   my @AoA3 ;
   while (<>) {
      my @tmp = split;        # Split elements into an array.
      print "tmp=[@tmp]\n" ;
      push @Array3, @tmp ;    # Append the elements to the Array
      push @AoA3, [ @tmp ];   # Add an anonymous array reference to @AoA.
   }
Push into an explicit array index.
   push @{ $AoA[$index] }, "apple", "banana" ;
   push @{ $AoA[$index] }, @record ;
Other alternatives
   $AoA[$i] = [ @array ];   # Safest, sometimes fastest
   $AoA[$i] = \@array;      # Fast but risky, depends on my-ness of array
   @{ $AoA[$i] } = @array;  # A bit tricky

Print

   print "Array max index = [$#AoA]\n" ;
   for my $i (0 .. $#AoA) {
      for $j (0 .. $#{ $AoA[$i] } ) {
         print "element $i $j is $AoA[$i][$j]\n";
      }
   }