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

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

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    LanX wished he could vote in Britain
    LanX - A referendum on whether or not to have another Brexit referendum.
    LanX - Legalise the hunting of fox hunters

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (8)
    As of 2017-06-23 18:17 GMT
    Find Nodes?
      Voting Booth?
      How many monitors do you use while coding?

      Results (554 votes). Check out past polls.