Welcome to the Monastery PerlMonks

### COnversion problems?

 on Oct 04, 2015 at 18:26 UTC Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i am new to PERL. so i have a lot of difficulties in conversions. i am trying to convert Modern numbers to Roman numbers without using the function ROMAN.. also Trying to convert Octal To Hexadecimal number without any oct or hex function. is it possible?

Replies are listed 'Best First'.
Re: COnversion problems?
by davido (Cardinal) on Oct 04, 2015 at 18:36 UTC

Yes, It's absolutely possible. Both of those functions have source code. It was possible for them to be written in the first place. It is possible for someone willing to figure things out to write similar functionality too.

Show what you've tried and where you're stuck so we can help you past the blockers.

Update: One thing I like to do is to establish in my mind (or on paper) what the rules are. Once you have a firm understanding of the rules for converting to Roman numerals, the rest is just a matter of putting them to code. The same goes for conversions to other bases; understand first how someone would do it without a computer, in 8th grade (give or take a year), and then put that to practice with code.

Dave

Re: COnversion problems?
by Corion (Pope) on Oct 04, 2015 at 20:07 UTC

My suggestion is to do it by hand first. First convert your number from (say) octal to decimal. Then convert your number from decimal to hexadecimal. You will find out that conversion from one base to another base means usually dividing the number by its base repeatedly until the number to convert is zero.

Re: COnversion problems?
by BillKSmith (Prior) on Oct 05, 2015 at 03:15 UTC
Your first problem with "Roman Numerals" is to specify exactly what you mean. There are several issues which you must consider. Lower case letters, in common use today, were not even invented in Roman times. Modern medical practice includes a extension for fractions. Worst of all, the ancient Romans were not consistent in writing certain numbers. (e.g. Even modern clock faces usually display "IIII" for four, but use "IX" for nine.)
Bill
Re: COnversion problems?
by karlgoethebier (Abbot) on Oct 05, 2015 at 11:02 UTC

Stolen from Rosseta Code (no warranty):

```use strict;
use warnings;
use feature qw(say);

my @symbols = (
[ 1000, 'M' ],
[ 900,  'CM' ],
[ 500,  'D' ],
[ 400,  'CD' ],
[ 100,  'C' ],
[ 90,   'XC' ],
[ 50,   'L' ],
[ 40,   'XL' ],
[ 10,   'X' ],
[ 9,    'IX' ],
[ 5,    'V' ],
[ 4,    'IV' ],
[ 1,    'I' ]
);

sub roman {
my ( \$n, \$r ) = ( shift, '' );
( \$r, \$n ) = ( '-', -\$n ) if \$n < 0;
foreach my \$s (@symbols) {
my ( \$arabic, \$roman ) = @\$s;
( \$r, \$n ) = ( \$r .= \$roman x int( \$n / \$arabic ), \$n % \$arabi
+c )
if \$n >= \$arabic;
}
\$r;
}

say roman(\$_) for 1 .. 100;

Regards, Karl

«The Crux of the Biscuit is the Apostrophe»

Re: COnversion problems?
by u65 (Chaplain) on Oct 05, 2015 at 11:51 UTC

But at some point in your Perl quest you need to stop attempting to reinvent the wheel and start using such things as the built-in function oct and the CPAN module Roman.

Re: COnversion problems?
by Anonymous Monk on Oct 05, 2015 at 16:20 UTC
```echo 1944 | perl -pe 'tr/IVXLC/XLCDM/, s/\d/qw(IX VIII VII VI V IV III
+ II I)[9-\$&]/e while /\d/'

hehehe

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1143760]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2021-06-18 22:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What does the "s" stand for in "perls"? (Whence perls)

Results (90 votes). Check out past polls.

Notices?