復制代碼 代碼如下:
#!/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";
#當哈希結構用于列表環(huán)境中時,perl會將hash重新變?yōu)橛申P鍵詞和鍵值組成的普通列表
@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";
以下為輸出結果:
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=();
您可能感興趣的文章:- php-perl哈希算法實現(xiàn)(times33哈希算法)
- Perl 哈希的創(chuàng)建和引用介紹
- Perl哈希表用法解析
- Perl 哈希Hash用法之入門教程
- perl哈希hash的常見用法介紹
- Perl與JS的對比分析(數(shù)組、哈希)