I want to write a Perl Script that reads a file.txt with columns of numbers,
20 30 12
31 20 54
63 30 21
11 12 10
and do some calculatio
This shell one-liner multiply first col with second one :
perl -lane 'print $F[0] * $F[1]'
EDIT: And the perl script version with your new requirements and the file with 3 cols :
#!/usr/bin/perl
use strict;
use warnings;
my (@vals, $sum, $med);
while (<>) {
@vals = split;
print "UNSORTED: @vals\n";
#sort data points
@vals = sort(@vals);
print "SORTED: @vals\n"; #test to see if there are an even number of data points
if(@vals % 2 == 0) {
#if even then:
$sum = $vals[(@vals/2)-1] + $vals[(@vals/2)];
$med = $sum/2;
print "The median value is $med\n";
}
else{
#if odd then:
print "The median value is $vals[@vals/2]\n";
}
print "\n";
}
You may understand what's going on instead of just cut & paste ;)
To run the script :
./script.pl file_with_cols.txt