if (ref $iteminitem eq 'HASH') { foreach $iteminitem1 (sort(keys %{$Myhash{$item}{$iteminitem}})){ .... } }