I would carry it around in your recursion. I have also eliminated the global @position:

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_u +3' ], '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..@$posit +ion-2]; print "[",$position->[-1]-1,"] $parent + \n"; } $previous = $treeElement; } $pos++; } } my @position; findElementPosition( \@tree, \@position, "", "ng1_1_1_u3" ); findElementPosition( \@tree, \@position, "", "ng3_1" );

In reply to Re: How to access adjacent/parent array OR how to access multi dimensional array with dynamic indexes by hdb
in thread How to access adjacent/parent array OR how to access multi dimensional array with dynamic indexes by wakatana

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.