Ravi Malghan wrote: > > Hi: I am trying to extract some stuff from a string and not getting the > expected results. I have looked through > http://www.perl.com/doc/manual/html/pod/perlre.html and can't seem to figure > this one out. > > I have a string which is a sequence of words and each item is comma seperated > field1, lengthof value1, value1,field2, length of value2, > value2,field3,length of value3, value3 and so on > > After each field name I have the length of the value > > I want to split this string into an array using comma seperator, but the > problem is some values have one or more commas within them. > > so for example my string might look like this > > $origString = "EMPLID,4,9066,USERID,7,W3LWEB1,TEXT,54,This is a test note, > with some commas, and more commas,ADDR,3515421 Test Lane, Rockville, MD, > USA,ESCALATION-LVL,1,0" > > My current code goes character by character and constructs what I want. But > is very slow when this string gets large.
The program below will do what you describe. HTH, Rob use strict; use warnings; my $origString = "EMPLID,4,9066,USERID,7,W3LWEB1,TEXT,54,This is a test note, with some commas, and more commas,ADDR,3515421 Test Lane, Rockville, MD, USA,ESCALATION-LVL,1,0"; while() { $origString =~ /\G([^,]+),/g or last; my $field = $1; $origString =~ /\G(\d+),/g or last; my $size = $1; $origString =~ /\G(.{$size}),?/g or last; my $value = $1; printf "%s(%d) - %s\n", $field, $size, $value; } **OUTPUT** EMPLID(4) - 9066 USERID(7) - W3LWEB1 TEXT(54) - This is a test note, with some commas, and more commas -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/