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.


In reply to incremental additions to multidimensional arrays by Anonymous Monk

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.