Jason Price wrote:
Gunnar Hjalmarsson wrote:
Jason Price wrote:
Not every line will have the same keys, or the same number of
keys.
In that case the example you posted is obviously not sufficient
as a problem description.
Basically, every line will follow a similar pattern: "junk KEY1
value1 KEY2 value2 KEY3 'value 3 here'". Each word that is all
caps is a key, and text between that key and the next is the value
for that key. The problem is that one line may have 2 key/value
sets, while the next has 4 sets.
Here's 2 lines from the file, demonstrating differences:
"( uiwgAttribute-OID NAME 'uiwgAttribute' DESC 'Contains meta data
about an attribute' SUP top MUST cn MAY ( uiwgADsType $
uiwgDescription $ uiwgDisplayName $ uiwgIsMultiValued ) )
( wgDestination-OID NAME 'wgDestination' SUP top MUST cn MAY wgURI
)"
Well, that makes it a little easier to understand. Assuming that you
know the names of possible keys and that NAME is always present, this
suggestion stores an anonymous hash per line in the array @lines:
my @lines;
while (<FILE>) {
if ( /
NAME\s+'([^']+)'\s+
(?:
DESC\s+'([^']+)'\s+
)?
(?:
SUP\s+(\w+)\s+
)?
(?:
MUST\s+(\w+)\s+
)?
(?:
MAY\s+\(?\s*([^)]+\w)
)?
/x ) {
push @lines, {
NAME => ($1 or undef),
DESC => ($2 or undef),
SUP => ($3 or undef),
MUST => ($4 or undef),
MAY => [ $5 ? split /\s+\$\s+/, $5 : () ],
}
}
}
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>