Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I, too, thought of BrowserUk's  (?= (overlapping capture)) hack (Update: ColonelPanic previously used a version of this hack.) when I first read the OP, but supriyoch_2008 also wants starting positions. No problem, thought I, just throw in a little  (?{ code }) and the necessary info can be captured. (The offsets produced in the code examples below are 0-based rather than 1-based as supriyoch_2008 wants, but that's a mere detail. Also, I don't maintain that this approach is necessarily to be preferred as being faster/better/etc.)

However, a little fly in the soup. The code examples 'work', but I don't quite understand what's going on: the positions in the  @tetras_pos array are doubled for some reason, hence the  $_ * 2 indexing hack in printing position info. In the second example, I can understand the presence of the (5, 6, 7, 8) positions at the end of the (0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 6, 7, 8) list of positions as resulting from failed attempts by  (?= (....)) to match in positions in which a match is impossible because there are fewer than four characters remaining in the string, but I still don't understand the doubling in the previous part of the list.

I have the feeling this behavior has been touched on before somewhere, but I can't lay my hands on a reference. Can anyone offer any insight?

>perl -wMstrict -le "my $pro = 'ABCDEFGH'; ;; my @tetras_pos; my @tetras = $pro =~ m{ (?= (....) (?{ push @tetras_pos, $-[1] })) }xmsg; ;; print qq{'$tetras[$_]' @ $tetras_pos[$_ * 2]} for 0 .. $#tetras; print qq{@tetras_pos}; ;; @tetras_pos = (); @tetras = $pro =~ m{ (?= ((?{ push @tetras_pos, pos $pro }) ....)) }xmsg; ;; print qq{'$tetras[$_]' @ $tetras_pos[$_ * 2]} for 0 .. $#tetras; print qq{@tetras_pos}; " 'ABCD' @ 0 'BCDE' @ 1 'CDEF' @ 2 'DEFG' @ 3 'EFGH' @ 4 0 0 1 1 2 2 3 3 4 4 'ABCD' @ 0 'BCDE' @ 1 'CDEF' @ 2 'DEFG' @ 3 'EFGH' @ 4 0 0 1 1 2 2 3 3 4 4 5 6 7 8

In reply to Re: Is it possible to get all tetra words with correct starting position using a better code within a loop? by AnomalousMonk
in thread Is it possible to get all tetra words with correct starting position using a better code within a loop? by supriyoch_2008

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (8)
    As of 2014-08-23 18:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (176 votes), past polls