Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Characters must match both the class AND the position. I want to ignore characters that are "out of order" (the "I" in "IV") but allow subsequent matches of characters that are in order (the "V" in "IV"). I want to make sure a period (".") replaces any character that doesn't match both the character and its designated position.

As far as I can tell your desired output does not match the above description. The expected output for "XIV" is "....XV." and the expected output for "XIX" is "....X.I" I dont see how these two interpretations are compatible with each other. A few other cases are also unclear, notably the one that says "CDXLVI" should result in ".D..XVI", which I can't understand at all. Anyway, I wrote two solutions, one parsing from the front and one from the back, neither gets all your output cases correct, which makes me suspect your output expectations are incorrect.

use strict; use warnings; my $max=0; my %val=map { $_ => $max++ } split //,'MDCLXVI'; sub rtol { my ($word)=@_; my @r = (".") x $max; my $last = $max + 1; while (length $word) { my $c=chop $word; if ($val{$c}<$last) { $r[$val{$c}]=$c; $last=$val{$c}; } } return join "",@r; } sub ltor { my ($word)=@_; my @r = (".") x $max; my $last = -1; for my $c (split //,$word) { if ($val{$c}>$last) { $r[$val{$c}]=$c; $last=$val{$c}; } } return join "",@r; } printf "%7s => %7s ( %7s : %-3s) ( %7s : %-3s )\n", qw(Input Expect L +toR ok? RroL ok?); printf "%7s %7s %7s %-3s %7s %-3s \n", ("---") x 6; while(<DATA>) { last unless /\S/; chomp; my ($input,$expect)=split /\s+/,$_; my $ltor=ltor($input); my $rtol=rtol($input); printf "%7s => %7s ( %7s : %-3s) ( %7s : %-3s )\n", $input, $expect, $ltor, $ltor eq $expect ? 'Ok' : 'Not', $rtol, $rtol eq $expect ? 'Ok' : 'Not'; } __END__ 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
Input => Expect ( LtoR : ok?) ( RroL : ok? ) --- --- --- --- --- --- I => ......I ( ......I : Ok ) ( ......I : Ok ) IV => .....V. ( ......I : Not) ( .....V. : Ok ) V => .....V. ( .....V. : Ok ) ( .....V. : Ok ) VI => .....VI ( .....VI : Ok ) ( .....VI : Ok ) IX => ....X.. ( ......I : Not) ( ....X.. : Ok ) X => ....X.. ( ....X.. : Ok ) ( ....X.. : Ok ) XI => ....X.I ( ....X.I : Ok ) ( ....X.I : Ok ) XIV => ....XV. ( ....X.I : Not) ( ....XV. : Ok ) XV => ....XV. ( ....XV. : Ok ) ( ....XV. : Ok ) XVI => ....XVI ( ....XVI : Ok ) ( ....XVI : Ok ) XIX => ....X.I ( ....X.I : Ok ) ( ....X.. : Not ) X => ....X.. ( ....X.. : Ok ) ( ....X.. : Ok ) XL => ....X.. ( ....X.. : Ok ) ( ...L... : Not ) LX => ...LX.. ( ...LX.. : Ok ) ( ...LX.. : Ok ) XC => ....X.. ( ....X.. : Ok ) ( ..C.... : Not ) CLXIX => ..CLX.I ( ..CLX.I : Ok ) ( ..CLX.. : Not ) CDXLVI => .D..XVI ( ..C.XVI : Not) ( .D.L.VI : Not ) MCMXCVI => M.C.XVI ( M.C.XVI : Ok ) ( M.C..VI : Not ) MDCLI => MDCL..I ( MDCL..I : Ok ) ( MDCL..I : Ok )

Id be interested to see a better explanation of what you expect.

---
$world=~s/war/peace/g


In reply to Re: Help with a Regex by demerphq
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
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 perusing the Monastery: (6)
As of 2024-03-28 19:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found