HSchmale
HSchmale

Reputation: 1929

Creating an array of arrays for GD::Graph

All right, I'm trying to make an array of arrays in Perl for use with the GD::Graph module. And I can't figure out why the following is not valid array of arrays for GD::Graph.

my @t = (1, 2, 3, 4);
my @g = (2, 4, 5, 6);
my @data = (@t, @g);

I've also tried constructing the data like below, and it still does not like it.

my @data;
push @data, @t;
push @data, @g;

I want to keep my values in seperate arrays and just combine them for use with GD::Graph, because that is what I've found to be the easiest way, even if it is ugly. How would I go about creating a valid structure for use with GD::Graph, that is created on the fly?

It complains about it here.

my $gd = $graph->plot(\@data) or die $graph->error;

Upvotes: 0

Views: 324

Answers (2)

taiko
taiko

Reputation: 448

For me using array references did the trick

my @t = (1, 2, 3, 4);
my @g = (2, 4, 5, 6);
my @data = (\@t, \@g);

and the plot the chart with fro example:

my $graph = new GD::Graph::lines(800,600 );
my $gd = $graph->plot( \@data );
open OUT, ">","whatever.png" or die "Couldn't open for output: $!";
binmode(OUT);
print OUT $gd->png( );
close OUT;

Upvotes: 0

Kim Ryan
Kim Ryan

Reputation: 515

Looks like @data is just a single dimension array with 8 elements.

You can define array of arrays by using the anonymous array constructor []

my @data = (
    [1, 2, 3, 4],
    [2, 4, 5, 6]
);

Upvotes: 1

Related Questions