#head of code my @array; foreach my $tag (keys %licensehash) { foreach my $type (keys %{$licensehash{$tag}}) { push(@array, [$tag,$type,$licensehash{$tag}{$type}[3]]); } } @array = sort {$a->[2] cmp $b->[2]} @array; #body of program (used more than once) foreach my $order (@array) { print "$licensehash{@$order[0]}{@$order[1]}[3]"; }