in reply to Re: pre-size a two-dimensional array
in thread pre-size a two-dimensional array

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

Replies are listed 'Best First'.
Re^3: pre-size a two-dimensional array
by GrandFather (Saint) on Jul 12, 2007 at 04:23 UTC

    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
Re^3: pre-size a two-dimensional array
by chrism01 (Friar) on Jul 12, 2007 at 04:16 UTC
    You should push an anonymous hashref to get a new copy:

    push @summary, { %dayhash };

    Cheers
    Chris