mobiusinversion has asked for the wisdom of the Perl Monks concerning the following question:
http://perlbuzz.com/2008/03/good-perl-code-is-the-best-form-of-evangelism.html
The article regards the second event in the Microsoft Scriptnet 2008 Winter Games, namely: "to read in a text file of comma-separated values of ice skating scores, drop the highest and lowest scores from each skater, and show the top three skaters by the average of their remaining scores" For example, the first two lines from the actual file is: Janko Cajhen,84,80,61,81,71,62,76I have posted skaters.txt to the web: Note that the text file does not require any fancy escapes for its comma separated values as it might if the ice skaters names were given in the last name, first name format. Or in something even more interesting using double quoted escapes like: "Wojciech, ""the man"" Czupta",89,53,96,81,63,65,85 which would require some fancier unrolling like:use strict; sub sum {my $x; $x += $_ for @_; $x } sub avg {sum(@_)/@_} undef $/; open(my $F, 'skaters.txt'); my @x = map{[split/,/]}grep{/\S/}split/\n+/,<$F>; for(@x){ @$_ = ($_->[0],avg(@{[sort{$a<=>$b}@{$_}[1..$#$_]]}[1..$#$_-2])) } @x = sort{$b->[1] <=> $a->[1]}@x; printf "%-5s %-20s %-20s\n",$_+1,@{$x[$_]} for(0..2);
Do you think Microsoft did that on purpose so as not to expose just how bad their solution would be if the data were in this format? Or perhaps how much better Perl would be than VB Script or Powershell at parsing such data? Ultimately, what would be the most minimalistic and or elegant way to solve the problem given such data? That of the Cookbook?our $csvs = qr/(?:"(?:[^"]|"")*"|[^",])+/; sub csv_file_to_AoA { map{[csv($_)]}grep{/\S/}split/\n/,file_contents($_[0]) } sub csv { my @x = $_[0] =~ /(${csvs})(?:,|$)/g; for(@x){ /^"(.*?)"$/s ? $_ = $1 : undef; $_ =~ s/""/"/g; } @x } sub file_contents { local $/ = undef; open(my$F,$_[0])||die"cant open : $_[0]\n$!\n"; my $x = <$F> }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: winter games, perl, ice and bling
by kyle (Abbot) on Mar 26, 2008 at 21:48 UTC | |
|
Re: winter games, perl, ice and bling
by perrin (Chancellor) on Mar 26, 2008 at 21:52 UTC | |
|
Re: winter games, perl, ice and bling
by mr_mischief (Monsignor) on Mar 26, 2008 at 22:13 UTC | |
|
Re: winter games, perl, ice and bling
by mobiusinversion (Beadle) on Mar 27, 2008 at 00:01 UTC | |
by BrowserUk (Patriarch) on Mar 27, 2008 at 00:27 UTC | |
by kyle (Abbot) on Mar 27, 2008 at 00:16 UTC | |
by perrin (Chancellor) on Mar 27, 2008 at 02:24 UTC | |
by BrowserUk (Patriarch) on Mar 27, 2008 at 05:28 UTC | |
by perrin (Chancellor) on Mar 27, 2008 at 12:33 UTC | |
by BrowserUk (Patriarch) on Mar 27, 2008 at 13:20 UTC | |
| |
|
Re: winter games, perl, ice and bling
by jdporter (Paladin) on Mar 27, 2008 at 03:29 UTC | |
by mr_mischief (Monsignor) on Mar 27, 2008 at 15:27 UTC | |
by jdporter (Paladin) on Mar 27, 2008 at 15:43 UTC | |
by mr_mischief (Monsignor) on Mar 27, 2008 at 16:19 UTC | |
|
Re: winter games, perl, ice and bling
by ysth (Canon) on Mar 27, 2008 at 03:32 UTC |