免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3005 | 回复: 7
打印 上一主题 下一主题

遍历hash出错 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-21 17:26 |只看该作者 |倒序浏览
代码如下:
sub func()
{
        my %hash =
        (
                aaa => 111,
                bbb => 222,
                ccc => 333
        );
       
        return \%hash;
}


my $ref_hash = func();

foreach ( my ($key,$value) = each %{$ref_hash} )
{
        print $key," => ",$value,"\n";
}


结果是:
bbb => 222
bbb => 222

我预想的结果应该是:
aaa => 111
bbb => 222
ccc => 333

[ 本帖最后由 qfmeal 于 2009-5-21 17:36 编辑 ]

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
2 [报告]
发表于 2009-05-21 17:28 |只看该作者
$enter 是啥?

论坛徽章:
0
3 [报告]
发表于 2009-05-21 17:37 |只看该作者
$enter 是多余的。

论坛徽章:
0
4 [报告]
发表于 2009-05-21 17:39 |只看该作者
把foreach改成while即可

参见each函数的说明:
When called in list context, returns a 2-element list consisting of the key and value for the next element of a hash, so that you can iterate over it.
each函数只是返回一个键值对,而不是所有的键值对

[ 本帖最后由 yashiro_lj 于 2009-5-21 17:47 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2009-05-21 17:43 |只看该作者
each返回hash的第一个键值对
所以

  1. foreach(my($key,$value)=(bbb,222))
复制代码

这个循环执行了两次,因为列表有两个元素($key,$value)

你可以打印一下$_看看

  1. sub func()
  2. {
  3.         my %hash =
  4.         (
  5.                 aaa => 111,
  6.                 bbb => 222,
  7.                 ccc => 333
  8.         );
  9.       
  10.         return \%hash;
  11. }


  12. my $ref_hash = func();

  13. foreach ( my ($key,$value) = each %{$ref_hash} )
  14. {
  15.                                 print $_,"\n";
  16.         print $key," => ",$value,"\n";
  17. }

复制代码

[ 本帖最后由 churchmice 于 2009-5-21 17:45 编辑 ]

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
6 [报告]
发表于 2009-05-21 17:51 |只看该作者
原帖由 churchmice 于 2009-5-21 17:43 发表
each返回hash的第一个键值对

对 hash 来说不存在第几的问题。

论坛徽章:
0
7 [报告]
发表于 2009-05-21 17:55 |只看该作者
改成while就好了。

另外我原想each %hash是返回一个键字/数值对的。

论坛徽章:
0
8 [报告]
发表于 2009-05-25 12:21 |只看该作者

perl cookbook里面的例子.

Traversing a Hash

#-----------------------------
while(($key, $value) = each(%HASH)) {
    # do something with $key and $value
}
#-----------------------------
foreach $key (keys %HASH) {
    $value = $HASH{$key};
    # do something with $key and $value
}
#-----------------------------
# %food_color per the introduction
while(($food, $color) = each(%food_color)) {
    print "$food is $color.\n";
}
# Banana is yellow.
#
# Apple is red.
#
# Carrot is orange.
#
# Lemon is yellow.

foreach $food (keys %food_color) {
    my $color = $food_color{$food};
    print "$food is $color.\n";
}
# Banana is yellow.
#
# Apple is red.
#
# Carrot is orange.
#
# Lemon is yellow.
#-----------------------------
print

"$food

is

$food_color{$food}.\n"

#-----------------------------
foreach $food (sort keys %food_color) {
    print "$food is $food_color{$food}.\n";
}
# Apple is red.
#
# Banana is yellow.
#
# Carrot is orange.
#
# Lemon is yellow.
#-----------------------------
while ( ($k,$v) = each %food_color ) {
    print "Processing $k\n";
    keys %food_color;               # goes back to the start of %food_color
}
#-----------------------------
# download the following standalone program
#!/usr/bin/perl
# countfrom - count number of messages from each sender

$filename = $ARGV[0] || "-";

open(FILE, "<$filename")         or die "Can't open $filename : $!";

while(<FILE>) {
    if (/^From: (.*)/) { $from{$1}++ }
}

foreach $person (sort keys %from) {   
    print "$person: $from{$person}\n";
}

#-----------------------------
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP