配列に代入されているハッシュを操作

back
配列の中にはハッシュのリファレンスを入れる。

#!/usr/bin/perl

%mta1 = ( "mps1" => 15,
         "mps2" => 5,
         "mps3" => 12,
         "mps4" => 11 );
%mta2 = ( "mpb1" => "hoge",
         "mpb2" => "hage",
         "mpb3" => "foo",
         "mpb4" => "bar" );

@addr = (\%mta1, \%mta2);
$ad = \%mta2;

$tmp = $addr[1]{"mpb1"};


print "$tmp\n";                # hoge
print $$ad{"mpb3"} . "\n";     # foo

print "\n----\n";
foreach $a (@addr) {
  foreach (keys %$a) {
    print "$_: $$a{$_}\n";
  }
  print "\n";
}

最後のループの出力は
----
mps1: 15
mps3: 12
mps2: 5
mps4: 11

mpb4: bar
mpb3: foo
mpb1: hoge
mpb2: hage

back