in reply to Regular Expressions Challenge
While certainly not shorter than other posts, I believe the following provides what the OP is looking for.
#!c:\perl\bin\perl.exe use strict; use warnings; my @comments; my $i; my $j = 0; my $found_comment = 0; my $input = 'c:\yourdirectoryhere\sample_perl_data1.txt'; my $line; open(IN, $input) or die("Can't open file - $input: $!"); while($line = <IN>) { if($line =~ m/^=/) { if($line =~ m/^===Comments===/) { $found_comment = 1; if(! defined $i) { $i = 0; } else { $i++; } $comments[$i] = ""; # initialize new comments array el +ements } else { $found_comment = 0; } } else { if($found_comment == 1) { $comments[$i] = $comments[$i] . $line; } } } close(IN) or die("Unable to close file - $input: $!"); #print output with a separator line between each element for($j=0; $j<=$i; $j++) { print("comments[$j] = $comments[$j]"); print("------------separator-------------------\n"); }
Update:
Sample output below:
comments[0] = User comments are added here. A user may write whatever they may wish. ------------separator------------------- comments[1] = Comments are related to the microarray data here. ------------separator------------------- comments[2] = Comments related to the pathway information here. ------------separator-------------------
|
|---|