2013-09-21 25 views
1

考慮這個例子:perl gnuplot是可以傳遞數組作爲數組?

#!/usr/bin/perl -w 
use strict; 
use Chart::Gnuplot; 

# Initiate the chart object 
my $chart = Chart::Gnuplot->new(
    output => "plotStyle_1.png", 
); 

# A line 
my $lines = Chart::Gnuplot::DataSet->new(
    func => "cos(x)", 
    style => "lines", 
    title => "Plot a line", 
); 

# Points 
my $points = Chart::Gnuplot::DataSet->new(
    func => "sin(x)", 
    style => "points", 
    title => "Plot points", 
); 

# Points on a line 
my $linespoints = Chart::Gnuplot::DataSet->new(
    func => "-atan(x)", 
    style => "linespoints", 
    title => "Plot points on a line", 
); 

# Plot the graph 
$chart->plot2d($lines, $points, $linespoints); 

我想是在plot2d 3個對象(在代碼最後一行)推到一個數組並調用它是這樣的:

$chart->plot2d(@array); 

這是實施plot2d

sub plot2d 
{ 
    my ($self, @dataSet) = @_; 
    &_setChart($self, \@dataSet); 

    my $plotString = join(', ', map {$_->_thaw($self)} @dataSet); 
    open(GPH, ">>$self->{_script}") || confess("Can't write $self->{_script}"); 
    print GPH "\nplot $plotString\n"; 
    close(GPH); 

    # Generate image file 
    &execute($self); 
    return($self); 
} 

* 是否可以使用數組作爲參數調用它? * 請注意,即時消息不是專家在perl我無法理解plot2d的執行情況

回答

2

是的,這是可能的,就這樣做。在您的具體情況下,這將是:

my @datasets; 
push @datasets, $lines; 
push @datasets, $points; 
push @datasets, $linespoints; 

$chart->plot2d(@datasets);