2011-02-09 wcdj
(1) exists函数 (2) delete函数 (3) 哈希值内插 (4) %ENV哈希 3道应用题目 (1) exists函数 用于检查哈希中是否有某个键。它能返回真或假,分别表示键存在与否,和键对应的值无关。 print "exists函数/n"; my %my_hash = ( "wcdj"=>"C++","gerry"=>"Perl","yj"=>"Python" ); print "each函数/n"; while ( ($key, $value) = each %my_hash ) { print "$key => $value/n"; } print "检查哈希中是否有某个键/n"; if (exists $my_hash{"gerry"}) { print "have/n";# 输出此句 } else { print "not have/n"; } if (exists $my_hash{"none"}) { print "have/n"; } else { print "not have/n";# 输出此句 }
(2) delete函数 用于从哈希中删除指定的键和其相对应的值。假如没有这样的键,它就会直接结束,而不会出现任何警告或错误信息。
print "exists函数/n"; my %my_hash = ( "wcdj"=>"C++","gerry"=>"Perl","yj"=>"Python" ); print "each函数/n"; while ( ($key, $value) = each %my_hash ) { print "$key => $value/n"; } print "检查哈希中是否有某个键/n"; if (exists $my_hash{"gerry"}) { print "have/n";# 输出此句 } else { print "not have/n"; } print "delete函数/n"; my $person = "gerry"; delete $my_hash{$person};# 删除$person的信息 if (exists $my_hash{"gerry"}) { print "have/n"; } else { print "not have/n";# 输出此句 }
注意:执行delete之后,并不是将undef存入哈希值。在delete之后,键便不会出现在哈希之中,但是存入undef之后,键却一定会存在。 (3) 哈希值内插 注意:可以将单一哈希元素内插到双引号引起的字符串中。但是整个哈希的内插是不支持的。 (4) %ENV哈希
print "%ENV哈希/n"; print "PATH is $ENV{PATH}/n"; # 根据系统的设定输出如下 %ENV哈希 PATH is /usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/cygdrive/c/Perl/site/bin:/cygdrive/c/Perl/bin:/cygdrive/c/Python26/Scripts:/cygdrive/c/Python26/:/cygdrive/c/Tcl/bin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS:/cygdrive/c/WINDOWS/System32/Wbem:/cygdrive/c/Program Files/Microsoft SQL Server/90/Tools/binn/:/cygdrive/c/Python25:/cygdrive/d/Program Files/Vim:%APPDATA%/Python/Scripts:/cygdrive/c/Program Files/Microsoft Visual Studio/Common/Tools/WinNT:/cygdrive/c/Program Files/Microsoft Visual Studio/Common/MSDev98/Bin:/cygdrive/c/Program Files/Microsoft Visual Studio/Common/Tools:/cygdrive/c/Program Files/Microsoft Visual Studio/VC98/bin:/home/WCDJ/ns-allinone-2.27/bin:/home/WCDJ/ns-allinone-2.27/tcl8.4.5/unix:/home/WCDJ/ns-allinone-2.27/tk8.4.5/unix
【应用1】 编程读入用户指定的名字并且汇报相应的姓。 my %last_name_hash = ( "Jia"=>"Yang","Ming"=>"Li","San"=>"Zhang" ); print "Please enter a first name: ";# 输入名 chomp(my $name=<STDIN>); # 检查次键是否存在 if (exists $last_name_hash{$name}) { # print "have/n"; print "That's $name $last_name_hash{$name}"; } else { print "not have/n"; }
【应用2】 编程读取一系列单词,每行一个直到文件结束,然后打印每个单词出现次数的列表。
my(@words, %count, $word);# 声明变量(可以省略) chomp(@words=<STDIN>);# UNIX下,按Ctrl-D结束输入 # 记录 foreach $word (@words) { $count{$word} += 1; } # 输出 foreach $word (sort keys %count) { print "$word was seen $count{$word} times./n"; }
【应用3】 编程输出%ENV哈希所有的键/值对,输出按照ASCII编码排序,分两列打印(让打印结果纵向对齐)。
my $longest=0; # 记录最长的字符串 foreach my $key (keys %ENV) { my $key_length=length($key); $longest=$key_length if $key_length>$longest; } # 输出 foreach my $key (sort keys %ENV) { printf "%-${longest}s %s/n", $key, $ENV{$key};# 向左对齐 }
输出:
!C: C:/Documents and Settings/WCDJ/桌面 !G: G:/CrossPlatform/ns2_tcl/LEACH/Do_Myself/bin ALLUSERSPROFILE C:/Documents and Settings/All Users APPDATA C:/Documents and Settings/WCDJ/Application Data CLIENTNAME Console COLINUX C:/coLinux COMMONPROGRAMFILES C:/Program Files/Common Files COMPUTERNAME WCDJ_PC COMSPEC C:/WINDOWS/system32/cmd.exe CVS_RSH /bin/ssh DEFLOGDIR C:/Documents and Settings/All Users/Application Data/McAfee/DesktopProtection FP_NO_HOST_CHECK NO HOME /home/WCDJ HOMEDRIVE C: HOMEPATH /Documents and Settings/WCDJ HOSTNAME WCDJ_PC INCLUDE C:/Program Files/Microsoft Visual Studio/VC98/atl/include;C:/Program Files/Microsoft Visual Studio/VC98/mfc/include;C:/Program Files/Microsoft Visual Studio/VC98/include INFOPATH /usr/local/info:/usr/info:/usr/share/info:/usr/autotool/devel/info:/usr/autotool/stable/info: LANG zh_CN LD_LIBRARY_PATH /home/WCDJ/ns-allinone-2.27/otcl-1.8:/home/WCDJ/ns-allinone-2.27/lib:/usr/X11R6/lib:/usr/local/lib LIB C:/Program Files/Microsoft Visual Studio/VC98/mfc/lib;C:/Program Files/Microsoft Visual Studio/VC98/lib LOGONSERVER //WCDJ_PC MAKE_MODE unix MANPATH /usr/local/man:/usr/man:/usr/share/man:/usr/autotool/devel/man::/usr/ssl/man MSDEVDIR C:/Program Files/Microsoft Visual Studio/Common/MSDev98 NUMBER_OF_PROCESSORS 2 OLDPWD /home/WCDJ OS Windows_NT PATH /usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/cygdrive/c/Perl/site/bin:/cygdrive/c/Perl/bin:/cygdrive/c/Python26/Scripts:/cygdrive/c/Python26/:/cygdrive/c/Tcl/bin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS:/cygdrive/c/WINDOWS/System32/Wbem:/cygdrive/c/Program Files/Microsoft SQL Server/90/Tools/binn/:/cygdrive/c/Python25:/cygdrive/d/Program Files/Vim:%APPDATA%/Python/Scripts:/cygdrive/c/Program Files/Microsoft Visual Studio/Common/Tools/WinNT:/cygdrive/c/Program Files/Microsoft Visual Studio/Common/MSDev98/Bin:/cygdrive/c/Program Files/Microsoft Visual Studio/Common/Tools:/cygdrive/c/Program Files/Microsoft Visual Studio/VC98/bin:/home/WCDJ/ns-allinone-2.27/bin:/home/WCDJ/ns-allinone-2.27/tcl8.4.5/unix:/home/WCDJ/ns-allinone-2.27/tk8.4.5/unix PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.tcl;.py;.pyw PKG_CONFIG_PATH :/usr/X11R6/lib/pkgconfig PROCESSOR_ARCHITECTURE x86 PROCESSOR_IDENTIFIER x86 Family 6 Model 15 Stepping 13, GenuineIntel PROCESSOR_LEVEL 6 PROCESSOR_REVISION 0f0d PROGRAMFILES C:/Program Files PROMPT $P$G PS1 /[/033]0;/w/007 /033[32m/]/u@/h /[/033[33m/w/033[0m/] $ PWD /home/WCDJ/perl_test RCA_LIBRARY /home/WCDJ/ns-allinone-2.27/ns-2.27/mit/rca SESSIONNAME Console SHLVL 1 SYSTEMDRIVE C: SYSTEMROOT C:/WINDOWS TCL_LIBRARY /home/WCDJ/ns-allinone-2.27/tcl8.4.5/library:/usr/lib TEMP /cygdrive/c/DOCUME~1/WCDJ/LOCALS~1/Temp TERM cygwin TMP /cygdrive/c/DOCUME~1/WCDJ/LOCALS~1/Temp USER WCDJ USERDOMAIN WCDJ_PC USERNAME WCDJ USERPROFILE C:/Documents and Settings/WCDJ VS90COMNTOOLS C:/Program Files/Microsoft Visual Studio 9.0/Common7/Tools/ VSEDEFLOGDIR C:/Documents and Settings/All Users/Application Data/McAfee/DesktopProtection WINDIR C:/WINDOWS _ ./hash uAMPS_LIBRARY /home/WCDJ/ns-allinone-2.27/ns-2.27/mit/uAMPS