Hello.

I used regex to solve one problem in contest. And received error message. Later I can't understand my mistake, how I have got it.

The problem. You are given an array of non-decreasing elements. Your task is to find (if any) two rightmost neighbouring elements whose difference is bigger than some number (c) and delete left element and all preceding elements. Then say how much elements have left.

It seems that simple procedural programming here is suitable much more than regex. But I tried with regex:

#!/usr/bin/perl use warnings; use strict; while(my $c = <>){ $_ = <>; s/ .*?(\b\d+)[ ] (?=(\d+)\b) (??{ print "[$1 $2]\n"; $2 - $1 <= $c ? 'X' : '' }) / (print "DELETED:[$&] REMAINS:[$']\n"), '' /egx; printf "NUMBERS:[%d:%s]\n", (length s/\d//gr), $_; print '-' x 10, "\n"; }
INPUT:
1 1000000000 5 1 1 1 2 2 2 1 1 3 5 7 9 10 1 1 2 3 4 5 28 31 36 43 5 1 3 8 14 19 26 33 1 1 2 3 4
OUTPUT:
NUMBERS:[1:1000000000 ] ---------- [1 1] [1 1] [1 2] [2 2] [2 2] [1 1] [1 2] [2 2] [2 2] [1 1] [1 2] [2 2] [2 2] [1 2] [2 2] [2 2] [1 2] [2 2] [2 2] [2 2] [2 2] [2 2] [2 2] [2 2] [2 2] NUMBERS:[6:1 1 1 2 2 2 ] ---------- [1 3] DELETED:[1 ] REMAINS:[3 5 7 9 10 ] [3 5] DELETED:[3 ] REMAINS:[5 7 9 10 ] [5 7] DELETED:[5 ] REMAINS:[7 9 10 ] [7 9] DELETED:[7 ] REMAINS:[9 10 ] [9 10] NUMBERS:[2:9 10 ] ---------- [1 2] [2 3] [3 4] [2 3] [3 4] [2 3] [3 4] [3 4] [3 4] NUMBERS:[4:1 2 3 4 ] ---------- [28 31] [31 36] [36 43] DELETED:[28 31 36 ] REMAINS:[43 ] NUMBERS:[1:43 ] ---------- [1 3] [3 8] [8 14] DELETED:[1 3 8 ] REMAINS:[14 19 26 33 ] [14 19] [19 26] DELETED:[14 19 ] REMAINS:[26 33 ] Can't coerce UNKNOWN to string in substitution iterator at ./coerce.pl + line 11, <> line 12.

In reply to Can't coerce UNKNOWN to string in substitution iterator by rsFalse

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.