M
Mark
I have a loop similar to the following:
foreach (keys %myhash) {
# do stuff
}
My hash table can be quite large in certain situations, and so I need to
modify the loop such that it only iterates only 100 times. Can I limit
it cleverly? I know I can do it by keeping a counter, like so:
$c = 0;
foreach (keys %myhash) {
last if $c++ >= 100;
# do stuff
}
But this seems clunky to me. I am curious as to whether there is a more
elegant way (or "trick"), such as something like this:
foreach (grep 0,100 keys %myhash) {
# do stuff
}
Am I stuck with the clunky way?
Still learnin'
Mark
foreach (keys %myhash) {
# do stuff
}
My hash table can be quite large in certain situations, and so I need to
modify the loop such that it only iterates only 100 times. Can I limit
it cleverly? I know I can do it by keeping a counter, like so:
$c = 0;
foreach (keys %myhash) {
last if $c++ >= 100;
# do stuff
}
But this seems clunky to me. I am curious as to whether there is a more
elegant way (or "trick"), such as something like this:
foreach (grep 0,100 keys %myhash) {
# do stuff
}
Am I stuck with the clunky way?
Still learnin'
Mark