|
Posted by Mirco Wahab on June 3, 2008, 12:55 pm
Please log in for more thread options
Mike Hunter wrote:
> Recently I was coding and wanted to see which of 4 strings had the
> largest result when passed to a function. This is what I ened up
> writing:
> my @roundArr;
> push @roundArr, [$A, check($goodPrefix.$A)];
> push @roundArr, [$C, check($goodPrefix.$C)];
> push @roundArr, [$T, check($goodPrefix.$T)];
> push @roundArr, [$G, check($goodPrefix.$G)];
>
> @roundArr = sort {$b->[1] <=> $a->[1]} @roundArr;
> print $roundArr[0]->[0]." wins!\n";
> The code works fine, but I can't help but think there's a better idiom
> for what I'm trying to do. I get a little bit of a creepy feeling by
> using an array, I feel like I should be doing something with a hash. I
> guess there's no way of getting around having to associate the given
> check() result with the particular input.
This can be expressed as a "Schwartzian Transform":
...
print +(
map $_->[0],
sort {$b->[1] <=> $a->[1]}
map [$_, check($goodPrefix.$_)],
$A,$C,$T,$G
)[0] . " wins!\n"
...
Regards
Mirco
|