On Wed, Oct 03, 2001 at 03:12:55PM -0600, Tyler Cruickshank wrote:
> I have a hash that I am storing arrays in.  Each array corresponds to one
> hour of a day.  After the hash has been populated I need to cycle through
> the "days" and sum them up.  So, I have written my hash such that each key
> can be looped thru via a for loop.

Given this, I would expect something along the lines of:

    my %hr_hash = (
        'data.hr1'  =>  [1, 4, 10, 20],
        'data.hr2'  =>  [17, 9, 8],
        'data.hr3'  =>  [5, 8, 2, 3],
    );


    while (my($hr_key, $days) = each(%hr_hash)) {
        $sum{$hr_key} = 0;

        foreach my $day (@$days) {
            $sum{$hr_key} += $day;
        }
    }

This results in %sum, the keys of which are the keys in %hr_hash, and the
values the sum of days.

That's what I would expect, but your code doesn't quite do that.


> for(local $b=0; $b<=23; $b++){ 

Are you from a C background?  :)

Where did 23 come from?


>     
>    local $hour = $b+1;
>    local $root = 'data.hr';
>    local $name = "$root$hour";

You shouldn't be using local this much; local is largely obsolete, except
for a very few cases; use my.

 
>    for(local $i=0; $i<=1847; $i++){ 

Where did 1847 come from?


>       $specSum{$name}[$i] = $specSum{$name}[$i] + $hrHash{$name}[$i];

I believe this is your problem.  You just created a mirror of %hrHash in
%specSum.  I don't believe you wanted %specSum to be a hash of arrays, but a
hash of sums.

 
>       } 
> 
>     } 

Hopefully that'll help.

Finally, why is %hrHash a hash?  You seem to have keys that are all the
same, the only difference being the integer on the end, and the integers are
sequential.  To me this screams array, not hash.


Michael
--
Administrator                      www.shoebox.net
Programmer, System Administrator   www.gallanttech.com
--

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to