对匿名哈希的创建与使用
#!/usr/bin/perl
use warnings;
use strict;
普通方法定义hash引用
my %gilligan_info = (
name => 'Gilligan',
hat => 'White',
shirt => 'Red',
position => 'First Mate',
);
my %shipper_info = (
name => 'Shipper',
hat => 'Black',
shirt => 'Blue',
position => 'Captain',
);
my @crew = (/%gilligan_info, /%shipper_info);
my $name = $crew[0]{'name'};
print $name."/n";
my @keys = keys %{$crew[0]};# attention to the dereference of the hash element of the array
print "@keys./n";
print $crew[1]->{'name'}."/n";
创建匿名hash表,注意书写方法
my @new_crew = (
{
name => 'Gilligan',
hat => 'White',
shirt => 'Red',
position => 'First Mate',
},
{
name => 'Skipper',
hat => 'Black',
shirt => 'Blue',
position => 'Caption',
},
);
@keys = %{$new_crew[1]};
print "@keys./n";
本例结构引用自intermidiate perl