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!
|
|---|