Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Help with a Regex

by Animator (Hermit)
on May 06, 2005 at 12:46 UTC ( [id://454660]=note: print w/replies, xml ) Need Help??


in reply to Help with a Regex

I believe that this code does what you want.

First it creates a string of 7 dots, then it will look for each letter, if it finds it, it sets all charachters after it (in the output string) to dots, and removes all occurence of that charachter from the input string.

my %Lookup = (M => 0, D => 1, C => 2, L => 3, X => 4, V => 5, I => 6); while (<DATA>) { my $out = "." x 7; while (m/([MDCLXVI])/g) { substr ($out, $Lookup{$1}) = "." x (length($out) - $Lookup{$1}); substr ($out, $Lookup{$1}, 1, $1); s/$1//g; } print $out; }

Note, this code is similar to tlm's code, except that his code does not reset the dots, which is what the OP wants... (or atleast I guess that's what he wants by looking at the desired output)

Update: typo + note about tlm's code

Update2: code has a bug... input: 'XL', 'XC' and 'CDXLVI' do not generate the correct output...

Update3: I fail to see the consintency in the OP's desired output.

XL, and XC should result in X, which would mean that any charachter following X (other then V and I) should be ignored, but CDXLVI should result in .D..XVI, meaning that the C is ignored... this makes no sense to me.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://454660]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found