I am writing a small program in Perl for my assignment and I am new to Perl.
Code that I have written provides me with exactly the same values I need, but I am getting this error while creating bar chart.
Invalid data set: 0 at line 67
Line 67 is marked with a comment in the code below.
The values stored in x-axis are:
40 44 48 52 64 76 83 104 105 148 149 249 431 665 805 1420 1500
And y_axis are:
16 1 1 6 1 1 1 1 1 1 1 1 1 1 1 2 5
Here’s my code:
use GD::Graph::bars; open(CHECKBOOK,'c:\\Perl\\bin\\ip_packet_trace1.txt'); my $counter = -1; my @sizearray = {}; while ($record = <CHECKBOOK>) { @array = split(/\t/,$record); $counter++; $sizearray[$counter] = $array[6]; } $counter++; my @array1 = sort {$a <=> $b} @sizearray; print '$counter\n'; print '@array1\n'; my @freq = {0...0}; foreach $elem (@array1){ my $s = $freq[$elem]+1; $freq[$elem] = $s; } my $size = @freq; my @x_axis = {}; my @y_axis = {}; my $count2 = -1; for($i = 1; $i < $size; $i++){ my $elem = $freq[$i]; if($elem and $elem > 0 ){ $count2++; $x_axis[$count2] = $i; $y_axis[$count2] = $elem; } } print '@x_axis \n'; print '@y_axis \n'; my $mygraph = GD::Graph::bars->new(500, 300); # line 67 $mygraph->set(x_label => 'Month', y_label => 'Number of Hits', title => 'Number of Hits in Each Month in 2002', ) or warn $mygraph->error; my @data = {@x_axis,@y_axis}; my $myimage = $mygraph->plot(\@data) or die $mygraph->error; open(IMG, '>C:\\image\\file.gif') or die $!; binmode IMG; print IMG $myimage->gif; close IMG;
K. I tested and altered you code. The below code works. The array part that everyone mentioned was important, but not your only problem. The example in cpan, was of an anonymous array, so instead of passing @data 2 arrays, you just needed to pass 2 references to @data.