At 01:40 2001.07.01, Jeff 'japhy' Pinyan wrote:
>On Jun 30, Sanjeeb Basak said:
>
>>I want to perform a simple split operation, but can't get the regular expr
>>working. Can anybody help me on this?
>>
>>my $line from a file read is:
>>xyz abc 12sd "pqr stz" dfg (delimited by blank char).
>>
>>I'm doing
>>my ($par1, $par2, $par3, $par4, $par5) = split(/ /, $line);
>
>Using split() isn't good enough here.  You'll need to use a more involving
>regex:
>
>  my @pieces;
>  push @pieces, $1 while
>    $line =~ /\G\s*"([^"]*)"/gc or
>    $line =~ /\G\s*(\S+)/gc;
>
>That code matches either quoted strings, or groups of non-whitespace, and
>puts them, one-at-a-time, into @pieces.

The module Text::ParseWords might do exactly want you want. The functions in it allow 
you to split line with quote in them.

With the line 

        one two "three four"

parse_line('\s', 0, $line) returns the list ('one', 'two', 'three four'). If you put 1 
instead of 0 for the second parameter, the quotes are kept with the result.

Even better, if you need a quote in one your entries, you can \ed it. So, the line

        one two "three \"four"

would result in the list ('one', 'two', 'three "four'). 

The doc of the module is way clearer than me.

Hope it helps.



-----------------------------------------------------------
Éric Beaudoin                <mailto:[EMAIL PROTECTED]>

Reply via email to