use Data::Dumper; my $n = 5; my %hash; @hash{ 0..100_000 } = 0..100_000; print time, "\n"; my %temp; @temp{(keys(%hash))[0..4]} = (values(%hash))[0..4]; %hash = %temp; print time, "\n"; print Dumper(\%hash);