Perl学习笔记(3)----遍历哈希表的一个容易疏忽的地方
2016-07-25 21:21
483 查看
今天做 Google的 Code Jam 上的一道题目:https://code.google.com/codejam/contest/351101/dashboard#s=p2,用Perl语言解答的。遇到一个关于hash遍历的问题,思考了好一会儿才发现问题所在,为了简化叙述,写了一个简单的遍历哈希表的Perl程序如下:
1 #!/usr/bin/perl 2 my %hash=( 3 1=>"a", 4 2=>"b", 5 3=>"c", 6 4=>"d", 7 5=>"e", 8 6=>"f", 9 7=>"g", 10 8=>"h", 11 9=>"i", 12 0=>"j", 13 ); 14 for(keys %hash){ 15 print "$_ => $hash{$_}\n"; 16 } 17 my $times; 18 for(my $i=1;$i<=4;$i++){ 19 $times=0; 20 print "====================Loop No.$i:=====================\n"; 21 LOOP1: while(my ($key,$value)=each %hash){ 22 $times++; 23 print "\t'$key'=>'$value'\n"; 24 if ($times>=2){ 25 last LOOP1; 26 } 27 } 28 }
该程序 2 ~ 13 行先建立了一个哈希表,然后遍历输出这个哈希表。
接下来的 18 ~ 28 行,用 while 循环和哈希表的 each 函数遍历该哈希表,用 for 循环控制遍历四次,每次遍历只遍历两个哈希表中的值。按照设想,这四次的遍历应当输出同样的内容,但输出如下:
6 => f 3 => c 7 => g 9 => i 2 => b 8 => h 1 => a 4 => d 0 => j 5 => e ====================Loop No.1:===================== '6'=>'f' '3'=>'c' ====================Loop No.2:===================== '7'=>'g' '9'=>'i' ====================Loop No.3:===================== '2'=>'b' '8'=>'h' ====================Loop No.4:===================== '1'=>'a' '4'=>'d'
由结果可以看出,这四次的输出并非都是一样的,这说明,用 while 循环 + each 函数遍历哈希表的时候,如果提前跳出了while循环,那么下次再接着用 each 函数遍历该哈希表的时候,会从上次已经遍历过的关键字的下一个关键字处开始遍历。
如果将 while 循环改成 for 或 foreach 循环呢?(Perl 中 for 和 foreach 其实是等价的):
1 #!/usr/bin/perl 2 my %hash=( 3 1=>"a", 4 2=>"b", 5 3=>"c", 6 4=>"d", 7 5=>"e", 8 6=>"f", 9 7=>"g", 10 8=>"h", 11 9=>"i", 12 0=>"j", 13 ); 14 for(keys %hash){ 15 print "$_ => $hash{$_}\n"; 16 } 17 my $times; 18 for(my $i=1;$i<=4;$i++){ 19 $times=0; 20 print "========== Loop No.$i ==========\n"; 21 foreach(my ($key,$value)=each %hash){ 22 $times++; 23 print "\t'$key'=>'$value' \tand times=$times\n"; 24 } 25 }
输出结果如下:
6 => f 3 => c 7 => g 9 => i 2 => b 8 => h 1 => a 4 => d 0 => j 5 => e ========== Loop No.1 ========== '6'=>'f' and times=1 '6'=>'f' and times=2 ========== Loop No.2 ========== '3'=>'c' and times=1 '3'=>'c' and times=2 ========== Loop No.3 ========== '7'=>'g' and times=1 '7'=>'g' and times=2 ========== Loop No.4 ========== '9'=>'i' and times=1 '9'=>'i' and times=2
每次 foreach 循环会遍历两次,而且并没有改变循环的关键字,有点奇怪啊...
相关文章推荐
- 2016多校联合第二场
- mysql主从复制(二)之原理篇
- Android MaterialProgressDrawable类
- java.net.MalformedURLException: no protocol 可能的解决方法
- Android studio显示行数
- POJ3221——Apple Tree(线段树,dfs序)
- 42.在应用中打开别的应用
- CoordinatorLayout源码解析,探索Behavior机制的奥秘
- php学习笔记(二)php与mysql连接与用php发送SQL查询
- php学习笔记(一)php模板与控制器
- php项目笔记(二)php部分
- php项目笔记(一)mysql准备
- 在CentOS系统中把PHP 5.3.x更新到PHP 5.4.x
- 【Prim 求次小生成树】HDU 4081 Qin Shi Huang's National Road System
- Linux常用命令大全
- SAP Backgroud job
- 华为交换机snmp配置
- 深度学习笔记(三)
- CentOS 6 时间,时区,设置修改及时间同步
- Android之用jadx进行反编译