troubleshooting Question

Perl Array vs List Square Brackets GD::Graph Error

Avatar of LauraKornstein
LauraKornstein asked on
5 Comments1 Solution469 ViewsLast Modified:
I am having trouble understanding the difference in perl between an array and a list.  Specifically, I am trying to use the GD::Graph CPAN module to graph some database query results.  The input in the CPAN documentation example appears to submit the data set as a list of arrays.  I am having trouble converting my database output into this format.

I can run the sample code and it is included below along with my own database / dbi code that pulls the results I am trying to graph.

Right now I am getting an "Invalid data set" error.

Any advice would be greatly appreciated, thanks.
use strict;
use warnings;
use GD::Graph;
use GD::Graph::lines;
use DBI;
use Config::IniFiles;
my $uid = "<db_username>";
my $pwd = "<db_password>";
my $svr = "<db_server>";
my $str = "DBI:Sybase:" . $svr;
# Establish database connections
my $db = DBI->connect($str, $uid, $pwd) or die "Cannot log into the DB server.\n";
my $sql = "<tested db query>";
my $st = $db->prepare($sql) or die "Cannot prepare SQL statement.\n";
$st->execute() or die "Cannot execute SQL statement.\n";
my @graphData;
my @dt = [];
my @qty = [];
my @avg = [];
while (@graphData = $st->fetchrow_array()) {
        push(@dt, $graphData[0]);
        push(@qty, $graphData[1]);
        push(@avg, $graphData[2]);
# This array/list works:
my @data = ( 
	["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"],
	[    1,    2,    5,    6,    3,  1.5,    1,     3,     4]
# ... but this produces the error:
#my @data = (@dt, @qty);
my $graph = GD::Graph::lines->new(400, 300);
	x_label           => 'X Label',
	y_label           => 'Y label',
	title             => 'Some simple graph'
) or die $graph->error;
my $gd = $graph->plot(\@data) or die $graph->error;
open(IMG, '>file.gif') or die $!;
binmode IMG;
print IMG $gd->gif;
close IMG;

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros