Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Choosing grouping of split items

Reply
Thread Tools

Choosing grouping of split items

 
 
fishfry
Guest
Posts: n/a
 
      02-15-2004
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?
 
Reply With Quote
 
 
 
 
Juha Laiho
Guest
Posts: n/a
 
      02-15-2004
fishfry <(E-Mail Removed)> said:
>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?


I don't understand the magic of one-lining "everything"; write code as
it fits nicely. Of course, Perl golf is a different issue (writing code
to accomplish a given task with a minimal amount of characters).

As to how I'd solve your problem would be split the data, then join
the first two fields if the split resulted in 4 fields. Alternative
would be to write a regular expression that always returns 3 fields;
something like
/(.*)\s+-\s+([^-]*)\s+-\s+(.*)/
might be close to your needs. Whether this is faster or slower than
splitting and recombining I don't know -- but that you can find out
with the benchmarking modules.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      02-15-2004
fishfry <(E-Mail Removed)> 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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-15-2004
fishfry <(E-Mail Removed)> 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

 
Reply With Quote
 
Jay Tilton
Guest
Posts: n/a
 
      02-15-2004
fishfry <(E-Mail Removed)> 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.

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-15-2004
>>>>> "AS" == Anno Siegel <(E-Mail Removed)-berlin.de> writes:

AS> my @tokens = split /-/, $str;
AS> @tokens = ( "$tokens[ 0]-$tokens[ 1]", @tokens[ 2 .. $#tokens]) if
AS> @tokens > 3;

blech!

$tokens[0] .= '-' . splice( @tokens, 1, 1 ) if @tokens > 3;

or

splice( @tokens, 0, 2 ) = join '-', splice( @tokens, 1, 1 )
if @tokens > 3;

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      02-15-2004
Uri Guttman <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >>>>> "AS" == Anno Siegel <(E-Mail Removed)-berlin.de> writes:

>
> AS> my @tokens = split /-/, $str;
> AS> @tokens = ( "$tokens[ 0]-$tokens[ 1]", @tokens[ 2 .. $#tokens]) if
> AS> @tokens > 3;
>
> blech!


Agreed.

> $tokens[0] .= '-' . splice( @tokens, 1, 1 ) if @tokens > 3;


Much better.

> or
>
> splice( @tokens, 0, 2 ) = join '-', splice( @tokens, 1, 1 )
> if @tokens > 3;


That's splice( @tokens, ...) twice in a single statement.

Anno
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-16-2004
>>>>> "AS" == Anno Siegel <(E-Mail Removed)-berlin.de> writes:

AS> Uri Guttman <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> >>>>> "AS" == Anno Siegel <(E-Mail Removed)-berlin.de> writes:

>>

AS> my @tokens = split /-/, $str;
AS> @tokens = ( "$tokens[ 0]-$tokens[ 1]", @tokens[ 2 .. $#tokens]) if
AS> @tokens > 3;
>>
>> blech!


AS> Agreed.

>> $tokens[0] .= '-' . splice( @tokens, 1, 1 ) if @tokens > 3;


AS> Much better.

>> or
>>
>> splice( @tokens, 0, 2 ) = join '-', splice( @tokens, 1, 1 )
>> if @tokens > 3;


AS> That's splice( @tokens, ...) twice in a single statement.

true. i thought it was kinda cute but not optimal. how about this
variant:

$tokens[0] = join '-', shift @tokens, splice( @tokens, 1, 1 ) ;

again it has @tokens twice.

oh well. i have other fish to fry.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      02-16-2004
fishfry 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.
>
> Is there a one-liner for that?


$ perl -le'
$text = "abc - def - ghi - jkl";
@tokens = $text =~ /(.+)-(.+)-(.+)/;
print for @tokens;
'
abc - def
ghi
jkl



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Grouping items in checkboxlist maury ASP .Net 0 04-17-2011 05:37 PM
How to split a grouping into 3 parts Hvid Hat XML 6 03-07-2008 02:00 PM
grouping and split barjunk Ruby 7 07-11-2007 07:30 PM
Grouping like items together AcCeSsDeNiEd Perl Misc 9 11-17-2005 01:46 AM
grouping items among a list according to items subtag value Gilles Kuhn XML 0 09-15-2003 12:01 PM



Advertisments