buchi2 has asked for the wisdom of the Perl Monks concerning the following question:

Hello!

I have several datasets with different y-values, sometime with different x-values, which I want to give to Chart::Gnuplot::DataSet->new.

In the moment I copy the codeblock 10times and give another ydata/xdata.

More nicefull it will be, if I can do it with a subroutine, if it is possible?

in the moment e.g.:
my $tempmin = Chart::Gnuplot::DataSet->new( xdata => \@yfix, ydata => \@minit, style => "lines", color => "dark-gray", linetype => "dash", width => 1, ); my $tempmax = Chart::Gnuplot::DataSet->new( xdata => \@yfix, ydata => \@maxit, style => "lines", color => "dark-gray", linetype => "dash", width => 1, );

But I have no idea, how a subroutine have to look, where I pass e.g. @x, @y and other plotparameters to?

And as next step a subroitine to which I can give some plotsets and some other parameters, like filename, for creating a picture. And call it again with other parameters for the next picture?

Regards, Buchi

  • Comment on how to put Chart::Gnuplot::DataSet->new and Chart::Gnuplot->new into a subroutine?
  • Download Code

Replies are listed 'Best First'.
Re: how to put Chart::Gnuplot::DataSet->new and Chart::Gnuplot->new into a subroutine?
by choroba (Cardinal) on Jul 25, 2017 at 15:06 UTC
    Just pass the value you want to change as a parameter and pick it up from the @_ array inside the subroutine.
    sub create_chart { my ($ydata) = @_; Chart::Gnuplot::DataSet->new( xdata => \@yfix, ydata => $ydata, style => "lines", color => "dark-gray", linetype => "dash", width => 1, ); } my $tempmin = create_chart(\@minit); my $tempmax = create_chart(\@maxit);

    If you want to change other attributes, too, you might use default values with named attributes:

    sub create_chart { my (%args) = @_; die "No ydata\n" unless exists $args{ydata}; Chart::Gnuplot::DataSet->new( xdata => \@yfix, style => "lines", color => "dark-gray", linetype => "dash", width => 1, %args ); } my $tempmin = create_chart(ydata => \@minit); my $tempmax = create_chart(ydata => \@maxit, color => 'red');

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: how to put Chart::Gnuplot::DataSet->new and Chart::Gnuplot->new into a subroutine?
by thanos1983 (Parson) on Jul 25, 2017 at 15:20 UTC

    Hello again buchi2,

    You can get more information about your question here perlsub/Pass by Reference.

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!