0.22929292 0.32928322, 0.528289202 0.19383838, 0.32892929992, 0.78299283839 0.929283893, 0.4829299299, 0.628299292, 0.929389393 . . . #### 0.2939383839, -0.0929288282,0.1293893939, 0.833883929 . . . 250 elements . . . 1000 elements #### open TMP, "datafile.txt" || die ("Could not open data file\n") ; my @input_data = ; close TMP; my $record_count = 0; # Loop through the array for my $element (@input_data) { chomp; @first = split(",", $element ) ; my $jcount = 0; my @result; # Loop through the array again for my $inside_elem (@input_data) { chomp; @second = split(",", $inside_elem) ; # Build only elements below the diagonal last if ( $jcount++ > $record_count ) ; # Covariance logic # No issues with the logic # sum of products of corresponding elements in the arrays $sum = 0; $count = 0; for (@first) { $sum += $_ * $second[$count++] ; } $sum /= scalar(@first) ; push @result, $sum ; } my $str_to_write = join(", ", @result)."\n" ; undef @result ; # Open the output file handler in append mode. open TMP, ">>Outfile.txt" || die ("Could not open output file "); print TMP $str_to_write; close TMP ; }