sub expand { my ($key) = @_; return sort { $a <=> $b } $key, map { expand($_) } @{$hash{$key}} }