gpt4 book ai didi

perl - 为什么 Perl 的 GD::Graph 提示 "Invalid data set"?

转载 作者:行者123 更新时间:2023-12-04 16:15:57 28 4
gpt4 key购买 nike

我正在为我的作业编写一个 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;

最佳答案

我认为你对 @data 的分配可能是罪魁祸首。

my @data = {@x_axis,@y_axis};

这将创建一个只有一个元素的数组。那一个元素是一个散列。 GD::Graph文档显示您需要一个数组数组。正如 daotoad 所述,这就是 Data::Dumper派上用场。尝试以下操作:

use Data::Dumper;
my @x_axis = 1...100;
my @y_axis = "a"..."z";
my @data = {@x_axis,@y_axis};
warn Dumper(\@data);

您可以看到数据是如何被解释的,并且看到它与 GD::Graph example 不同。 :

@data = ( 
["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"],
[ 1, 2, 5, 6, 3, 1.5, 1, 3, 4],
[ sort { $a <=> $b } (1, 2, 5, 6, 3, 1.5, 1, 3, 4) ]
);

关于perl - 为什么 Perl 的 GD::Graph 提示 "Invalid data set"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/720646/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com