Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

(jeffa) Re: Question about using hash indexes with GD::Graph

by jeffa (Bishop)
on Mar 20, 2001 at 20:25 UTC ( [id://65720] : note . print w/replies, xml ) Need Help??


in reply to Question about using hash indexes with GD::Graph

UPDATE: see comments

How about:

my (@h,@c); push @h, $_->{hour} foreach(@{$data}); # no so great push @c, $_->{count} foreach(@{$data}); # see extremely's comment my $new = [ \@h, \@c ]; $gd = $graph->plot($new); # wrong - see orbital's comme +nt
I really tried to avoid the temp lists, maybe someone will come along and make this puppy scream with a nasty map block. ;)

BEFORE: AFTER: $VAR1 = [ $VAR1 = [ { [ 'count' => 5, 1, 'hour' => 1 5, }, 2 { ], 'count' => 2, [ 'hour' => 5 5, }, 2, { 8 'count' => 8, ] 'hour' => 2 ]; } ];
Data::Dumper - don't code anonymous data structures without it.

Jeff

R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--

Replies are listed 'Best First'.
Re: (jeffa) Re: Question about using hash indexes with GD::Graph
by extremely (Priest) on Mar 21, 2001 at 02:43 UTC
    merlyn showed ya map but honestly you shouldn't get into the habit of traipsing through a data set multiple times. When ever you see two lines that both end "foreach (@array)" you should be doing:
    foreach (@data) { push @h, $_->{'hour'}; push @c, $_->{'count'); }

    You get in a hurry and do it wrong then wind up going through the same data like 7 or 8 times when there are 100,000 items in the list and you are going start wondering where your afternoon of surfing PM went. =P

    --
    $you = new YOU;
    honk() if $you->love(perl)

Re: (jeffa) Re: Question about using hash indexes with GD::Graph
by vonman (Acolyte) on Mar 20, 2001 at 21:15 UTC
    This is perfect except that I had to cange the foreach lines to look like follows.
    my (@h,@c); push @h, $_->{'hour'} foreach(@data); push @c, $_->{'count'} foreach(@data); my $new = [ \@h, \@c];
    Now it is graphing correctly
      Any kind of push @x, .... for @y should immediately invoke that magic three-letter word, "m a p".
      my $new = [ [map $_->{hour}, @data], [map $_->{count}, @data] ];

      -- Randal L. Schwartz, Perl hacker

        Man, I was *this* close to getting that. Nice! Thanks once again, Randal!

        Jeff

        R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
        L-L--L-L--L-L--L-L--L-L--L-L--L-L--