my @ordered; while( my $n = keys %hash ) { my @temp; for ( 1 .. $n < 1000 ? $n : 1000 ) { push @temp, scalar each %hash; } delete @hash{ @temp }; push @ordered, @temp; }