最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

perl哈希的一個實例分析

 更新時間:2013年02月17日 14:42:32   作者:  
上一篇文章介紹了hash的入門教程,這篇文章為大家提供一個實例,方便大家深入學(xué)習(xí)

復(fù)制代碼 代碼如下:

#!/bin/perl
use strict; 
use warnings; 

my %movies; 
my $film; 
my %reverse_result; 
my $director; 
my @data; 

%movies = 

  'The Shining'       => 'Kubrick', 
  'Ten Commandments'  => 'DeMille', 
  'Goonies'           => 'Spielberg', 
); 

#輸出哈希的值,輸出的結(jié)果為Kubrick 
print $movies{'The Shining'}; 

#同時輸出鍵和值 
foreach $film(keys %movies) 

   print "$film was directed by $movies{$film}.\n"; 


#添加空格 
print "\n"; 

#哈希結(jié)構(gòu)的切換 
%reverse_result=reverse %movies; 
foreach $director(keys %reverse_result) 

   print "$director directe the $reverse_result{$director}.\n";  
}

#添加空格 
print "\n";

#當(dāng)哈希結(jié)構(gòu)用于列表環(huán)境中時,perl會將hash重新變?yōu)橛申P(guān)鍵詞和鍵值組成的普通列表 
@data=%movies; 
 print "@data\n"; 

#添加空格 
print"\n"; 

#得到的數(shù)組是一個分為奇數(shù)為film,偶數(shù)為director的數(shù)組,或者相反 
#然后我們將數(shù)組賦值給hash 
%movies=@data; 
foreach $director(keys %reverse_result) 

  print "$director directe the $reverse_result{$director}.\n";  
}     
print "The result is not change\n";

以下為輸出結(jié)果:
    F:\>perl\a.pl 
    KubrickGoonies was directed by Spielberg. 
    The Shining was directed by Kubrick. 
    Ten Commandments was directed by DeMille. 

    DeMille directe the Ten Commandments. 
    Spielberg directe the Goonies. 
    Kubrick directe the The Shining. 

    Goonies Spielberg The Shining Kubrick Ten Commandments DeMille 

    DeMille directe the Ten Commandments. 
    Spielberg directe the Goonies. 
    Kubrick directe the The Shining. 

    F:\>

#----測試哈希key的方法:
if(exists $hash{keyval}) 


#----刪除關(guān)鍵字:
delete hash {keyval};

#---清空哈希:
%hash=();

相關(guān)文章

最新評論

喜德县| 上栗县| 晋城| 泰顺县| 洛南县| 泰来县| 越西县| 隆化县| 长岛县| 肇庆市| 井研县| 阳信县| 兴仁县| 高唐县| 襄汾县| 阿坝县| 霍林郭勒市| 睢宁县| 铜陵市| 宝山区| 太谷县| 兴隆县| 襄城县| 剑川县| 涿州市| 博爱县| 金寨县| 南江县| 隆德县| 潞城市| 肥城市| 阿图什市| 玉树县| 尼勒克县| 红桥区| 富顺县| 丹东市| 合作市| 嘉峪关市| 兴国县| 淄博市|