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

I asked a similar question earlier. due to my bad example, i sidetracked the helpful commentators. Basically, i need to incrementally add to a multi-dimensional array. In addition, i can't dynamicaly deal with a variable number of dimensions (instead of with code specific to each dimension).

also, if you have another way to structure this, i would appreciate comments. last time, there were suggestions to use a hash with composite keys. i guess i still don't see its benefit yet.

use strict; use warnings; use Data::Dumper; my $ranges = [ [ { I => 3, }, { I => 4, }, { I => 2, }, { I => 6, }, { I => 9, }, ], [ { I => 3, J => 2, }, { I => 4, J => 3, }, { I => 3, J => 4, }, { I => 3, J => 6, }, { I => 4, J => 0, }, { I => 4, J => 1, }, ], [ { I => 4, J => 3, K => 2, }, { I => 4, J => 3, K => 1, }, { I => 4, J => 2, K => 2, }, { I => 2, J => 3, K => 2, }, { I => 6, J => 2, K => 1, }, ], ]; my $loop =0; foreach my $rngGrp ( @{ $ranges } ) { my $ds; my $f=0; foreach my $rngHash ( @{ $rngGrp } ) { my @rngList = sort keys %$rngHash; my ($ii,$vi,$ij,$vj,$ik,$vk); if ( scalar @rngList == 1 ) { $ii = $rngList[0]; $vi = $rngHash->{$ii}; $ds->[$vi] = "line $f"; } elsif ( scalar @rngList == 2 ) { $ii = $rngList[0]; $vi = $rngHash->{$ii}; $ij = $rngList[1]; $vj = $rngHash->{$ij}; $ds->[$vi]->[$vj] = "line $f"; } elsif ( scalar @rngList == 3 ) { $ii = $rngList[0]; $vi = $rngHash->{$ii}; $ij = $rngList[1]; $vj = $rngHash->{$ij}; $ik = $rngList[2]; $vk = $rngHash->{$ik}; $ds->[$vi]->[$vj]->[$vk] = "line $f"; } $f++; } if ( $loop == 0 ) { $ds->[6] = "new line"; $ds->[9] = "newer line"; } elsif ( $loop == 1 ) { $ds->[4]->[0] = "new line"; $ds->[3]->[6] = "newer line"; } elsif ( $loop == 2 ) { $ds->[2]->[3]->[2] = "new line"; $ds->[6]->[2]->[1] = "newer line"; } $loop++; print "DS = $ds\n"; print Dumper $ds; print "\n\n"; }

note that the $ds variable is NOT outside the outer loop. The outer loop represents both the incremental addition to a multidimensional array as well as the variable number of dimensions.

note the inner loop is composed of two functions; one to build/add to an array and one to overwrite existing indicies. this code is part of a huge program and the accessing and modification is a big part of the program.

previous suggestions worked only for the first array of each range. i failed to emphasize that i need to add new indicies to an already existing array. to me, that makes it hard.

Replies are listed 'Best First'.
Re: incremental additions to multidimensional arrays
by GrandFather (Saint) on Jun 14, 2007 at 03:31 UTC

    Perhaps you are looking for:

    use strict; use warnings; use Data::Dump::Streamer; my @ranges = ( [ { I => 3, }, { I => 4, }, { I => 2, }, { I => 6, }, { I => 9, }, ], [ { I => 3, J => 2, }, { I => 4, J => 3, }, { I => 3, J => 4, }, { I => 3, J => 6, }, { I => 4, J => 0, }, { I => 4, J => 1, }, ], [ { I => 4, J => 3, K => 2, }, { I => 4, J => 3, K => 1, }, { I => 4, J => 2, K => 2, }, { I => 2, J => 3, K => 2, }, { I => 6, J => 2, K => 1, }, ], ); my $loop =0; foreach my $rngGrp (@ranges) { my @ds; my $f=0; foreach my $rngHash (@$rngGrp) { my @rngList = sort keys %$rngHash; insert (\@ds, $rngHash, \@rngList, "line $f"); $f++; } if ( $loop == 0 ) { insert (\@ds, undef, [6], "new line"); insert (\@ds, undef, [9], "new line"); } elsif ( $loop == 1 ) { insert (\@ds, undef, [4, 0], "new line"); insert (\@ds, undef, [3, 6], "newer line"); } elsif ( $loop == 2 ) { insert (\@ds, undef, [2, 3, 2], "new line"); insert (\@ds, undef, [6, 2, 1], "newer line"); } $loop++; Dump (\@ds); print "\n\n"; } exit; sub insert { my ($array, $hash, $keys, $text, $dim) = @_; $dim ||= 0; if (! defined $hash) { if ($#$keys > $dim) { $array->[$keys->[$dim]] ||= []; insert ($array->[$keys->[$dim]], $hash, $keys, $text, $dim + + 1); } else { $array->[$keys->[$dim]] = $text; } return; } if ($#$keys > $dim) { $array->[$hash->{$keys->[$dim]}] ||= []; insert ($array->[$hash->{$keys->[$dim]}], $hash, $keys, $text, + $dim + 1); } else { $array->[$hash->{$keys->[$dim]}] = $text; } }

    Prints:

    $ARRAY1 = [ ( undef ) x 2, 'line 2', 'line 0', 'line 1', undef, 'new line', ( undef ) x 2, 'new line' ]; $ARRAY1 = [ ( undef ) x 3, [ ( undef ) x 2, 'line 0', undef, 'line 2', undef, 'newer line' ], [ 'new line', 'line 5', undef, 'line 1' ] ]; $ARRAY1 = [ ( undef ) x 2, [ ( undef ) x 3, [ ( undef ) x 2, 'new line' ] ], undef, [ ( undef ) x 2, [ ( undef ) x 2, 'line 2' ], [ undef, 'line 1', 'line 0' ] ], undef, [ ( undef ) x 2, [ undef, 'newer line' ] ] ];

    DWIM is Perl's answer to Gödel
      wow, your reply is quite impressive. there is much to learn from. i especially like :
      $array->[ ... ] ||= [];
      to either create a array or grab the existing one before the recursive call.

      and i have a deeper appreciation for the use of {} and [] to group elements together :

      $array->[$hash-> { $keys->[$dim] } ] $array->[ $keys->[$dim] ]
      thank you very much! i glad i took the time to re-state my question.

      BTW, before my repost, i read "I know what I mean. Why don't you?" and now to have the author of that article respond to this question is astonishing.

Re: incremental additions to multidimensional arrays
by Anonymous Monk on Jun 14, 2007 at 03:29 UTC
    perldoc perlref
    perldoc -f push
    perldoc -f ref
      yeah, except that push simply adds the new item to the end of the array.

      it would be nice if this worked:

      my ($ref,$monk); $ref->[2]->[1] = "value"; $ref->[3]->[2] = "value"; $monk->[1]->[3] = "value"; push @{ $ref }, $monk;
      and a new entry
      $ref->[1]->[3]

      was created. But instead, this array "monk" is added as

      $ref->[4]

      thank you for your time and effort