On 22 Nov 2006 at 15:14, Beginner wrote:
> "ancode_1" = "ADV "
>       "ADV_2006" = "117216 "
>       "ADV_2005" = "104776 "
> "ancode_2" = "BAP "
>       "BAP_2006" = "0 "
>       "BAP_2005" = "270 "
> "ancode_3" = "BOO "
>       "BOO_2006" = "746854 "
>       "BOO_2005" = "673151 "
> "ancode_4" = "BUS "
>       "BUS_2006" = "0 "
>       "BUS_2005" = "2476 "
> "ancode_5" = "COM "
>       "COM_2006" = "87787 "
>       "COM_2005" = "97009 "
> 
> 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.
> 
> my $gd = GD::Graph::lines->new(600,400);
> my @data = ([EMAIL PROTECTED],[EMAIL PROTECTED], a number of arrays equal to 
> size of 
> @ancodes);
> 
> This is the bit of code I use to generate the hash of arrays
> 
> use GD::Graph::hbars;
> use GD::Graph::lines;
> use GD::Graph::Data;
> use CGI qw/:standard/;
> use Data::Dumper;
> use strict;
> use warnings;
> 
> my $q = new CGI;
> 
> my @par_names = $q->param;
> my (@cl_type,@values,@years,);
> my %yrs;
> 
> my $current_code;
> for (my $i = 0; $i < $#par_names; ++$i) {
>         if ($par_names[$i] =~ /ancode_\d+/) {
>                 $current_code = $q->param($par_names[$i]);
>                 push(@cl_type,$q->param($par_names[$i]) );
>         }
>         if ($par_names[$i] =~ /\w{3}_\d{4}/) {
>                 (my $y) = ($par_names[$i] =~ /\w{3}_(\d{4})/);
>                 if (! exists($yrs{$y}) ) {    # only accept yrs not 
> seen.
>                         $yrs{$y} = 0;
>                 }
>                 my $val = $q->param($par_names[$i]);
>                 (my $value = $val) =~ s/\s+$//;
>                 push @{$values{$current_code}},$value;
>         }
>  }
> 
> Can someone offer me some pointers here? I am struggling to do this 
> and I expect there is a short-hand way to achieve what I want.
> 

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.


-- 
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