etheral has asked for the wisdom of the Perl Monks concerning the following question:

#################################################################### ## MAIN #################################################################### my $contiguous_characters_length = <>; chomp $contiguous_characters_length; my $sequence_1 = 'qwertyuiop'; for (my $i = 0; $i < length $sequence_1 ; $i++) { my $results = substr ($sequence_1, $i-($contiguous_characters_length +-1), $contiguous_characters_length); print "$results\n"; } exit;

The code is flawed in that if you input length 4 it returns:

iop op p qwer wert erty rtyu tyui yuio uiop

And I don't want first three lines. (If length is 3 I don't want first two lines etc.) Could you help me solve this problem? Where's the bug in the for loop? This is essential for further BLAST analysis, if anyone wonders why I need that program:)

Replies are listed 'Best First'.
Re: Contiguous characters BLAST
by choroba (Cardinal) on Oct 26, 2011 at 16:42 UTC
    Just initialize the loop with
    my $i = $contiguous_characters_length - 1
    instead of 0.
Re: Contiguous characters BLAST
by AnomalousMonk (Archbishop) on Oct 26, 2011 at 19:34 UTC

    The unpack approach is probably fastest, but here's a regex way that doesn't require shortening the output array:

    >perl -wMstrict -le "my $len = 4; my $seq = 'qwertyuiop' ;; my @contigs = $seq =~ m{ (?= (.{$len})) }xmsg; printf qq{'$_' } for @contigs; " 'qwer' 'wert' 'erty' 'rtyu' 'tyui' 'yuio' 'uiop'
Re: Contiguous characters BLAST
by Cristoforo (Curate) on Oct 26, 2011 at 18:52 UTC