couple general suggestions: Hmm .. actually, it might be your split -- try split(' ') instead -- see the split() docs for full info, including that "split(/ /)" will give you as many null initial fields as there are leading spaces. so it could be that your data file has leading spaces in it somewhere.

Here's a little refactoring example, too, to demo several more "perlish" constructs:
#!/usr/bin/perl -w use strict; my @sum; my @sumsq; my $n = 0; while( <DATA> ){ my @fields = split / /; foreach my $i ( 0..$#fields ){ $sum[$i] += $fields[$i]; $sumsq[$i] += $fields[$i]**2; } $n++; } $_ /= $n for @sum, @sumsq; my @stddev = map { sqrt( $sumsq[$_] - $sum[$_]**2 ) } 0 .. $#sum; print join(" ", @stddev) . "\n"; #foreach my $i ( 0 .. $#sum ){ # printf "%5s %10s %10s\n", $i, $sum[$i], $stddev[$i]; #} __DATA__ 1.8 2.5 3.8 1.9 -3.5 -3.5 3.2 -3.9 4.2 4.5 2.8 -1.3 -0.9 -0.7 -0.4 -0.8 -3.5 -3.5 -1.6 -4.5

In reply to Re: to calculate mean and variance by davidrw
in thread to calculate mean and variance by cdfd123

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.