in reply to formatting output question (use of recursive subroutine?)

I was bored :)
my $ref = 'agctagctagctagcatgctagctagctgatcgatgctagctagctgactgacgacg'; my @subseq_to_find = qw{ gctagctag tagcatgctagctag gctagctgac tag }; my @output; for my $subseq ( @subseq_to_find ) { my $p = -1; MATCH: while (1) { $p = index $ref, $subseq, $p + 1; last if $p < 0; my $len = length $subseq; for ( @output ) { my $substr = \ substr $_, $p, $len; $$substr = $subseq and next MATCH unless $$substr =~ /\S/; } push @output, " " x length $ref; substr $output[-1], $p, $len, $subseq; } } print "$_\n" for $ref, @output;

Output:

agctagctagctagcatgctagctagctgatcgatgctagctagctgactgacgacg gctagctag tag gctagctag gctagctag gctagctag tag tag gctagctgac tag tag tagcatgctagctag tag tag

Update: next SUBSEQ should be next MATCH. Added a smaller pattern to test the fix. I guess I'm still bored.


Unless I state otherwise, all my code runs with strict and warnings

Replies are listed 'Best First'.
Re^2: formatting output question (use of recursive subroutine?)
by rogerd (Sexton) on Aug 04, 2008 at 21:24 UTC
    Thank you FunkyMonk! Very helpful boreness! At this time I am working with the code that BrowserUK suggested me. I had to change some algorithms of my previous code to use that algorithm, so I am working on that. But I will take a look at yours also, it seems interesting. You use at least one function that I didn't know: index.