in reply to winter games, perl, ice and bling
You've reimplemented List::Util::sum.
You open without checking whether it succeeded.
open my $input_fh, '<', 'skaters.txt' or die "Can't read skaters.txt: $!";
I see no reason to undef $/ and then split on /\n+/
my @skater_records = map { chomp; [ split /,/ ] } grep { /\S/ } <$input_fh>;
I'm not going to unravel the the rest. The whole thing reads like an obfuscation or attempt at golfing. This is a simple problem, and the solution is not difficult, but you've made it hard to read. You may have found that entertaining (and I might have as well), but it.doesn't have much more to recommend it.
Update with a solution.
use strict; use warnings; use List::Util qw( sum ); my $skaters_file = shift @ARGV || 'skaters.txt'; my @top_three; open my $skaters_fh, '<', $skaters_file or die "Can't read skaters file '$skaters_file': $!"; while ( my $csv_line = <$skaters_fh> ) { chomp $csv_line; my ( $name, @scores ) = split /,/, $csv_line; # remove highest and lowest score @scores = sort { $a <=> $b } @scores; shift @scores; pop @scores; my $mean_score = sum( @scores ) / scalar @scores; push @top_three, { name => $name, score => $mean_score }; @top_three = reverse sort { $a->{score} <=> $b->{score} } @top_thr +ee; @top_three = @top_three[ 0 .. 2 ] if 3 < scalar @top_three; } close $skaters_fh or die "close failed: $!"; foreach my $skater ( @top_three ) { print "$skater->{name}: $skater->{score}\n"; } __END__ Guido Chuffart: 88.2 Jack Creasey: 85.8 Cecilia Cornejo: 85.4
It's not super efficient, but it doesn't read every record into memory (it just retains the top three), so it can handle any number of records without exhausting RAM. Nothing too tricky, I don't think.
|
|---|