Home: Perl Programming Help: Intermediate:
Parsing Delimited String



keith73
Novice

Nov 14, 2000, 9:09 AM


Views: 265
Parsing Delimited String

Have a string that I need to separate but only on the first delimit character.
The string is delimited using | and I only need to separate it using the first word and then everything after becomes the value.

product|name|type|price

I could do a split, splice and join but I want to try to do it with one command.

I've been trying to do this.

($item,$value) = /([^\|]*)/;

this only gets me $item. I want name|type|price to become $value.

I've tried adding to that match to account for everything after the first | including all remaing |'s and just can't get it to work.

Any help is appreciated.


japhy
Enthusiast

Nov 14, 2000, 9:31 AM


Views: 265
Re: Parsing Delimited String

If you read the documentation for split(), you'll see that you can tell it to split it into a specific number of parts:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


$str = "john jacob jingleheimer schmidt";

($first,$mid1,$mid2,$last) = split ' ', $str;
print "$first, $mid1, $mid2, $last\n";
# john, jacob, jingleheimer, schmidt

($first,$rest) = split ' ', $str, 2;
print "$first, $rest\n";
# john, jacob jingleheimer schmidt
</pre><HR></BLOCKQUOTE>

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



keith73
Novice

Nov 14, 2000, 9:54 AM


Views: 265
Re: Parsing Delimited String

Well smack me silly and call me slappy, I totally forgot about that.

Thanks for refreshing my memory. Of course, now I have that song stuck in my head.
"... his name is my name too..."

-Keith