Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Is it possible to get all tetra words with correct starting position using a better code within a loop?

by AnomalousMonk (Chancellor)
on Nov 22, 2012 at 18:54 UTC ( #1005172=note: print w/replies, xml ) Need Help??


in reply to Is it possible to get all tetra words with correct starting position using a better code within a loop?

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
  • Comment on Re: Is it possible to get all tetra words with correct starting position using a better code within a loop?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Is it possible to get all tetra words with correct starting position using a better code within a loop?
by supriyoch_2008 (Monk) on Dec 03, 2012 at 06:28 UTC

    AnomalousMonk

    Thank you very much for the code. It has solved my problem. I am sorry for late reply as I had no access to internet for a few days due to some technical problem.

    With Regards,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005172]
help
Chatterbox?
[Discipulus]: Corion are you would able to realize such thing? O_O
[Corion]: In the same vein I have a script that automates Firefox to enter some data into another system. It's not faster than the people using the script if they were to do it manually, but they prefer not having to check the data and not having typos when ...
[Corion]: ... entering the data
[Corion]: Discipulus: I don't know whether I could really do that, but the init process itself mostly launches other processes, and the whole startup is just following a path of dependencies and making sure they are all running. Which basically is what ...
[Discipulus]: when at work my time is (temporarly) owned by the firm, so i do not care (coworkers whatch movies.. I code Perl)
[Corion]: ... make already does, except for files instead of programs. But maybe with some /proc hackery, that could be eliminated and one could use plain make :-D
[choroba]: systemd just makes is asynchronous
[choroba]: so, make -j
[Corion]: Discipulus: Yeah - but when writing Perl to save time (instead of having fun), it helps to look whether you're actually saving time ;) Why spend 5 minutes doing manually what you can spend three years automating? ;)
[Corion]: choroba: Oh, yeah :-D

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2017-07-27 09:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (408 votes). Check out past polls.