#
# さまざまなデータ構造のテスト
# 2008/06/14
#
use strict;
# ハッシュの配列
print "--- TEST 1 ---\n";
my @fruits = (
{ NAME => "メロン", PRICE => 2800, SIZE => "big" },
{ NAME => "りんご", PRICE => 100, SIZE => "middle" },
{ NAME => "みかん", PRICE => 20, SIZE => "small" },
);
foreach my $e (@fruits){
print $e->{NAME} . "\t" . $e->{PRICE} . "\t" . $e->{SIZE} . "\n";
}
# ハッシュの配列への参照
print "--- TEST 2 ---\n";
my $fruits_ref = [
{ NAME => "メロン", PRICE => 2800, SIZE => "big" },
{ NAME => "りんご", PRICE => 100, SIZE => "middle" },
{ NAME => "みかん", PRICE => 20, SIZE => "small" },
];
my $e = $fruits_ref->[1]; # 「りんご」のハッシュへの参照を得る
print $e->{NAME} . "\t" . $e->{PRICE} . "\t" . $e->{SIZE} . "\n";
# ハッシュのハッシュ
print "--- TEST 3 ---\n";
my %hfruits = (
"MELON" => { NAME => "メロン", PRICE => 2800, SIZE => "big" },
"APPLE" => { NAME => "りんご", PRICE => 100, SIZE => "middle" },
"ORANGE" => { NAME => "みかん", PRICE => 20, SIZE => "small" },
);
foreach my $k (keys(%hfruits)){
my $e = $hfruits{$k};
print $k . ":" . $e->{NAME} . "\t" . $e->{PRICE} . "\t" . $e->{SIZE} . "\n";
}
# ハッシュのハッシュへの参照
print "--- TEST 4 ---\n";
my $hfruits_ref = {
"MELON" => { NAME => "メロン", PRICE => 2800, SIZE => "big" },
"APPLE" => { NAME => "りんご", PRICE => 100, SIZE => "middle" },
"ORANGE" => { NAME => "みかん", PRICE => 20, SIZE => "small" },
};
my $e = $hfruits_ref->{"ORANGE"};
print "ORANGE:" . $e->{NAME} . "\t" . $e->{PRICE} . "\t" . $e->{SIZE} . "\n";
|