Hello monks. I have following structure:
$VAR1 = 'ng1'; $VAR2 = [ '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' ]; $VAR3 = 'ng2'; $VAR4 = [ 'ng2_1', [ 'ng2_1_u1', 'ng2_1_u2', 'ng2_1_u3' ], 'ng2_2', 'ng2_3', 'ng2_4' ]; $VAR5 = 'ng3'; $VAR6 = [ 'ng3_1', 'ng3_2', 'ng3_3', 'ng3_4' ];
Here is the same structure with indexes and indentation for better orientation:
[0] ng1 [1][0] ng1_1 [1][1][0] ng1_1_1 [1][1][1][0] ng1_1_1_u1 [1][1][1][1] ng1_1_1_u2 [1][1][1][2] ng1_1_1_u3 [1][1][2] ng1_1_2 [1][1][3] ng1_1_3 [1][1][4] ng1_1_4 [1][2] ng1_2 [1][3] ng1_3 [1][4] ng1_4 [2] ng2 [3][0] ng2_1 [3][1][0] ng2_1_u1 [3][1][1] ng2_1_u2 [3][1][2] ng2_1_u3 [3][2] ng2_2 [3][3] ng2_3 [3][4] ng2_4 [4] ng3 [5][0] ng3_1 [5][1] ng3_2 [5][2] ng3_3 [5][3] ng3_4
I have function which prints indexes of particular element:
# &findElementPosition(\@tree,"ng1_1_1_u3"); # sub findElementPosition{ my ($tree,$needle) = @_; for my $treeElement (@{$tree}){ if (ref $treeElement eq "ARRAY"){ push @position,0; &findElementPosition($treeElement,$needle); pop @position; $position[-1]++; } else{ if ($treeElement eq $needle){ for (@position){ print "[$_]" } print " " . $treeElement . "\n"; } $position[-1]++; } } }
The code above prints:
[1][1][1][2] ng1_1_1_u3
But, I need a function which is able to print the parent's index. E.g. if I give the input: "ng1_1_1_u3" I want the output: [1][1][0] ng1_1_1
The idea is simple. All that is need before printing is to remove the last element from "@position" array and then decrement the actual last element in this array, something like this:
pop @position; $position[-1]--;
The question is, how can I print coresponding element to those indexes?

In reply to 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.