limiting a loop to 100

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
 
X

Xicheng

use "each" fuction, it's iterated automatically through your hash like
readline to your filehandle...
===================
foreach (1..100) {
my $k = each %myhash;
print "$k => $h{$k}\n";
}
===================
Xicheng
 
X

Xicheng

Xicheng said:
use "each" fuction, it iterated automatically through your hash like
readline to your filehandle...
===================
foreach (1..100) {
my $k = each %myhash;
print "$k => $h{$k}\n";
print "$k => $myhash{$k}\n";
sorry for the typo..
 
U

usenet

Mark said:
foreach (keys %myhash) {
last if $c++ >= 100;
# do stuff
}

Or:
foreach ( (keys(%myhash) )[0..99] ) {
#do stuff;
}

the keys() function returns a list, which you can subscript just like
any other array.
 
G

Glenn Jackman

At 2006-01-18 04:49PM said:
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;

Perhaps:
foreach ((keys %myhash)[0..99]) {
# do stuff
}
 
J

John Bokma

Mark said:
$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:

my @todo = keys %myhash;
@todo = splice @keys, 0, 100;
 
P

Paul Lalli

John said:
my @todo = keys %myhash;
@todo = splice @keys, 0, 100;

Assuming @keys is supposed to be @todo there, why would you add the
extra step? Why copy a potentially huge amount of values only to throw
them away in the very next statement?

my @todo = (keys %myhash)[0..100];

A shame. Seems you could still learn something from us if you didn't.

Paul Lalli
 
J

John Bokma

Abigail said:
John Bokma ([email protected]) wrote on MMMMDXXIII September

{} my @todo = keys %myhash;
{} @todo = splice @keys, 0, 100;


If %myhash is large, that may require a lot of extra memory.

Yup, I pressed the send button a bit too fast (hence the @keys error). The
solution has advantages if you need to use the subset a lot though.
 
X

Xicheng

Abigail said:
If %myhash is large, that may require a lot of extra memory.
I'd write it as:
for (my $i = 100; $i -- && (my ($k, $v) = each %myhash); ) {
# do stuff
}
This is basically the same as:
for (1..100) {
my($k,$v) = each %myhash or last;
}

Xicheng
 
G

Glenn Jackman

At 2006-01-18 08:29PM said:
Glenn Jackman ([email protected]) wrote on MMMMDXXIII September
MCMXCIII in <URL:[] foreach ((keys %myhash)[0..99]) {
[] # do stuff
[] }


That doesn't too well if %myhash happens to contain less than 100 elements.

Thanks for pointing that out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top