Perl, really?!?

This just cost me two hours. Perl’s each continues where you left of the next time you iterate over the same hash!

use strict;
use warnings;

my %h = (
    'a' => 'A',
    'b' => 'B',
    'c' => 'C',
    'd' => 'D' );

my $i=0 ;
while( my( $n, $v ) = each %h ) {
    print "(1) $n => $v\n";
    if( ++$i == 3 ) {
        last;
    }
}
while( my( $n, $v ) = each %h ) {
    print "(2) $n => $v\n";
}
1;

prints:

(1) c => C
(1) a => A
(1) b => B
(2) d => D

If you remove the last in the first loop, the second loop will print out all four elements.

Of course, given that Perl randomizes the iteration sequence (another insanity if you ask me), your second loop will print entirely different elements every time you run it. No, in this case the problem is not between keyboard and chair, at least not between my keyboard and my chair.