chintu has asked for the wisdom of the Perl Monks concerning the following question:

Hi I want to split the sentence into two parts by using the first whitespace as delimiter. For example
$string = " How are you?";
as
print "How"; print "are you?";
i tried using split function.
$firstword = split(/^\s+/, $string); print $firstword;
It is not working. Can anyone help me? Thanks Chintu.

Replies are listed 'Best First'.
Re: How to split to the sentence by first whitespace as delimiter?
by japhy (Canon) on Mar 28, 2001 at 20:06 UTC
    Use the third argument to split(), which says how many parts to split the string into:
    ($first, $rest) = split ' ', $string, 2;
Re: How to split to the sentence by first whitespace as delimiter?
by I0 (Priest) on Mar 28, 2001 at 08:50 UTC
    print ($string =~ /(\s)(.*?)\1(.*)/)[1,2];
Re: How to split to the sentence by first whitespace as delimiter?
by I0 (Priest) on Mar 28, 2001 at 08:45 UTC
    ($firstword) = $string =~ /(\w+)/;