免费注册 查看新帖 |

Chinaunix

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

散列自加,也许很简单的一个问题,但是想不明白,悬赏 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-09 15:52 |只看该作者 |倒序浏览
10可用积分
#!/usr/bin/perl -w
use strict;
use warnings;
my @people=qw/hwllo hello yaya llll hwllo hwllo baby llll/;
foreach (@people)
{
    print "数组的内容是:\n";
    print"$_\n";
    }
my %count;
#$count{$_}++ foreach(@people);
print "test hash##########\n";
foreach(@people)
{
    print "($_)\n";
    #my $hash_val=$count{$_};
    #print "now hash is: $hash_val\n";
    print "now hashh ishash_val\n";
   $count{$_}++;
    #$count{$_}=$count{$_};
    #$count{$_}+ 1;
    }


$count{$_}++;这里到底是怎么处理的呢?
一直想不明白

最佳答案

查看完整内容

先$count{$_}, 得到的东西++你说的报错是未初始化吧? 正常的.

论坛徽章:
0
2 [报告]
发表于 2007-11-09 15:52 |只看该作者
先$count{$_}, 得到的东西++

你说的报错是未初始化吧? 正常的.

论坛徽章:
0
3 [报告]
发表于 2007-11-09 16:19 |只看该作者
每次循环: {
  $_为数组中的一个元素
在%count这个hash中, 将key为$_对应的value加1
}

论坛徽章:
0
4 [报告]
发表于 2007-11-09 16:43 |只看该作者
原帖由 Lonki 于 2007-11-9 16:19 发表
每次循环: {
  $_为数组中的一个元素
在%count这个hash中, 将key为$_对应的value加1
}


分析运行结果是这样的,我想知道散列内部怎么变化的
比如

foreach(@people)
{
    print "($_)\n";
    my $hash_val=$count{$_};
    print "now hash is: $hash_val\n";
    $count{$_}++;
    }

这样就报错。
$count{$_}++这个应该怎么分解能看得比较明白?

论坛徽章:
0
5 [报告]
发表于 2007-11-09 17:30 |只看该作者
原帖由 Lonki 于 2007-11-9 16:45 发表
先$count{$_}, 得到的东西++

你说的报错是未初始化吧? 正常的.


不是未初始化

#!/usr/bin/perl -w
use strict;
my @people=qw/hwllo hello yaya llll hwllo hwllo baby llll/;
foreach (@people)
{
    print "数组的内容是:\n";
    print"$_\n";
    }
my %count;
#$count{$_}++ foreach(@people);
print "test hash##########\n";

foreach(@people)
{
    print "($_)\n";
    my $hash_val=$count{$_};
    #print "now hash is: $hash_val\n";
    print "now hashh is: $hash_val\n";    #18行
    $count{$_}++;
    #$count{$_}=$count{$_};
    #$count{$_}+ 1;
    }

##################################
输出的结果是
数组的内容是:
Use of uninitialized value in concatenation (.) or string at D:\workspace\perl\hash.pl line 18.
hwllo
数组的内容是:
hello
数组的内容是:
yaya
数组的内容是:
llll
数组的内容是:
hwllo
数组的内容是:
hwllo
数组的内容是:
baby
数组的内容是:
llll
test hash##########
(hwllo)
now hashh is:
(hello)
now hashh is:
(yaya)
now hashh is:
(llll)
now hashh is:
(hwllo)
now hashh is:1
(hwllo)
now hashh is:2
(baby)
now hashh is:
(llll)
now hashh is:1
Use of uninitialized value in concatenation (.) or string at D:\workspace\perl\hash.pl line 18.
Use of uninitialized value in concatenation (.) or string at D:\workspace\perl\hash.pl line 18.
Use of uninitialized value in concatenation (.) or string at D:\workspace\perl\hash.pl line 18.
Use of uninitialized value in concatenation (.) or string at D:\workspace\perl\hash.pl line 18.



哪位高人幫忙講解下foreach裏邊
$count{$_}++;時散列的變化,哪怕只講一兩個數組元素

暈,輸入法怎麽突然變成了繁體的?

[ 本帖最后由 yuio654 于 2007-11-9 18:06 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2007-11-09 18:09 |只看该作者
Hash初始化了,那个Key-Value对没有初始化。
运行一下下面的代码可能会对你的理解有帮助:

#use warnings;
use strict;
my %aa;
$aa{"a"};
print keys %aa;      #produce nothing
print "\n";
$aa{"a"}++;
print keys %aa;      #produce "a"

论坛徽章:
0
7 [报告]
发表于 2007-11-09 19:46 |只看该作者
原帖由 yuio654 于 2007-11-9 17:30 发表


不是未初始化

#!/usr/bin/perl -w
use strict;
my @people=qw/hwllo hello yaya llll hwllo hwllo baby llll/;
foreach (@people)
{
    print "数组的内容是:\n";
    print"$_\n";
    }
my % ...



这还不是初始化????
你print一个undef自然这样.
但是对一个undef做++结果就是1了

论坛徽章:
0
8 [报告]
发表于 2007-11-12 17:14 |只看该作者
晕,原来是跟数组的上下文搞混了,如果是数组的话,自加应该是数组的个数自加
散列的话,$count{$_}++;
$count{$_}应该是被替换成%count的keys $_对应的values,
应该是散列的值自加,第一次应该是undef+1,

另外,标量可以是undef,但是print undef会报这个错
“Use of uninitialized value in concatenation (.) or string ”
不知道这样理解对不对?


感谢cu,感谢perl,感谢JasonLee8872,Lonki ……:wink:

论坛徽章:
0
9 [报告]
发表于 2007-11-13 10:29 |只看该作者
原帖由 yuio654 于 2007-11-12 17:14 发表
晕,原来是跟数组的上下文搞混了,如果是数组的话,自加应该是数组的个数自加
散列的话,$count{$_}++;
$count{$_}应该是被替换成%count的keys $_对应的values,
应该是散列的值自加,第一次应该是undef+1,
...

这和申明一个变量,然后不赋值一个意思啊,对于不赋值的变量,你print,他当然说你使用了未初始化的变量
还是lonki兄那句话,未初始化的意思

[ 本帖最后由 perljoker 于 2007-11-13 10:31 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP