Question

I have a string that with several parts separated by tabs:

 Hello\t2009-08-08\t1\t2009-08-09\t5\t2009-08-11\t15

I want to split it up only on the first tab, so that "Hello" ends up in $k and and rest ends up in $v. This doesn't quite work:

my ($k, $v) = split(/\t/, $string);

How can I do that?

Was it helpful?

Solution

In order to get that, you need to use the 3rd parameter to split(), which gives the function a maximum number of fields to split into (if positive):

my($first, $rest) = split(/\t/, $string, 2);

OTHER TIPS

No. It will give you the first two items and toss the rest. Try this:

my ($k, $v) = split(/\t/, $string, 2);

Another option would be to use a simple regex.

my($k,$v) = $str =~ /([^\t]+)\t(.+)/;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top