A "quick and dirty" way to do this task is to use the perl
Command Switches "-F" to do the input, main loop, and splitting. Use an END block to print the summary. Note that the variable @sums must be a declared as a package variable in order to access it in the END block.
(Anyone know a better way?)
#!perl -F','
use strict;
use warnings;
use feature 'state';
state $header = 1;
our @sums;
if ($header) {
printf "%-24s" . " %4s"x10,
'Number', @F[1..10];
$header = 0;
next;
}
chomp $F[10];
for (reverse 2..10) {
$F[$_] = ( ( $F[$_-1] xor $F[$_] ) ? .5 : 0 );
$sums[$_] += $F[$_];
}
$F[1] = 0;
$sums[1] += $F[1];
printf "%-24s " . " %3.1f "x10 . "\n", @F;
END{
our @sums;
printf '*'x75 . "\n";
printf "%-24s" . " %3.1f "x10 . "\n",
"Sum of weights", @sums[1..10];
printf '*'x75 . "\n";
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.