Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by GrandFather (Cardinal)
on Nov 22, 2012 at 09:28 UTC ( #1005089=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?

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
True laziness is hard work


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
Re^2: Is it possible to get all tetra words with correct starting position using a better code within a loop?
by supriyoch_2008 (Scribe) on Dec 03, 2012 at 06:16 UTC

    Grand Father

    Thanks for the code. I am sorry for late reply as I had no access to internet for a few days due to some technical problem. Your code has worked nicely and it has solved my problem.

    With Regards,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005089]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2014-07-24 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (160 votes), past polls