Monday, June 21, 2010

mix of hash & array (perl)

  • array of hashes
@AoH = (
    {
       husband  => "barney",
       wife     => "betty",
       son      => "bamm bamm",
    },

)
push @AoH, { husband => "fred", wife => "wilma", daughter => "pebbles" };
$AoH[0]{husband} = "fred";
$AoH[1]{husband} =~ s/(\w)/\u$1/;

  • array of arrays
@AoA = (
         [ "fred", "barney" ],
         [ "george", "jane", "elroy" ],
         [ "homer", "marge", "bart" ],
);
$AoA[2][3]
$ref_to_AoA->[2][3]


  • hash of arrays
%HoA = (
  flintstones => [ "fred","barney" ],
…)
$HoA{teletubbies} = [ "tinky winky", "dipsy", "laa-laa", "po" ];
$HoA{flintstones}[0] = "Fred";


  • hash of hashes
%HoH = (
    flintstones => {
        husband   => "fred",
        pal       => "barney",
    },
...
)
$HoH{ mash } = {
    captain  => "pierce",
    major    => "burns",
    corporal => "radar",
};
$role=$HoH{$family}{$role};

No comments: