well... i got some suckie code (just woke up with a terrible hangover... too much free beer ;-) )
but i think i understand it a bit more now... take the input string, look for the highest value (first time it's M, second time it's D), and start looking from that position for the next highest value (skipping of course the previous highest one).

#!/usr/bin/perl use warnings; use strict; my @a = ('M','D','C','L','X','V','I'); my $max = 0; my %a_val=map { $_ => $max++ } split //,'MDCLXVI'; sub search_biggest { my ($array,$model)= @_; my $begin=7; my $pos = 0; my $j; for (my $i = 0; $i < @$array; $i++) { $j = 0; while ($j < @$model) { if ($array->[$i] eq $model->[$j] and $a_val{$array->[$i]} +< $begin){ $begin = $a_val{$array->[$i]}; $pos = $i; $j++; last; } $j++; } } return $pos; } while(my $line = <DATA>){ my ($b,$expect) = split /\s+/, $line; my @b_arr = split(//,$b); my $count = 0; my %val=map { $_ => $count++ } split //, $b; my @results = ('.') x @a; my $searched="0"; my $j = 0; my $pos = search_biggest(\@b_arr, \@a); for (my $i = $pos; $i < @b_arr; $i++) { my $found = 0; while ($j < @a) { last if $b_arr[$i] =~ /$searched/; if ($b_arr[$i] eq $a[$j]){ $found = 1; $results[$j]=$a[$j]; $searched .= $a[$j]; } $j++; if($found and $i < @b_arr){ my @new = ( $i + 1 < @b_arr ? @b_arr[ $i +1 .. $#b_a +rr ] : ($b_arr[$#b_arr]) ); my @new2 = @a; $i = search_biggest(\@new,\@new2) if not @new = 1; last; } } $j = 0 unless $found; } my $result = join "",@results; printf "%7s => %7s ( %7s : %-3s)\n", $b, $expect, $result, $result eq $expect ? "Ok" : "Not Ok"; } print "\n"; __DATA__ I ......I IV .....V. V .....V. VI .....VI IX ....X.. X ....X.. XI ....X.I XIV ....XV. XV ....XV. XVI ....XVI XIX ....X.I X ....X.. XL ....X.. LX ...LX.. XC ....X.. CLXIX ..CLX.I CDXLVI .D..XVI MCMXCVI M.C.XVI MDCLI MDCL..I
for the suckie code, here's the output:
I => ......I ( ......I : Ok ) IV => .....V. ( .....V. : Ok ) V => .....V. ( .....V. : Ok ) VI => .....VI ( .....VI : Ok ) IX => ....X.. ( ....X.. : Ok ) X => ....X.. ( ....X.. : Ok ) XI => ....X.I ( ....X.I : Ok ) XIV => ....XV. ( ....X.I : Not Ok) XV => ....XV. ( ....XV. : Ok ) XVI => ....XVI ( ....XVI : Ok ) XIX => ....X.I ( ....X.I : Ok ) X => ....X.. ( ....X.. : Ok ) XL => ....X.. ( ...L... : Not Ok) LX => ...LX.. ( ...LX.. : Ok ) XC => ....X.. ( ..C.... : Not Ok) CLXIX => ..CLX.I ( ..CLX.I : Ok ) CDXLVI => .D..XVI ( .D..XVI : Ok ) MCMXCVI => M.C.XVI ( M.C.XVI : Ok ) MDCLI => MDCL..I ( MDCL..I : Ok )
I think i know what to do, but my code is not really working as I expected.. or maybe I should wait till my hangover is ... over ;-)

to ask a question is a moment of shame
to remain ignorant is a lifelong shame


In reply to Re^3: Help with a Regex by insaniac
in thread Help with a Regex by planetscape

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.