首页 > 解决方案 > perl:绘制来自多个数组的数据

问题描述

在我的 perl 代码中,我有几个名为 'a'、'b'、'c'.... 'e'、'f' 的数组。我在调用“MyFunc”时将它们作为参数发送。在那里我想绘制任何两个数组,例如,'e' vs 'f'。

我尝试了以下方式(请查看代码),但我收到一条消息,指出my $gd = $graph->plot(\@e,\@f) or die $graph->error;正在执行命令的行中没有可用的数据点。

如何让它发挥作用?

MyFunc(
    'a' => [0, 1,2,3,4],
    'c' => [0, -1,2,-3,6],  
    'c' => [0, 2,4,2,5],
    'd' => [0, 1,2,3,4],
    'e' => [0, 9,2,1,7],
    'f' => [-2, 5,-1,1,7],
    'g' => [5, 1,8,-2,5],
);

sub MyFunc {

use GD::Graph::lines;
my $graph = GD::Graph::lines->new;

$graph->set( 
    x_label           => 'X Label',
    y_label           => 'Y label',
    title             => 'Some simple graph',
    y_max_value       => 8,
    y_tick_number     => 8,
    y_label_skip      => 2 
) or die $graph->error;

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

open(IMG, '>file.gif') or die $!;
binmode IMG;
print IMG $gd->gif;
close IMG;


};

标签: arraysperlplot

解决方案


将参数传递'e' => [0,9,2,1,7]给子例程不会自动创建@e在子例程内调用的变量。您的子例程不会对任何参数进行任何处理。考虑这样的事情来做你想做的事:

sub MyFunc {

    my %params = @_;
    ...
    my $gd = $graph->plot( [$params{"e"}, $params{"f"}] ) ...
    ...
}

推荐阅读