Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Help with storing data in a hash

by hallikpapa (Scribe)
on Jan 28, 2008 at 22:19 UTC ( [id://664795]=perlquestion: print w/replies, xml ) Need Help??

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

I have multiple procedures that need to dump data into a hash (Well it's not necessarily a hash, but I would still like unqiue entries stored this way.). There is a range of dates stored in an array, and looping thru the array I do this:
%calls = &call_type( $d, $d ); sub call_type { #FIELD NAMES (name, calltype, calls, minutes, revenue, cost, margin) my ( $name, $calltype, $calls, $minutes, $revenue, $cost, $margin, + %calls ); my ( $day_start, $day_end ) = @_; my $sp = $msdbh->prepare( qq|usp_Dashboard_CallType \@start = '$day_start', \@end = '$da +y_end'|); $sp->execute or die( $sp->errstr ); while ( ( $name, $calltype, $calls, $minutes, $revenue, $cost, $ma +rgin ) = $sp->fetchrow() ) { $calls{ $day_start . ":" . $name } = [ "$calltype", "$calls", "$minutes", "$revenue", "$cost", "$ +margin" ]; } return %calls; }
So there should be 40 unique entries. 4 entries per day, 10 days. But right after I loop thru all of the days, I do a print Dumper \%calls; And get this
$VAR1 = { '20080110:Dedicated' => [ '1', '4722194', '12730293.0000', '65169.0000', '38247.000000', '41.311000' ], '20080110:Canada' => [ '4', '7898', '49191.0000', '168.0000', '248.000000', '-47.619000' ], '20080110:Directory' => [ '8', '18', '29.0000', '7.0000', '.000000', '100.000000' ], '20080110:Toll Free' => [ '5', '91114', '140912.0000', '-116.0000', '-703.000000', '-506.034400' ] };
Why is there only the last day/entry? thank you.

Replies are listed 'Best First'.
Re: Help with storing data in a hash
by samtregar (Abbot) on Jan 28, 2008 at 22:24 UTC
    %calls = &call_type( $d, $d );

    There's your problem - you're only getting 1 day's worth of data because you're passing the same date as the start and end. Maybe you meant:

    %calls = &call_type( $d, $d + 10 );

    Not sure how your dates are stored though - that only works if they're epoch times or objects that know how to add.

    If you mean you're calling call_type() multiple times and expecting the results to accumulate in %calls, you may want something more like:

    %calls = (%calls, &call_type( $d, $d ));

    -sam

      Well here's the dates:
      my @dates = ( '20080101', '20080102', '20080103', '20080104', '20080105', '20080 +106', '20080107', '20080108', '20080109', '20080110' );
      And for this specific procedure, it only wants one day. way the store procedure in the db is.
      foreach my $d (@dates) { %calls = &call_type( $d, $d ); } print Dumper \%calls;
      I followed your second suggestion and that worked. I appreciate the help.
        Aha - that's the important part! You're overwriting %calls each time you call that function. I added an example you can use to my answer above. A better solution would be to make call_type() accept a reference to %calls and add entries to it.

        -sam

        Hi hallikpapa

        You simply declare %calls in outside of the subroutine call_type like

        my %calls; foreach my $d (@dates) { %calls = &call_type( $d, $d ); }

        Punitha

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://664795]
Approved by samtregar
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2024-04-23 12:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found