in reply to Re: can split() use a regex?
in thread can split() use a regex?

Make that
my ($one, $two, $three) = split( /(\d+)\s+/, $line, 2);
The number itself will be put into $two, what comes before it into $one, what comes after it (except for the leading whitespace, that is dropped) into $three.

So yes, if you use capturing parens in the regex for split, you'll get more return values. From the docs:

If the PATTERN contains parentheses, additional array elements are created from each matching substring in the delimiter.
split(/([,-])/, "1-10,20", 3);
produces the list value
(1, '-', 10, ',', 20)

I recommend using better variables' names.

Replies are listed 'Best First'.
Re^3: can split() use a regex?
by BrowserUk (Patriarch) on Jun 18, 2006 at 11:40 UTC

    Right++ I forgot about the implicit null match at the beginning, but then I'd always use m// for this anyway :)


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.