perl哈希的一個(gè)實(shí)例分析
#!/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',
);
#輸出哈希的值,輸出的結(jié)果為Kubrick
print $movies{'The Shining'};
#同時(shí)輸出鍵和值
foreach $film(keys %movies)
{
print "$film was directed by $movies{$film}.\n";
}
#添加空格
print "\n";
#哈希結(jié)構(gòu)的切換
%reverse_result=reverse %movies;
foreach $director(keys %reverse_result)
{
print "$director directe the $reverse_result{$director}.\n";
}
#添加空格
print "\n";
#當(dāng)哈希結(jié)構(gòu)用于列表環(huán)境中時(shí),perl會將hash重新變?yōu)橛申P(guān)鍵詞和鍵值組成的普通列表
@data=%movies;
print "@data\n";
#添加空格
print"\n";
#得到的數(shù)組是一個(gè)分為奇數(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";
以下為輸出結(jié)果:
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})
{
}
#----刪除關(guān)鍵字:
delete hash {keyval};
#---清空哈希:
%hash=();
相關(guān)文章
Perl實(shí)現(xiàn)刪除Windows下的圖片緩存縮略圖Thumbs.db
這篇文章主要介紹了Perl實(shí)現(xiàn)刪除Windows下的圖片緩存縮略圖Thumbs.db,本文實(shí)現(xiàn)了批量刪除Thumbs.db文件,需要的朋友可以參考下2014-12-12
Perl 和 StrawberryPerl 與 ActivePerl 的區(qū)別詳解
這篇文章主要介紹了Perl 和 StrawberryPerl 與 ActivePerl 的區(qū)別詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
使用perl實(shí)現(xiàn)拆分?jǐn)?shù)據(jù)表(mysql)并遷移數(shù)據(jù)實(shí)例
這篇文章主要介紹了使用perl實(shí)現(xiàn)拆分?jǐn)?shù)據(jù)表(mysql)并遷移數(shù)據(jù)實(shí)例,本文提供了3個(gè)腳本,分別用于拆分?jǐn)?shù)據(jù)表、遷移數(shù)據(jù)、插入測試數(shù)據(jù),需要的朋友可以參考下2014-10-10
perl 調(diào)試命令的相關(guān)知識小結(jié)
有關(guān) perl 調(diào)試命令的相關(guān)知識,有需要的朋友可以參考下2013-02-02
Perl使用File::Basename獲取文件擴(kuò)展名的代碼
本文為大家介紹的這個(gè)例子,實(shí)現(xiàn)了獲取/home/topgkw中所有文件后綴,其中目錄返回空值2013-02-02
讓apache2以cgi方式運(yùn)行perl cgi程序的實(shí)現(xiàn)方法
讓apache2以cgi方式運(yùn)行perl cgi程序的方法,供大家學(xué)習(xí)參考2013-02-02

