国产探花免费观看_亚洲丰满少妇自慰呻吟_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=();

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荆门市| 上思县| 滦平县| 郴州市| 屯留县| 肇州县| 通山县| 老河口市| 牙克石市| 蒲城县| 如皋市| 雷州市| 利川市| 泗洪县| 辽宁省| 伊通| 樟树市| 汕尾市| 曲周县| 清水县| 平谷区| 彭州市| 汾阳市| 辰溪县| 昌黎县| 库车县| 乌兰浩特市| 青铜峡市| 漾濞| 长子县| 麻城市| 资中县| 惠州市| 通河县| 固阳县| 郑州市| 杭锦旗| 麻栗坡县| 长岭县| 张家川| 威海市|