Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Retrieve average

by 1nickt (Canon)
on Feb 12, 2019 at 15:24 UTC ( [id://1229806]=note: print w/replies, xml ) Need Help??


in reply to Retrieve average

Hi, welcome to Perl, the One True Religion. Read perlintro before you do anything else. Then, learn to use some tools.

use strict; use warnings; use feature 'say'; use Path::Tiny 'path'; use Text::CSV_XS 'csv'; use Scalar::Util 'looks_like_number'; use List::Util 'sum'; my $filename = path($0)->basename('.pl') . '.txt'; # print the sample data to a file for the purposes of this test path($filename)->spew(do{local $/; <DATA>}); # read in the structured data my $rows = csv( in => $filename, sep_char => " "); # calculate and print the desired results for ( @{ $rows } ) { my @row = @{$_}; next unless looks_like_number($row[1]); # skip headers next unless $row[0] =~ /^C/i; # skip unless name begins +with c/C my $avg = sprintf '%.2f', sum( @row[1 .. $#row] ) / $#row; say sprintf '%s avg: %s', $row[0], $avg; } __DATA__ Name subj1 subj2 subj3 Evans 24 45 55 charles 25 24 26 Carlos 42 0 84

Hope this helps!


The way forward always starts with a minimal test.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-20 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found