Gossamer Forum
Home : General : Perl Programming :

multiline text/paragraph to single array element

Quote Reply
multiline text/paragraph to single array element
Hello all,
I've got a few files to extract information from, and i am having trouble with the multi-line query. i have paragraphs of information with a info line, followed by mulitple lines of text with no spaces, folowed by a blank line.
Code:

>infoline
manylinesof
text

>infoline
manylinesof
text

i really need each seaction ie paragraph without infoline
of" manylinesoftext" each to be stored as 1 element of an array, and the infoline to be stored in a second array. i have writen a multiline regexp but i think it stores all the txt paragraphs in 1 element. the code is below
Code:
$i=0;
while($line=<INPUT1>){

if($line=~m/^[a-z].*?\b$/xm){
$array[$i]= $line;
#print $array[$i];
print OUTFILE "$array[$i]";
$i++;

}

elsif ($line=~/^[>].*?/){
push @headers, $fastafile;

$i++;
}
else{}

}
i was then going to reference each txt paragraph to $i-1 infoline. Any help on getting these of these multiline sections into seperate elements will be grateful.
txpipSmile
Quote Reply
Re: [tzpip] multiline text/paragraph to single array element In reply to
Try assigning the string to a variable. i.e;

Code:
my $data;
open(FILE, "something.txt") || die "Cant open. Reason: $!";
$data = <FILE>;
close(FILE);

Then below that, have something like;[/code]
my @data = (">infoline",$data);

my @back;
foreach (@data) {

# skip blank lines...
next if $_ =~ /^\s/;

push @back, $_;

}

print join(",",@back);

}[/code]
Untested, so may not work.

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates