Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Calculating Row Averages From a CSV File

by Velaki (Chaplain)
on Aug 15, 2007 at 11:52 UTC ( [id://632719]=note: print w/replies, xml ) Need Help??


in reply to Calculating Row Averages From a CSV File

Here's a nifty twist on it.

#!/bin/perl use strict; use warnings; my ( $name, $avg ); while (<DATA>) { my @list; ( $name, @list ) = split /,/; my $sum = 0; $sum += $_ for @list; $avg = $sum / scalar @list; write; } format STDOUT= @<<<< @#.## $name,$avg . __DATA__ AAA01,1.45,0.42,1.54,1.49,1.47,1.36,1.81,0.47,1.8,0.55 ABA05,1.29,1.09,1.13,1.88,1.11,1.44,1.25,1.23,1.05,1.39 BCD06,4.58,4.24,3.87,3.9,4.13,2.04,3.34,7.6,3.58,1.26,7.45 DFG12,26,26.34,24.59,26.46,26.24,26.14,32.35,31.77,31.77 MJK82,8.27,13.23,7.73,8.85,9.15,13.95,0,0,0,0,0 POU45,3.07,3.14,2.97,3.28,21.65,54.23,3.16,3.02,3.26 RTY76,1.22,1.3,1.11,0.92,1.57,1.06,1.01,0.87,0.93 SDH45,15.38,12,22.32,23.3,19.74,46.42,2.06,1.7,2.17 WPL02,13.24,13.23,7.73,8.85,9.15,13.95,0,0,0,0,0

The output you get is:

AAA01 1.24 ABA05 1.29 BCD06 4.18 DFG12 27.96 MJK82 5.56 POU45 10.86 RTY76 1.11 SDH45 16.12 WPL02 6.01

Hope this helped,
-v.

"Perl. There is no substitute."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://632719]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-19 21:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found