ts: how to parse embedded commas 
Author Message
 ts: how to parse embedded commas

** Using the split function or Text::ParseWords won't work here **
Suppose a line of variables has this format:
   var1, var2,   var3,var4
But sometimes variables are computed and have an alias:
   sum(var1, var2) as var3
or even:
   sum(sum(var1, var2), sum(var3, sum(var4, var5))) as var6
So how does one parse the following line to get the variable
names var1, var2, and var3:
   $x= "var1,var2, sum(sum(i,j), sum(k, sum(l, m))) as var3";
Is there an easier way than iterating over the string char by
char and keeping track of the depth of parenthesis?


  Posted using Reference.COM                   http://www.*-*-*.com/
  FREE Usenet and Mailing list archive, directory and clipping service

Tue, 08 Aug 2000 03:00:00 GMT  
 ts: how to parse embedded commas

>    $x= "var1,var2, sum(sum(i,j), sum(k, sum(l, m))) as var3";

you should be able to do something like:

while($x =~ s/sum\(([^()]*)\)/&sum($1)/e) {}

sub sum
   my ($v1, $v2) = split(/\s*,\s*/, $_[0]);
   return $v1 + $v2;

if you have other function names you should be able to substitute the
following while loop

while($x =~ s/sum\(([^()]*)\)/&sum($1)/e || $x =~
s/product\(([^()])\)/&product($1)/e) {}

by using the [^()] in the regex, it ensures that it parses the innermost
functions calls first.

Wed, 09 Aug 2000 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. ts: ts: how to parse embedded commas

2. ts: ts: Remove files in a PERL sript

3. split on comma except when embedded inside quotes

4. Embedded new lines in a comma delimited file

5. Easy way to find Embedded Commas, etc?

6. reular expressions with embedded commas

7. Comma Seperated Value (CSV) files, Parsing

8. Looking for code to parse records with comma separated values

9. OT? Parse::RecDescent - comma-list question

10. Parsing Comma Delemited Text DataBase

11. help!: RE parse comma separated list


Powered by phpBB® Forum Software