There are many issues with your code. First off though, always use strictures (use strict; use warnings; - see The strictures, according to Seuss). 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.
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.
Your "uninitialized value" variable warning is because you use @+ before the first regular expression match.
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.
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:
#!/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;
Prints:
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
In reply to Re: Is it possible to get all tetra words with correct starting position using a better code within a loop?
by GrandFather
in thread Is it possible to get all tetra words with correct starting position using a better code within a loop?
by supriyoch_2008
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |