in reply to while loop not stopping

Your last will only apply the looped it is scoped in, which in this case is your while loop... not your foreach loop.

The following simply ends the looping when it finds twice your '$pics_to_find'.... maybe easier to read at a leter stage to add a variable '$max_pics' instead of always multiplying by 2 everytime.

my $max_pics = $pics_to_find * 2; my $pics_found = 0; foreach my $gal(@gals) { last if($pics_found >= $max_pics); $pics_found++; } print "Found what I wanted " if($pics_found >= $max_pics);