foreach (hash key){ my $last_one = pop(@{$hash{$_}}); print "$_ $last_one \n"; } so that rtesult is going to be: key3 2 key2 9 key4 8 key1 7 and in next iteration : key2 9 key3 8 key1 1