Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Selecting successive lines

by frozenwithjoy (Priest)
on Sep 17, 2013 at 03:52 UTC ( #1054381=note: print w/replies, xml ) Need Help??

in reply to Selecting successive lines

If I were going to do this and wanted to have it expandable, I'd probably plan to make a hash like this (you can simplify it if you don't care about keeping track of student IDs and never have students with the same names):
my %records = ( 12445 => { name => 'Jack', scores => [ 45, 10 ], }, 234254 => { name => 'Jill', scores => [ 45, 10 ], }, );

If the data are consistently in the format shown, the following will make the hash (it can accept scores w/ decimals). It then calculates and reports the mean score.

#!/usr/bin/env perl use strict; use warnings; use feature 'say'; use List::Util 'sum'; my %records; while ( my $name = <DATA> ) { my ($id) = <DATA> =~ /(\d+)$/; my ($score) = <DATA> =~ /(\d+\.?\d*)$/; <DATA>; chomp( $name, $id, $score ); $records{$id}{name} = $name; push @{ $records{$id}{scores} }, $score; } for ( sort { $a <=> $b } keys %records ) { my $name = $records{$_}{name}; my @scores = @{ $records{$_}{scores} }; my $mean = sum(@scores) / @scores; say "$name ($_) has an average score of $mean"; } __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


Jack (12445) has an average score of 39 Jill (234254) has an average score of 50

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1054381]
[atcroft]: Lady_Aleena: I know that they used the L<text|url> syntax to link to them, but I was wondering if there was a more "standard" way (for instance, if the L<module_name> syntax might work better).
[Lady_Aleena]: atcroft, I"m trying to think of a module I've used that linked to another module.
[Lady_Aleena]: atcroft, I just put a link to another module in one of my local ones. For a normal link L<text|url> the text would be written out then the url would follow the text in brackets <>. However, when I linked to another module, the text was written ..
[Lady_Aleena]: ... but no link. L<module name> might localize to where ever it is, like PerlMonks [id://1026423] does. (Cookies)

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2017-05-27 04:14 GMT
Find Nodes?
    Voting Booth?