in reply to Re^6: A short whishlist of Perl5 improvements leaping to Perl7
in thread A short whishlist of Perl5 improvements leaping to Perl7
I'm not sure why rtoa has to be in the closure.Just realised it doesn't thanks to the introduction of state variables in perl v5.10.
use v5.10; use strict; use warnings; use List::Util qw(reduce); sub roman_to_dec { state %rtoa = ( M=>1000, D=>500, C=>100, L=>50, X=>10, V=>5, I=>1 ); reduce { $a+$b-$a%$b*2 } map { $rtoa{$_} } split//, uc(shift); } my @testdata = ( "XLII", "LXIX", "mi" ); for my $r (@testdata) { print "$r: ", roman_to_dec($r), "\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^8: A short whishlist of Perl5 improvements leaping to Perl7
by choroba (Cardinal) on Nov 30, 2020 at 16:54 UTC | |
Re^8: A short whishlist of Perl5 improvements leaping to Perl7
by LanX (Saint) on Nov 28, 2020 at 12:12 UTC | |
by eyepopslikeamosquito (Archbishop) on Nov 28, 2020 at 20:33 UTC | |
by LanX (Saint) on Nov 28, 2020 at 23:36 UTC | |
by eyepopslikeamosquito (Archbishop) on Nov 29, 2020 at 01:22 UTC |