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

首頁 > 編程 > Perl > 正文

perl哈希的一個實例分析

2020-10-31 15:17:57
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

#!/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=();

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 曲周县| 封丘县| 海晏县| 房山区| 噶尔县| 泾川县| 东宁县| 商南县| 顺平县| 揭阳市| 阿拉善左旗| 盱眙县| 常德市| 迭部县| 霸州市| 班玛县| 建始县| 静安区| 南郑县| 宜丰县| 凭祥市| 卢龙县| 丁青县| 乐至县| 洛川县| 富锦市| 资阳市| 卓尼县| 黄龙县| 南木林县| 永吉县| 上虞市| 莒南县| 洪江市| 古丈县| 龙口市| 嘉定区| 千阳县| 罗平县| 黑山县| 黎川县|