Since some of your records contain spaces, a simple call to
split with no regex doesn'T do any good. Also you need to special-case the number in the first column, since you want to repeat it.
Here's my approach (reading from DATA instead of a file handle for convenience):
use strict;
use warnings;
while (<DATA>) {
chomp;
my ($id, @records) = split /\t|(?<=\)),\s+/, $_;
my (@left, @right);
for my $r (@records) {
if ($r =~ /^SP_/) {
push @left, $r;
} else {
push @right, $r;
}
}
while (@left || @right) {
print $id, "\t", (shift(@left) || ' - '),
', ', (shift(@right) || ' - '),
"\n";
}
}
__DATA__
1 SP_85(IS33, qqq), SP_155(IS33eee) spr_111(ISyyy33, qqq), spr_1
+71(IS33eee)
2 SP_83(S3 , jgjg), SP_32(IS33, jhdjdjd) spr_113(Stty3 , jgjg),
+spr_1881(IS33, jhdjdjd)
3 SP_78(3jmdsjkdej), SP_66(IShbdhdhd33) spr_115(3jmhhggggdsjkdej
+), spr_1551(IShbdhdjjjhd33), spr_88881(Iyt33ff), spr_145411(Iddd3ff)
4 SP_77(3jmdsjkdej), SP_1485(Idhd33ff) spr_116(3jmdhhhhhsjkdej),
+ spr_17781(Idhdhhtytyt33ff)
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.