in reply to pre-size a two-dimensional array

Perl doesn't have two-dimensional arrays. What you are actually using is an array where each element happens to be a reference to another array.

When dealing with a single array, Perl will expand it as needed, so there's rarely any need to pre-define its size. Because of autovivification, the same applies to "2d arrays".

If you really did want to pre-allocate your "2d array", you could do it as follows:

my @summary; $#summary = $numWks - 1; foreach (@summary) { my @days; $#days = 7 - 1; $_ = \@days; }

However, I think a simple my @summary; will suffice for you.

Replies are listed 'Best First'.
Re^2: pre-size a two-dimensional array
by rightfield (Sexton) on Jul 12, 2007 at 03:44 UTC
    Hi ikegami,

    Thank you. After reading your post my decision is to not pre-allocate the array and I have decided on using an array of hashes.

    In the loop section where I am using the array and hash could you tell me if the hash 'dayHash' filled and pushed onto the @summary array for $wk=0 is different than the 'dayHash' pushed onto the @summary array for $wk=1? Said differently, am I saving a copy of the hash keys and values into the array or am I saving the same reference to the same hash in every element of the array and overwriting the hash values each time with the next week's order totals from %total_opd?

    Thanks.

    RF

    # create data for small summary table of all retailers my $numWks = (($num_days - ( 7 - $firstDOW_curMonth )) / 7 ) + 1; if ( (($num_days - ( 7 - $firstDOW_curMonth )) % 7 ) ) { $numWks++; } my %dayHash; # the hash my @summary; # the array of hashes # init the blank portion of $summary[][] if there is one #my $temp = 0; #while ( $temp < $firstDOW_curMonth ) { # $summary[0][$temp++] #} my $wktotal; my $count=1; # key into %total_opd, it is based on date (1-$numdays) my $firstDOW = $firstDOW_curMonth; # $wk is index for array for ( my $wk = 0; $wk < $numwks; $wk++ ) { $wktotal = 0; # the first time through $dw may be 0, 1, 2,...6 # $dw is the key of the hash for ( my $dw = $firstDOW; $dw < 7; $dw++ ) { $dayHash{$dw} = $total_opd{$count}; $wktotal += $total_opd{$count}; last if ($count++ == $num_days); } $firstDOW = 0; # next time through the $dw loop it will start a +t 0 $dayHash{7} = $wktotal; # save the accumulated total for the we +ek push @summary, $dayHash; # last if ($count > num_days); }

      I strongly recommend that you use strictures (use strict; use warnings;). That would pick up the use of $numwks and $numWks for example.

      It would also alert you to the use of the (presumably) undeclared $dayHash in push @summary, $dayHash; - although I suspect you really intended push @summary, \%dayHash;.

      Strictures would also alert you to the use of num_days in last if ( $count > num_days ); which seems pretty unlikely given you've used $num_days a few lines up

      In answer to your actual question: yes, you will be pushing copies of the reference. The easy fix is to make %dayHash (and $wktotal btw) local to the loop:

      for ( my $wk = 0 ; $wk < $numWks ; $wk++ ) { my $wktotal = 0; my %dayHash; ... }

      I notice that $wk is not used. You could just for (1 .. $numWks) { and if you do need it at some point you should for my $wk (1 .. $numWks) {.


      DWIM is Perl's answer to Gödel
      You should push an anonymous hashref to get a new copy:

      push @summary, { %dayhash };

      Cheers
      Chris

Re^2: pre-size a two-dimensional array
by Anonymous Monk on Jul 16, 2007 at 06:07 UTC
    perl -MData::Dumper -e"$a[1][2][3]=4;print Dumper\@a" $VAR1 = [ undef, [ undef, undef, [ undef, undef, undef, 4 ] ] ];