为什么 Perl 的 GD::Graph 抱怨“无效数据集”?
我正在为我的作业用 Perl 编写一个小程序,而且我是 Perl 新手。
我编写的代码为我提供了与我需要的完全相同的值,但在创建条形图时出现此错误。
Invalid data set: 0 at line 67
下面的代码中第 67 行被注释了。
存储在 x 轴中的值是:
40 44 48 52 64 76 83 104 105 148 149 249 431 665 805 1420 1500
y_axis 是:
16 1 1 6 1 1 1 1 1 1 1 1 1 1 1 2 5
这是我的代码:
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为您对
@data
的分配可能是罪魁祸首。这将创建一个包含一个元素的数组。 该元素是一个散列。 GD::Graph 文档显示您需要一组数组。 正如
daotoad
所说, Data::Dumper 派上用场了。 尝试以下操作:您可以查看数据的解释方式,并发现它与 GD::图示例:
I think your assignment of
@data
is probably to blame.This creates an array with one element. That one element is a hash. The GD::Graph documentation shows that you need an array of arrays. This is where, as
daotoad
stated, Data::Dumper comes in handy. Try out the following:You can see how the data is being interpreted, and see that it is not the same as the GD::Graph example:
请
使用严格
并使用警告
。 如果您使用这些编译指示,您在这段代码中遇到的许多麻烦的事情都会被标记出来。您还花费了大量的精力来附加到数组的末尾。 您可以使用
push
来执行此操作,而无需知道最后一项的索引。 像这样使用push可以让你的代码变得相当简单。使用
()
创建一个空数组(实际上是列表)。 使用[]
进行数组引用。 使用{}
进行哈希引用。 您已经在很多地方使用了哈希引用。最好使用词法文件句柄而不是全局文件句柄。 使用全局文件句柄就是使用不必要的全局变量,这是自找麻烦。 另请检查对
open
的调用是否成功。当您使用数据结构时,Data::Dumper 是一个有用的模块,可以查看正在发生的情况。
另外,请查看
perldsc
和perlreftut
,它们提供了有关如何使用引用和嵌套数据结构的很好的示例。Please
use strict
anduse warnings
. Many of the troublesome things you have going on in this code will be flagged for you if you use these pragmas.You are also expending a lot of effort to append to the end of your arrays. You can use
push
to do this without knowing the index of the last item. Using push like this will let you simplify your code quite a bit.Use
()
to make an empty array (well list really). Use[]
to make an array reference. use{}
to make a hash reference. You've been using hash refs in a number of places.It is also best to use lexical filehandles instead of global filehandles. Using global file handles is using unnecessary global variables, which is asking for trouble. Also check for success on your calls to
open
.When you are working with data structures, Data::Dumper is a useful module to see what's going on.
Also, take a look at
perldsc
andperlreftut
they have good examples of how to work with references and nested data structures.K. 我测试并修改了你的代码。 下面的代码有效。 每个人都提到的数组部分很重要,但不是你唯一的问题。 cpan 中的示例是一个匿名数组,因此您只需传递 2 个对 @data 的引用,而不是传递 @data 2 个数组。
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.
我不太确定我在看什么,但有一件事突然引起我的注意:您正在将数组初始化为哈希引用:
如果您想要的只是一个空数组,您可以简单地说:
如果您想使清除它是新的和空的,你需要括号; 见下文。 (然而,正如布拉德在他的评论中所说,这是多余的。您可能应该习惯查看和编写更简单的版本。)
数组存储有序列表,列表放在括号中。 有关更多信息,请参阅
perldoc perldata
。I'm not really sure what I'm looking at, but one thing jumps out at me: you are initializing your arrays as hash references:
If all you want is an empty array, you can say simply this:
If you want to make clear it's new and empty, you want parentheses; see below. (As Brad says in his comment, however, this is redundant. You should probably get used to seeing and writing the simpler version.)
Arrays store ordered lists and lists go in parentheses. See
perldoc perldata
for more.