Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Selecting successive lines

by davido (Cardinal)
on Sep 17, 2013 at 04:35 UTC ( [id://1054385]=note: print w/replies, xml ) Need Help??


in reply to Selecting successive lines

I would read each record complete rather than one line at a time, and to keep it simple, would push scores into an anonymous array per student. Then just iterate over each student and do the math.

use List::Util qw( sum ); my %student; local $/ = ''; while( <DATA> ) { push @{$student{$1}}, $2 and next if m/\A([^\n]+).+?(\d+)\n?\Z/s; warn "Invalid record #$.: <<$_>>\n"; } while( my( $name, $scores ) = each %student ) { print "$name: Average ", sum( @{$scores} ) / @$scores, "\n"; } __DATA__ Jack Student ID - 12445 Math Score - 45 Jill Student ID - 234254 Math Score - 90 Jack Student ID -12445 Math Score2 - 33 Jill Student ID - 234254 Math Score2 - 10

Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (2)
As of 2024-04-25 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found