Ravi Malghan wrote:
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.
Okay. There is a missing comma between "ADDR,35" and "15421", right?
Under that assumption, I believe this code gets what you want:
C:\home>type test.pl
my $origString = "EMPLID,4,9066,USERID,7,W3LWEB1,TEXT,54,"
. "This is a test note, with some commas, and more commas,"
. "ADDR,35,15421 Test Lane, Rockville, MD, USA,ESCALATION-LVL,1,0";
my @parts = split /([A-Z-]+),(\d+)/, $origString;
shift @parts;
while ( my $k = shift @parts ) {
my $length = shift @parts;
print "$k => ", substr( shift @parts, 1, $length ), "\n";
}
C:\home>test.pl
EMPLID => 9066
USERID => W3LWEB1
TEXT => This is a test note, with some commas, and more commas
ADDR => 15421 Test Lane, Rockville, MD, USA
ESCALATION-LVL => 0
C:\home>
--
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/