perl学习日记2(哈希结构)

时间:2022-01-29 16:50:01

原文:http://blog.chinaunix.net/space.php?uid=9398085&do=blog&id=1677464

哈希结构

%movies = ('The Shining'=> 'Kubrick', 'Ten Comandments' =>'DeMille','Goonies' =>'Spielberg');

print $movies{'The Shining'};

其中‘The Shining’是关键字,'Kubrick'是值

 

打印哈希结构中的每个关键字

foreach $film (keys %movies){

print "$film\n";

}

输出为:

Ten Comandments

Goonies

The Shining

 

利用keysvalues得到关键字和值

@directors=values %movies;

@film=keys %movies;

print "@directors\n";

print "@film\n";

输出为

DeMille Spielberg Kubrick

Ten Comandments Goonies The Shining]

 

拷贝和组合

%New_hash=%Old_hash;

%Both=(%first,%second); #组合后将随机排列

 

 

利用函数exists测试是否存在某个关键字

if (exists $movies{conan}){ # movies中是否存在conan这个关键字

print "exist!" ;

}

 

删除某个关键字

delete $both{conan};

 

寻找数组中的唯一元素

@fishwords=qw(one fish two fish red fish blue fish);

%seen=();

foreach (@fishwords){

$seen{$_}=1;

}

@uniquewords=keys %seen;

print "@uniquewords";

输出为:blue one red two fish

 

寻找两个数组间的不同部分和交汇部分

@stars=qw(R.Reagan C.Eastwood M.Jackson Cher S.Bono);

@pols=qw(N.Gingrich S.Thurmon R.Reagan S.Bono C.Eastwood M.Thatcher);

%seen=();

foreach (@stars){

$seen{$_}=1; #对电影明星的列表进行迭代操作,依次将$设置为每个名字

}

@intersection=grep($seen{$_},@pols); #@pols中的grep函数对政治家的列表进行迭代操作,依次将$_设置给每个政治家。然后,在哈希结构%seen中寻找该名字

print "@intersection";

输出为:R.Reagan S.Bono C.Eastwood

 

需找两个数组中的不同部分

@difference=grep(! $seen{$_},@pols);

输出为:N.Gingrich S.Thurmon M.Thatcher ,即不是明星的所有政治家

 

读入txt文件,并按输入的关键字输出customer信息

#!/usr/bin/perl -w

 

open(PH,"C:/Documents and Settings/Administrator/桌面/customers.txt") or die "Cannot open customers.txt: $!\n";

while(<PH>){ #文件句柄被读取,每一行被赋予$_

chomp;

($number,$email)=(split(/\s+/,$_)) [1,2]; # $_中的这一行在空格处被分割,只取前两项

$Phone{$number}=$_; #将每一行存入一个哈希结构,keynumbervalue为一条记录

$Email{$email}=$_;

}

close(PH);

 

print "Type 'q' to exit\n";

while (1){

print "\nNumber?";

$number=<stdin>;chomp($number);

$address="";

if(!$number){

print "E-mail?";

$address=<stdin>;chomp($address);

}

 

next if(!$number and !$address);

last if($number eq 'q' or $address eq 'q');

 

if ($number and exists $Phone{$number}) {

print "Customer: $Phone{$number}\n"; #输出关键字为number的值

next;

}

 

if ($address and exists $Email{$address}){

print "Customer: $Email{$address}\n";

next;

}

print "Customer record not found.\n";

next;

}

print "\nAll done.\n";

 

统计单词出现的频度

open(FH,"C:/Documents and Settings/Administrator/桌面/test.txt") or die "cannot open test: $!";

while(<FH>){ #每次读取一行标准输入,为每一行设置$_

while(/(\w[\w-]*)/g){ #以文件中的空格分隔成单词。对$_中的每个单词进行迭代操作

$words{$1}++;

}

}

foreach (keys %words){

print "$_ $words{$_}\n"; #输出每个单词的频度

}

输出为:

peach 2

banana 1

apple 2

orange 2

pear 1

 

统计单词的长度

open(FH,"C:/Documents and Settings/Administrator/×ÀÃæ/test.txt") or die "cannot open test: $!";

while(<FH>){ #每次读取一行,为每一行设置$_

while(/(\w[\w-]*)/g){ #以文件中的空格分隔成单词。对$_中的每个单词进行迭代操作

$seen{length $1}++; #以单词的长度为关键字

}

}

foreach (keys %seen){

print "There are $seen{$_} words have $_ letter(s). \n"; #输出关键字和值

}

输出为:

There are 3 words have 6 letter(s).

There are 1 words have 4 letter(s).

There are 4 words have 5 letter(s).