Hi there
I have a text file that looks like the following
0 48.23 17.90 48.23 0 49.58 17.90 49.58 0 59.62 52.04 65.80 62.20 56.02 68.82 35.37 37.87 36.52 27.33 50.73 31.85
etc ...
What I would like to do is take these values and calculate the average. The calculation is obviously trivial but I'm struggling somewhat with extracting the values due to the format (take into account that the actual files I am working on have many more than three columns per row and that these files all have a different number of columns (so one file may have 50 columns per row, another 35 columns per row and so on).
This is what I have come up with so far
#!/usr/bin/perl use strict; my @line; # shall we get the file then? my $inputfile = $ARGV[0]; # guess we need to open it too open(INPUT, "$inputfile") || die "Error: Can't open $inputfile for reading: $!\n"; # lets place contents of file into array my @filecontents = <INPUT>; # we now want a for loop to access the array for(my $i = 0; $i < @filecontents; $i++) { # place each line in file # into another array @line = split(/\,/, $filecontents[$i]); }
My problem is I'm not quite sure how to go from here in order to extract one value at a time from each row so I can add all values together when calculating my average.
Any help/suggestions much appreciated.

In reply to extracting and using values from a matrix file by Angharad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.