There's an iterator stored in with each hash
来源:互联网 发布:智能电视怎么连接网络 编辑:程序博客网 时间:2024/06/04 20:25
This is the 14-th footnote in Chapter 5 of Oreilly--Learning Perl(3rd) .
[14] Since each hash has its own private iterator, loops using each may be nested, as long as they are iterating over different hashes. And, as long as we're already in a footnote, we may as well tell you: it's unlikely you'll ever need to do so, but you may reset the iterator of a hash by using the keys or values function on the hash. The iterator is also automatically reset if a new list is stored into the entire hash, or if each has iterated through all of the items to the "end" of the hash. On the other hand, adding new key-value pairs to the hash while iterating over it is generally a bad idea, since that won't necessarily reset the iterator. That's likely to confuse you, your maintenance programmer, and each as well
Pay great attention to it please.
Look into this example:
#-----------------------------------------------------------------------------
my %ahash = ("a" => 1, "b" => 2, "c" =>3, "d" =>4");
my $index = 0;
while($index < 3) {
while (my ($key, $val) = each %ahash) {
print "$key ==> $val/n";
if ($key eq "b") {
last;
}
}
$index++;
}
#-----------------------------------------------------------------------------
Assume that the each function returns the pair of key-value by the order they are assigned.
Do you think this piece of function will print:
a ==> 1
b ==> 2
a ==> 1
b ==> 2
a ==> 1
b ==> 2
No!
Totally wrong!
In fact, It will print
a ==> 1
b ==> 2 ($index == 0)
c ==> 3
d ==> 4 ($index == 1, and here while containing each ends)
a ==> 1
b ==> 2 ($index == 2)
Why? There's an iterator stored in with each hash.
The iterator will go in its internal way, no matter what happens external.
And the iterator will NOT reset to the beginning unless you do.
How to reset it? See the 14-th footnote.
How to iterate the hash from the beginning every time?
Use keys function, also see the footnote.
[14] Since each hash has its own private iterator, loops using each may be nested, as long as they are iterating over different hashes. And, as long as we're already in a footnote, we may as well tell you: it's unlikely you'll ever need to do so, but you may reset the iterator of a hash by using the keys or values function on the hash. The iterator is also automatically reset if a new list is stored into the entire hash, or if each has iterated through all of the items to the "end" of the hash. On the other hand, adding new key-value pairs to the hash while iterating over it is generally a bad idea, since that won't necessarily reset the iterator. That's likely to confuse you, your maintenance programmer, and each as well
Pay great attention to it please.
Look into this example:
#-----------------------------------------------------------------------------
my %ahash = ("a" => 1, "b" => 2, "c" =>3, "d" =>4");
my $index = 0;
while($index < 3) {
while (my ($key, $val) = each %ahash) {
print "$key ==> $val/n";
if ($key eq "b") {
last;
}
}
$index++;
}
#-----------------------------------------------------------------------------
Assume that the each function returns the pair of key-value by the order they are assigned.
Do you think this piece of function will print:
a ==> 1
b ==> 2
a ==> 1
b ==> 2
a ==> 1
b ==> 2
No!
Totally wrong!
In fact, It will print
a ==> 1
b ==> 2 ($index == 0)
c ==> 3
d ==> 4 ($index == 1, and here while containing each ends)
a ==> 1
b ==> 2 ($index == 2)
Why? There's an iterator stored in with each hash.
The iterator will go in its internal way, no matter what happens external.
And the iterator will NOT reset to the beginning unless you do.
How to reset it? See the 14-th footnote.
How to iterate the hash from the beginning every time?
Use keys function, also see the footnote.
- There's an iterator stored in with each hash
- How to sum values in an array with different hash
- Warning: Each child in an array or iterator should have a unique "key" prop.
- python基础学习-There's an error in your program:expected an indented block错误解决
- There was an internal api err.. & The entitlements specified in your application’s Code Signing...
- Working With Stored JavaScript in MongoDB
- Warning: Each child in an array or iterator should have a unique "key" prop. Check the render ...
- react Warning: Each child in an array or iterator should have a unique "key" prop. Check the render
- Warning: Each child in an array or iterator should have a unique "key" prop. Check the render ...
- react-native中debug报错信息:Each child in an array or iterator should have a unique "key" prop.
- React总结7:Each child in an array or iterator should have a unique "key" prop.的处理方法
- There is a problem with this website’s security certificate in IE
- Creating a Stored Procedure or Function in an Oracle Database
- SYBASE Error 277 There was a transaction active when exiting the stored procedure '%.*s'. The tempo
- Using Flot's Bar Graph in an Android WebView with Highlighting
- Sorting an array of hash in Ruby
- It's like there's train in your mouth.
- There is an overlap in the region chain
- 简析微软的Ajax:Atlas
- process
- 分页css样式
- 解析Atlas客户端支持(1)
- 计算两个时间之差
- There's an iterator stored in with each hash
- C# 与 Java 的不同点总结(持续追加中)
- 实现GridView Tooltip的功能
- 策略模式的一点自我总结
- java词法的DFA描述
- 分解字符串到字段长度固定的数据行中
- SQLSERVER convert函数的简单用法
- 支持三态的TreeView控件
- 支付宝支持firefox了,请安装插件