use strict; use warnings; my @tree = ( 'ng1', [ 'ng1_1', [ 'ng1_1_1', [ 'ng1_1_1_u1', 'ng1_1_1_u2', 'ng1_1_1_u3' ], 'ng1_1_2', 'ng1_1_3', 'ng1_1_4' ], 'ng1_2', 'ng1_3', 'ng1_4' ], 'ng2', [ 'ng2_1', [ 'ng2_1_u1', 'ng2_1_u2', 'ng2_1_u3' ], 'ng2_2', 'ng2_3', 'ng2_4' ], 'ng3', [ 'ng3_1', 'ng3_2', 'ng3_3', 'ng3_4' ], ); sub findElementPosition{ my ($tree,$position,$parent,$needle) = @_; my $previous; my $pos = 0; for my $treeElement (@{$tree}){ if (ref $treeElement eq "ARRAY"){ findElementPosition($treeElement,[@$position,$pos],$previous,$needle); } else { if ($treeElement eq $needle){ print "[$_]" for @$position[0..@$position-2]; print "[",$position->[-1]-1,"] $parent \n"; } $previous = $treeElement; } $pos++; } } my @position; findElementPosition( \@tree, \@position, "", "ng1_1_1_u3" ); findElementPosition( \@tree, \@position, "", "ng3_1" );