- 论坛徽章:
- 0
|
Here's an example that prints the characters used in the string "an apple a day", sorted in ascending
ASCII order:
%seen = ();
$string = "an apple a day";
foreach $byte (split //, $string) {
$seen{$byte}++;
}
print "unique chars are: ", sort(keys %seen), "\n";
unique chars are: adelnpy
虽然很简单,但是小弟对这个不了解
$seen{$byte}++;
这里不知道是不是可以等价
$seen{$byte}= $seen{$byte}+1;
这里什么意思呢?第一次调用
$seen{a}=0+1
$seen{n}=0+1
$seen{a}=1+1
最后不就是哈西seen当中 a建的值是3 是这样理解吗?
但是后来用key排序,然后输出?那么要 $seen{$byte}++; 后面的++有什么意义?
我去掉++后,运行后结果是这样:
zhang@zhang:~/programming/perl$ perl c-string.pl
Useless use of hash element in void context at c-string.pl line 7.
unique chars are:
为什么就不显示呢?。。诡异阿。。 |
|