#!/usr/bin/perl
use strict;
use warnings;

($#ARGV == 2) or die "Usage : $ARGV[0] <file.csv> <1..n> <1..n>";

my $file = $ARGV[0];
my $i    = $ARGV[1];
my $j    = $ARGV[2];
 
open(my $data, '<',$file) or die "Could not open '$file' $!\n";

my $x  = 0;
my $y  = 0;
my $x2 = 0;
my $y2 = 0;
my $xy = 0;
my $n  = 0;
while (my $line = <$data>) {
  chomp $line; 
  my @fields = split("\t",$line);
  #print $#fields."\n";
  $x  += $fields[$i];
  $x2 += $fields[$i]*$fields[$i];
  $y  += $fields[$j];
  $y2 += $fields[$j]*$fields[$j];
  $xy += $fields[$i]*$fields[$j];
  $n  += 1;
}
my $cov  =      ($xy*$n) - ($x*$y);
my $sigx = sqrt(($x2*$n) - ($x*$x));
my $sigy = sqrt(($y2*$n) - ($y*$y));
my $corr = $cov / ($sigx * $sigy);
#print "$i\t$j\t$corr\t"
print "$corr\t"
