note
GrandFather
<p>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.</p>
<p>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.</p>
<p>Your "uninitialized value" variable warning is because you use @+ before the first regular expression match.</p>
<p>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.</p>
<p>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:</p>
<c>
#!/usr/bin/perl
use warnings;
use strict;
my $pro = "ABCDEFGH";
my @tetras;
push @tetras, [$1, $+[0] + 1] while $pro =~ /(?=(.{4}))/g;
print "$_->[0] -> Starting at pos $_->[1]\n" for @tetras;
</c>
<p>Prints:</p>
<c>
ABCD -> Starting at pos 1
BCDE -> Starting at pos 2
CDEF -> Starting at pos 3
DEFG -> Starting at pos 4
EFGH -> Starting at pos 5
</c>
<div class="pmsig"><div class="pmsig-461912">
True laziness is hard work
</div></div>
1005077
1005077