CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
matching between two strings

 



harry34
stranger

Jul 14, 2003, 2:54 AM

Post #1 of 2 (3282 views)
matching between two strings Can't Post

I have a file which contains numerous datasets having the following format, see below:
I need to extract the data between Final graph set matrix and PLUTO4 finished each time it is present in the file (so need some sort of loop).
I have the following pattern for matching the data:


Code
/([A-Z]\s\d+,\s\d+\(\s*\d+\))/g


Also I need each data seperated by a title or new line
Can anyone help


Final graph set matrix
----------
C 1, 1( 9)
----------
C 2, 2(11) C 1, 1( 4)
----------
C 2, 2(18) C 1, 2(11) C 1, 1( 9)
[R 2, 2( 8)]
----------
C 1, 2(11) C 2, 2(18) C 2, 2(11) C 1, 1( 4)
[R 2, 2(18)]
PLUTO4 finished


Final graph set matrix
----------
C 1, 1( 4)
----------
C 2, 2(18) C 1, 1( 4)
[R 2, 2(18)]
PLUTO4 finished


Final graph set matrix
----------
C 1, 1( 4)
----------
C 2, 2(18) C 1, 1( 4)
[R 2, 2(18)]
----------
C 2, 2(11) C 2, 2(11) C 1, 1( 9)
----------
C 2, 2(11) C 2, 2(11) C 2, 2(18) C 1, 1( 9)
[R 2, 2( 4)]
PLUTO4 finished


davorg
Thaumaturge / Moderator

Jul 15, 2003, 1:06 AM

Post #2 of 2 (3275 views)
Re: [harry34] matching between two strings [In reply to] Can't Post

You can do something like this to get your data into a useable data structure.


Code
my (@sets, @set); 

my $in_record;
while (<INPUT>) { # or whatver your filehandle is called
if (/^Final graph set matrix$/) {
$in_record = 1;
@set = ();
next;
}
if (/^ PLUTO4 finished$/) {
$in_record = 0;
push @sets, [ @set ];
}
next if /^-+$/; # skip separator lines

push @set, $_ if in_record;
}


At the end of this code you'll have an array called @sets. Each element in this array contain one of your data sets. Each element is actually a reference to another array which contains all of the lines of data in the set.

Don't really know what you want to do after that as I don't understand your description.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives