Re: Choosing grouping of split items

From: Jay Tilton (tiltonj_at_erols.com)
Date: 02/15/04


Date: Sun, 15 Feb 2004 11:57:04 GMT

fishfry <BLOCKSPAMfishfry@your-mailbox.com> wrote:

: If I have
:
: $text = 'abc - def - ghi - jkl";
:
: then
:
: @tokens = split(/-/, $text, 3);
:
: splits the line into abc, def, ghi - jkl (ignoring leading and trailing
: spaces).
:
: I have an application where there may be 3 or 4 items separated by '-'
: but if there are 4, I want them returned as abc - def, ghi, and jkl.

What did you try?

Split the string and rejoin whatever fields you want to be together.

    my @tokens = split /-/, $text;
    unshift @tokens, join '-', splice @tokens,0,-2;

: Is there a one-liner for that?

If that's what really turns you on.

    my @tokens = map scalar reverse, reverse split(/-/, reverse($text), 3);

Yuck. Stick with the two-liner.

Odd. I don't see anywhere in the documentation that says scalar reverse()
defaults to acting on $_ when no argument is supplied, but it evidently
does.



Relevant Pages