in reply to sum of integers in a column

You're on the right track, but you're keeping only one total, whereas there are more than one columns. Suddenly I have this nagging feeling that an array to keep the sums would be useful.

A very small hint:

while (my $line = <>) { my @column = split(' ', $line); # $sum[$_] += $column[$i] for 0..$#column; # line corrected by mjscott7202 into: $sum[$_] += $column[$_] for 0..$#column; }

That's two hints, actually. Or three, depending on how you look at it. Anyway, on a side note: you'll also want to use strict; and use warnings;. This will help you catch a lot of errors, typoes, and other mistakes.

Replies are listed 'Best First'.
Re^2: sum of integers in a column
by mjscott2702 (Pilgrim) on Oct 12, 2010 at 12:36 UTC
    Shouldn't that be:  $sum[$_] += $column[$_] for 0..$#column;

    I presume the $i should actually be the default index variable $_ generated by the for loop?

      You're quite right. Initially I wrote it with a for my $i (...) {...} loop but then thought I could do it with a postfix for. Forgot to replace that one instance of $i with $_. Corrected. Thank you :)

        Luckily using strict and warnings would have highlighted the typo :)
        - - for failure to add the pragmas you advocated -- which would have shown you that your correction is still incorrect.