Beloved Monks,
I have the following codes that take sequence of multiple lines then
concatenate if it doesn't begin with '>' and print the line index next to it.
See below for output.
Currently the solution I came up with is by using *array* and it somehow
looks clumsy. I couldn't think of a straighforward way that avoid using it.
In short a more elegant solution.
The reason why I want to do this is that I need to process the concatenated sequence
on the fly, without having to store it in array in the first place. There are practically
thousands of these sequences.
I wonder how would masters approach this.
#!/usr/bin/perl -w
use strict;
my @string; # Can I avoid using this array for the task?
while(<DATA>){
s/\s//g;
if (/^>/)
{ push(@string,'');
next;
}
chomp;
# concatenate current line
# to the last array item
$string[-1] .= $_;
}
print $_+1," : ", $string[$_],"\n" foreach (0..$#string);
__DATA__
> Seq 1 (two lines)
AAAAAAAAAAAAA
CCAAAAAAAAAAA
> Seq 2 (two lines)
AAAAAAAAAAAAA
AAAAAAAAAAAAA
> Seq 3 (one line)
TTTTTTTTTTTTAACTGAAGATTCGC
The desired output as the current code also gives is:
1 : AAAAAAAAAAAAACCAAAAAAAAAAA
2 : AAAAAAAAAAAAAAAAAAAAAAAAAA
3 : TTTTTTTTTTTTAACTGAAGATTCGC
Thanks so much beforehand.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.