use strict; use warnings; sub loop_over_hash { my( $keys, $href ) = @_; print join( ',', @$keys , $href ), "\n" and return unless 'HASH' eq ref $href; loop_over_hash( [ @$keys, $_ ], $href->{$_} ) for keys %$href; } my %hash = ( a => { 1 => { x => 5, y => 6 }, 2 => { s => 7, t => 8 } }, b => { 3 => 'end', }, ); loop_over_hash [], \%hash;