http://www.perlmonks.org?node_id=1070408

wakatana has asked for the wisdom of the Perl Monks concerning the following question:

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?
  • Comment on How to access adjacent/parent array OR how to access multi dimensional array with dynamic indexes
  • Select or Download Code

Replies are listed 'Best First'.
Re: How to access adjacent/parent array OR how to access multi dimensional array with dynamic indexes
by hdb (Monsignor) on Jan 13, 2014 at 14:37 UTC

    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" );