#!/usr/bin/perl -w use strict; use Heap::Simple; my %Stats = map { $_ => int rand 1000 } "AA".."FF"; my $NumOfPortsToGet = 10; my $heap = Heap::Simple->new (order => '<', elements => 'Any', max_count => $NumOfPortsToGet, dirty => 1, ); for my $key (keys %Stats ) { $heap->key_insert($Stats{$key}, $key); } print "$_ => $Stats{$_}\n" while defined($_ = $heap->extract_first);