Skip to content

Instantly share code, notes, and snippets.

@yko
Created October 18, 2011 15:50
Show Gist options
  • Save yko/1295774 to your computer and use it in GitHub Desktop.
Save yko/1295774 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
my %hash;
# Fill %hash with 20 random values 0 - 99
for (1 .. 20) {
my $rand = int(rand(100));
$hash{$rand} = $rand;
}
my @sorted = sort { $hash{$a} <=> $hash{$b} } keys %hash;
print "\nv1\n";
foreach my $key (@sorted[0..9]) {
print $hash{$key} . "\n";
}
print "\nv2\n";
# Note, splice damages @sorted array
foreach my $val (splice(@sorted, 0, 10)) {
print $val . "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment