<?xml version="1.0" encoding="windows-1252"?>
<node id="1005089" title="Re: Is it possible to get all tetra words with correct starting position using a better code within a loop?" created="2012-11-22 04:28:53" updated="2012-11-22 04:28:53">
<type id="11">
note</type>
<author id="461912">
GrandFather</author>
<data>
<field name="doctext">
&lt;p&gt;There are many issues with your code. First off though, always use strictures (use [doc://strict]; use [doc://warnings]; - see [id://686571]). You use warnings, but strict is at least as important for catching errors. As another general coding tip: don't use the same name for multiple variables. In your sample code you use both $pro and @pro as well as @tetra and $tetra.&lt;/p&gt;
&lt;p&gt;Although it is often a good idea to give a manifest constant a name so the intent of the constant is clear, using a variable for 1 called $one adds no information and is likely to cause confusion just because there seems no reason to use the variable.&lt;/p&gt;
&lt;p&gt;Your "uninitialized value" variable warning is because you use @+ before the first regular expression match.&lt;/p&gt;
&lt;p&gt;You aren't getting the number of iterations in the loop you expect because you update @pro within the loop. That is almost always a bad idea.&lt;/p&gt;
&lt;p&gt;There are many ways to skin this cat. One trick is to use a look ahead match and take advantage of the fact that the regular expression engine doesn't allow successive matches at the same position. Consider:&lt;/p&gt;
&lt;c&gt;
#!/usr/bin/perl  
use warnings;
use strict;

my $pro = "ABCDEFGH";
my @tetras;

push @tetras, [$1, $+[0] + 1] while $pro =~ /(?=(.{4}))/g;
print "$_-&gt;[0] -&gt; Starting at pos $_-&gt;[1]\n" for @tetras;
&lt;/c&gt;
&lt;p&gt;Prints:&lt;/p&gt;
&lt;c&gt;
ABCD -&gt; Starting at pos 1
BCDE -&gt; Starting at pos 2
CDEF -&gt; Starting at pos 3
DEFG -&gt; Starting at pos 4
EFGH -&gt; Starting at pos 5
&lt;/c&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-461912"&gt;
True laziness is hard work
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1005077</field>
<field name="parent_node">
1005077</field>
</data>
</node>
