Eric Walker wrote:

> Hey all,
> I have a file with some data that has like nested parens.. example
> 
> yes( "hello" "goodbye"
> 
>      ( (one 2) (two 3) (three 4)
>      )
>      (( mon 1) (tues 2) (wed 3)
>      )
>      ((jan 1) (feb 2) (march 3)
>      )
> )
> 
> I need help  in trying to break this up and make key value pairs out of
> the data example:
> KEY    VALUE
> one          2
> two          3
> three        4
> mon         1
> tues         2
> wed         3
> jan           1
> feb           2
> march       3
> 

there are many ways of doing that, here is one:

#!/usr/bin/perl -w
use strict;

(my $parens=<<PARENS) =~ s/\n//g;
(one 2) (two 3) (three 4)
)
((mon 1) (tues 2) (wed 3)
)
((jan 1) (feb 2) (march 3)
)
PARENS

my %hash = map split, $parens =~ /\(+([^()]+)\)+/g;

while(my($name,$value) = each %hash){
        print "$name,$value\n";
}

__END__

prints:

feb,2
jan,1
three,4
march,3
one,2
mon,1
wed,3
tues,2
two,3

depends on the actual data you have, you might need to modify the above 
slightly to suit your case. another way of doing it is to remove the parens 
and replace multiple spaces with one and then split on a single space to 
get the name value pair you want but this approach is slightly less 
efficient than the proposed one. it's, however, a bit easier to code.
 
david
-- 
s,.*,<<,e,y,\n,,d,y,.s,10,,s
.ss.s.s...s.s....ss.....s.ss
s.sssss.sssss...s...s..s....
...s.ss..s.sss..ss.s....ss.s
s.sssss.s.ssss..ss.s....ss.s
..s..sss.sssss.ss.sss..ssss.
..sss....s.s....ss.s....ss.s

,....{4},"|?{*=}_'y!'+0!$&;"
,ge,y,!#:$_(-*[./<[EMAIL PROTECTED],b-t,
.y...,$~=q~=?,;^_#+?{~,,$~=~
y.!-&*-/:[EMAIL PROTECTED] ().;s,;,
);,g,s,s,$~s,g,y,y,%,,g,eval

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to