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

Hi Monks, I wondered whether someone could help, i have two arrays @temps and @numbers from which I want to create a graph. The cpan docs for GD::GRAPH::LINES ask you to create an array for the graph like so;
my @data = ( ["31768","25498", "21736", "17138", "9614", "6688", "5016", + "4180", "-2508", "-3762", "-5434", "-10450", "-12122", "-17138", "-2 +0482", "-20900"], [84.44, 68.55, 56.43, 39.71, 26.57, 15.05, 7.11, 1.93, -9.6 +1, -17.56, -22.15, -34.69, -40.96, -48.91, -63.95, -71.04] );
However , I have no idea how to get my arrays in to this format without typing out every value. Just wondered if anyone could help!

Replies are listed 'Best First'.
Re: GD::Graph question
by CombatSquirrel (Hermit) on May 16, 2003 at 11:15 UTC
    If you just need to store two anonymous copies of the two arrays in a new array, you would probably use something like
    my @data = ( [ @temps ], [ @numbers ] );
    This interpolates the arrays into two new anonymous arrays which are then stored in the new array @data.
    If you just want to store references to the original arrays, then you can use
    my @data = ( \@temps, \@numbers );
    This would just link the two existing arrays to the new one.
    Probably you will be able to use either alternative, you just cannot change the contents of the original arrays without changing the data of the new array if you use the second version (similarly, if you change the contents of the orignal arrays, it will not have any effect on the contents of the new array if you use the first version -- it depends on what you want to do).
      Hi monks, I am trying to write a simple cgi application which makes a graph using GD::graph::lines and simply displays it to the screen within the same program. I cant get my code to work (I get a server error if I dont comment out the final line  print $gb->png;. I am modifying a woking non-cgi script but I presumed the general prinicpals were the same... Can anyone help? cheers
      use strict; use CGI ':standard'; use GD::Graph::lines; use CGI::Carp qw(fatalsToBrowser); my $query = new CGI; print header(), start_html( -title=> "curves", -BGCOLOR=>"#ffffff"); my @data = ( ["31768","25498", "21736", "17138", "9614", "6688", "5016", + "4180", "-2508", "-3762", "-5434", "-10450", "-12122", "-17138", "-2 +0482", "-20900"], [84.44, 68.55, 56.43, 39.71, 26.57, 15.05, 7.11, 1.93, -9.6 +1, -17.56, -22.15, -34.69, -40.96, -48.91, -63.95, -71.04] ); my $graph = GD::Graph::lines ->new (600,600); $graph-> set( x_label => 'Temp', y_label => 'Amount', title => 'Tria graph', y_max_value=>100 ) or warn $graph->error; $graph->set (dclrs=> [qw(green red blue)]); my $gb = $graph->plot (\@data) or die $graph->error; print "Content-type: image/png\n\n"; binmode (STDOUT); print $gb->png;
Re: GD::Graph question
by Skeeve (Parson) on May 16, 2003 at 11:04 UTC
Re: GD::Graph question
by Anonymous Monk on May 16, 2003 at 11:06 UTC
    my @data = \( @temps, @numbers ); my $gd = $my_graph->plot(\@data);
    or skip the intermediate step
    my $gd = $my_graph->plot( [ \@temps, \@numbers ] );