国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Perl > 正文

perl哈希的一個實例分析

2020-06-04 20:29:49
字體:
來源:轉載
供稿:網友

代碼如下:
#!/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', 
); 

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

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

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


#添加空格 
print "/n"; 

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

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

#添加空格 
print "/n";

#當哈希結構用于列表環境中時,perl會將hash重新變為由關鍵詞和鍵值組成的普通列表 
@data=%movies; 
 print "@data/n"; 

#添加空格 
print"/n"; 

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

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

以下為輸出結果:
    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}) 


#----刪除關鍵字:
delete hash {keyval};

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荃湾区| 遂川县| 来安县| 双鸭山市| 故城县| 蒙城县| 长岭县| 盈江县| 巴东县| 宁乡县| 全州县| 当雄县| 兴义市| 潞西市| 同仁县| 屯门区| 尚义县| 灵台县| 类乌齐县| 乃东县| 英山县| 上饶市| 孟连| 宜阳县| 化德县| 饶平县| 洛扎县| 南开区| 乌兰察布市| 平乡县| 黑山县| 昭苏县| 栾城县| 喀什市| 会宁县| 黄龙县| 荃湾区| 乾安县| 丽水市| 雷州市| 合山市|