#!/usr/bin/perl use strict; use warnings; use Tie::Hash::Sorted; my $numerically = sub { my $hash = shift; [ sort { $a <=> $b } keys %$hash ]; }; tie my %hash, 'Tie::Hash::Sorted', 'Sort_Routine' => $numerically; %hash = map { $_ => undef } 1..25; for ( keys %hash ) { tie %{ $hash{ $_ } }, 'Tie::Hash::Sorted', 'Sort_Routine' => $numerically; %{ $hash{ $_ } } = map { (int rand 5000) => undef } 1..5; for my $key ( keys %{ $hash{ $_ } } ) { @{ $hash{ $_ }{ $key } }{ qw/first last min max/ } = map { int(rand 100) + 1 } 1..4; } } # Modify %hash however you want and don't worry about it for my $o_key ( keys %hash ) { for my $i_key ( keys %{ $hash{ $o_key } } ) { print "$o_key : $i_key\n"; # do something with $hash{ $o_key }{ $i_key } if you want } }