> I have a list of the following format that I want to > parse:- > > ((sa1 da1 sp1 dp1 p1) (sa2 da2 sp2 dp2 p2) .... (saN daN > spN dpN pN) ) > > there are N entries. There are many such lists and N > varies in each list. One way to parse this is to use > brute force and use sth like the following for each > tuple (for simplicity let's assume there are only two > elements, x and y, instead of five):- > /\(\s*(\S+)\s*(\w+)\s*\)(.*)/ > > This will yeild x=$1, y=$2 and remainingTuples=$3. I > can put this in a loop until all tuples are parsed. > > I was wondering if there is a single-shot method of > parsing the whole thing. Any comments or ideas are > appreciated.
You could create an array of arrays (perldoc perllol) like so: --begin code-- #!perl -w use strict; my @data = (); my $data = '((sa1 da1 sp1 dp1 p1) (sa2 da2 sp2 dp2 p2))'; # this regex works for the sample data, but you # might want to expand it for your own purposes while($data =~ /\(([^()]*)\)/g) { push @data,[split /\s+/,$1]; } for(@data) { print "@$_\n\n"; } --end code-- You could then check the number of elements in the Nth list like so: my $nth_length = scalar @{$data[N]}; Or grab dp1 from the Nth list like so: my $nth_dp1 = $data[N]->[3]; Hope that helps, -dave -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]