use warnings; use strict; use Data::Dumper; my %com = ( 'mode' => { 'switch1' => { 'type' => ' auto mode for switch1', 'action' => ' completed with assistance.' }, 'switch2' => { 'type' => ' auto mode for switch2', 'action' => 'completed on own.' }, 'switch3' => { 'type' => ' auto mode for switch3', 'action' => ' rasied alarm, incident reported.' }, 'switch4' => { 'type' => ' manual mode for switch4', 'action' => ' complete process manually', }, }, ); # this shows what you have originally # with sorted keys though { local $Data::Dumper::Sortkeys = 1; print Dumper %com; } # you can print out on your own foreach my $key ( sort keys %com ) { print $key, $/; check_and_print( $com{$key} ); } sub check_and_print { my $val = shift; if ( ref $val eq 'HASH' ) { for my $key ( sort keys %{$val} ) { print q{ }, $key, $/; check_and_print( $val->{$key} ); } } else { print q{ }, $val, $/; } }