Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

symbolic increment of roman numerals

by PetaMem (Priest)
on May 03, 2002 at 15:57 UTC ( [id://163827]=perlcraft: print w/replies, xml ) Need Help??

   1:               my $str = uc shift @_;
   2: 
   3: 
   4: 	      if($str =~ s/XLVIII$/IL/) {
   5: 	      } elsif($str =~ s/VIII$/IX/) {
   6: 	      } elsif($str =~ s/III$/IV/) {
   7: 	      } elsif($str =~ s/DCCCXCIX$/CM/) {
   8: 	      } elsif($str =~ s/CCCXCIX$/CD/) {
   9: 		  
  10: 	      } elsif($str =~ s/LXXXIX$/XC/) {
  11: 	      } elsif($str =~ s/XXXIX$/XL/) {
  12: 		  
  13: 		  
  14: 	      } elsif($str =~ s/(I{1,2})$/$1I/) {
  15: 		  
  16: 	      } elsif($str =~ s/CDXCIX$/D/) {
  17: 	      } elsif($str =~ s/CMXCIX$/M/) {
  18: 	      } elsif($str =~ s/XCIX$/C/) {
  19: 		  
  20: 	      } elsif($str =~ s/I([VXLCDM])$/$1/) {
  21: 	      } elsif($str =~ s/([VXLCDM])$/$1I/) {
  22: 	      }
  23: 	      
  24: 	      return $str;

Replies are listed 'Best First'.
Re: symbolic increment of roman numerals
by thelenm (Vicar) on May 03, 2002 at 16:12 UTC
    ++Fatvamp, cool code! With a long string of empty-block elsifs, you could also write the code using or or ||, like the following. Good work.
    my $str = uc shift @_; $str =~ s/XLVIII$/IL/ or $str =~ s/VIII$/IX/ or $str =~ s/III$/IV/ or $str =~ s/DCCCXCIX$/CM/ or $str =~ s/CCCXCIX$/CD/ or $str =~ s/LXXXIX$/XC/ or $str =~ s/XXXIX$/XL/ or $str =~ s/(I{1,2})$/$1I/ or $str =~ s/CDXCIX$/D/ or $str =~ s/CMXCIX$/M/ or $str =~ s/XCIX$/C/ or $str =~ s/I([VXLCDM])$/$1/ or $str =~ s/([VXLCDM])$/$1I/; return $str;
Re: symbolic increment of roman numerals
by Fastolfe (Vicar) on May 04, 2002 at 16:04 UTC
    For those interested in doing more "real" roman numeral operations, Math::Roman handles much of this for you:
    use Math::Roman 'roman'; my $y = roman 'MCMLXXVII'; print "$y\n" while $y++ < 2002;
      Hi,

      I contacted the author of the Math::Roman module and reported some bugs, so the 1.07 should be usable now. The big difference in the approach, is the symbolic operation and not internal transformations to/from another nuber system/scheme.

      Try to increment the 'IM' number with the Math::Roman module...

      BTW: The symbolic approach lets you (though expensive operation) guess what roman number xy probably is, though its in a wrong format.

      Most probably we'll merge the best of both worlds in a later Math::Roman version.

      Bye
       PetaMem

Re: symbolic increment of roman numerals
by belg4mit (Prior) on May 03, 2002 at 21:00 UTC
    Seems like it would make an interesting addition (Ha!) to on of the Roman numeral modules. Add subtraction of course ;-).

    --
    perl -pew "s/\b;([mnst])/'$1/g"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-03-29 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found