On 22 Nov 2006 at 17:35, Rob Dixon wrote:

> Beginner wrote:
> > On 22 Nov 2006 at 15:14, Beginner wrote:
> >>
> [snip]
> >>
> >> The number of years can vary so you might get data from 2006->1990.
> >>
> >> The data looks like it is suited to a hash but GD::lines wants the
> >> data passed to in as arrays references ([EMAIL PROTECTED]). That is one 
> >> array
> >> with all the "ancode_n" and foreach code, an array of all the yearly
> >> values. I have managed to get my data into this type of structure
> >>
> >> 'ADV ' => [
> >>    '117216',
> >>    '104776',
> >>     ]
> >> 'BAP ' => [
> >>    '0',
> >>    '270',
> >>    ],
> >>
> >> But I am stuck trying to get it out into n number of arrays that I
> >> can pass to GD. I don't want to pre-declare n number of arrays as the
> >> number may vary and because I am using strict, I don't know how I can
> >> pass the data out of whatever loop I use to get to the values.
> [snip]
> >
> > I managed to do it with this:
> >
> > my (@data,@leg);
> >
> > foreach my $code (keys %values) {
> >         push @leg,$code;
> >         my @temparray;
> >         for (my $i = 0;$i <$no_years;++$i) {
> >                 push @temparray, $values{$code}[$i];
> >         }
> >         my @foo = reverse @temparray;
> >         push @data,[EMAIL PROTECTED];
> > }
> >
> >
> > That's the best golf I can do. Seems a bit verbose but it works.
> > Dp.
> 
> 
> OK that should work, you've got your codes into a list of legends now. There's
> no need for the reverse if you use unshift instead of push to add the values 
> to
> the array. And what are you doing for a list of x-values (the years)?
> 
> I understood you to mean that there may be gaps in the data, which is another
> reason why I said you needed to store hashes of data relating to the years. It
> also looked from your code like the data wasn't very well behaved, as you had
> allowed for a given year being repeated in some cases. If it's always complete
> and in reverse-year order then things are a lot easier. This program keeps 
> your
> original data format and produces the same output as my previous effort.
> 
> Cheers,
> 
> Rob
> 
> 
> 
> use strict;
> use warnings;
> 
> use GD::Graph::lines;
> use CGI qw/:standard/;
> 
> my $q = new CGI;
> 
> my %years;
> my %values;
> 
> foreach my $par ($q->param) {
> 
>   my $val = $q->param($par);
>   $val =~ s/\s+$//;
> 
>   if ($par =~ /(\w{3})_(\d{4})/ ) {
>     my ($ancode, $yr) = ($1, $2);
>     $years{$yr}++;
>     unshift @{$values{$ancode}}, $val;
>   }
> }
> 
> my @ancodes = sort keys %values;
> my $gdata = [
>   [sort keys %years],
>   @[EMAIL PROTECTED],
> ];
> 
> my $graph = GD::Graph::lines->new(600, 400);
> $graph->set_legend(@ancodes);
> my $gd = $graph->plot($gdata);
> 
> open my $fh, '> plot.gif' or die $!;
> binmode $fh;
> print $fh $gd->gif;
> close $fh;
> 

Thanx Rob,

I only noticed the year being out of kilter after I posted. I don't 
think I had the years sort in the post either! I did spot it 
though..honest.

I am going to stick with your first example. It works fine by the way 
and it a nice example of map (which I have to constantly refer back 
to the alpaca book for) and also uses GD:Data, again something I 
struggled with. The version above is, of course, is shorter and dare 
I say it, easier on the eye.

Thanx again.
Dp.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to