hash 使用

来源:互联网 发布:软件行业净利润率 编辑:程序博客网 时间:2024/06/10 06:23

 

use Data::Dumper;

 

#my %fruit_color = ("apple", "red", "banana", "yellow");

 

my %fruit_color = (

apple => red,             #note : 带不带“”都无所谓

banana => yellow,

);

 

print Dumper(%fruit_color);      

 

#print $fruit_color{apple},"/n";      

#print $fruit_color{"banana"};     

 

my @fruits = keys %fruit_color;     

print $#fruits,"/n";

for ( my $i=0; $i<=$#fruits; ++$i ){ 

my $key=$fruits[$i];

print $i,"[",$key,"]",$fruit_color{$key},"/n";      #{} not []

}            

 

my @colors = values %fruit_color; 

print $#colors,"/n";

for ( my $i=0; $i<=$#colors; ++$i ){ 

my $key=$colors[$i];

print $i,"[",$key,"]/n";      #{} not []

}  

 

原创粉丝点击