Re: Choosing grouping of split items
From: Anno Siegel (anno4000_at_lublin.zrz.tu-berlin.de)
Date: 02/15/04
- Next message: Jay Tilton: "Re: Choosing grouping of split items"
- Previous message: Bart Lateur: "Re: Replacing unicode characters"
- In reply to: fishfry: "Choosing grouping of split items"
- Next in thread: Jay Tilton: "Re: Choosing grouping of split items"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 15 Feb 2004 11:30:58 GMT
fishfry <BLOCKSPAMfishfry@your-mailbox.com> wrote in comp.lang.perl.misc:
> 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.
>
> Is there a one-liner for that?
You can force almost everything into a single statement, the question is
if you want to. Here is one way:
my @tokens = reverse map scalar reverse, split /-/, reverse( $str), 3;
How long does it take to find out what this does? And how long for a
plain two-statement procedure?
my @tokens = split /-/, $str;
@tokens = ( "$tokens[ 0]-$tokens[ 1]", @tokens[ 2 .. $#tokens]) if
@tokens > 3;
Anno
- Next message: Jay Tilton: "Re: Choosing grouping of split items"
- Previous message: Bart Lateur: "Re: Replacing unicode characters"
- In reply to: fishfry: "Choosing grouping of split items"
- Next in thread: Jay Tilton: "Re: Choosing grouping of split items"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]