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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金昌市| 泸州市| 阳原县| 桐梓县| 彰武县| 阿城市| 玉山县| 清水河县| 林口县| 措勤县| 阳泉市| 石屏县| 乌拉特前旗| 古丈县| 碌曲县| 嘉鱼县| 彭州市| 当阳市| 许昌县| 昌图县| 永兴县| 鹤壁市| 页游| 商河县| 黄石市| 搜索| 平泉县| 福建省| 枝江市| 洪湖市| 宁国市| 滨州市| 简阳市| 年辖:市辖区| 铅山县| 哈密市| 齐河县| 故城县| 扎兰屯市| 黄陵县| 麻城市|