#!/usr/bin/perl use strict; use Data::Dumper; my %h = (); # stick in some data at varying depths $h{ 1}{ 2}{ 3} = 123; $h{1}{3} = 13; $h{2} = 2; # this array will collect the values my @flat = (); hashTraverse( \%h, \@flat ); print Dumper( \@flat ); sub hashTraverse { my ( $href, $aref ) = ( shift(), shift() ); foreach my $key ( sort keys %$href ) { # this simple example sorts the tree left-right my $val = $href -> { $key }; if ( ref( $val ) eq 'HASH' ) { hashTraverse( $href -> { $key }, $aref ) } else { push @$aref, $val; # or instead whatever else should be done to or with the hash node } } } #### $VAR1 = [ 123, 13, 2 ];