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

Hi guys, I was reading an old thread here about some math calculations: http://www.perlmonks.org/?node_id=954802 An user named Eliya posted a very interesting code using gsl library but in the input he used predetermined values "
my $x = [1,2,3,4,5]; my $y = [5,7,8,12,13];
" How can I make x and y values being numbers from columns of different files? example: x is a column with 1000 values and y another column with 1000 different numbers? Thanks.

Replies are listed 'Best First'.
Re: Changing the input value
by Athanasius (Archbishop) on Apr 04, 2015 at 04:27 UTC

    Hello battingboy, and welcome to the Monastery!

    If we assume that:

    • your x values are contained in the file “x.dat”, which resides in the same directory as the Perl script
    • the values you want are in the 4th column (which is number 3, because we’re counting columns as offsets from the left, so the first column is number 0)
    • all data values are separated by whitespace
    • you want all the values in the column

    then you can extract the x values like this:

    #! perl use strict; use warnings; use Data::Dump; my $file = 'x.dat'; my $column = 3; my $separator = qr{\s+}; # split on whitespace my @values; open(my $fh, '<', $file) or die "Cannot open file '$file' for reading: $!"; while (my $line = <$fh>) { chomp $line; # remove the trailing newline my @cols = split $separator, $line; push @values, $cols[$column]; } close $fh or die "Cannot close file '$file': $!"; dd \@values;

    And of course you will need to repeat this for the y values (which you specify are in a different file).

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,