in reply to can split() use a regex?

If you don't want to discard the number, you'd need to use capture brackets

my ($one, $two) = split( /(\d+)\s+/, $line, 2);

Probably easier to use m//:

my ($one, $two) = $line =~ m[(\d+)\s+(.*)$];

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.

Replies are listed 'Best First'.
Re^2: can split() use a regex?
by bart (Canon) on Jun 18, 2006 at 11:33 UTC
    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.

      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.