Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Splitting and keeping the delimiter

Reply
Thread Tools

Splitting and keeping the delimiter

 
 
Sandman
Guest
Posts: n/a
 
      09-10-2003
Hello! I have a string like this:

12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak

And I want to split that in seperate "programs" that I'd like to deal with
individually. So, I want this in an array like this:

"12.00 Simpsons",
"12.30 Seindfeld"
"13.00 Movie: Dante's Peak"

And the one thing that I can split on is '\d\d\.\d\d', which is the only common
thing, but using

split /\d\d\.\d\d/

produces

"Simpsons",
"Seindfeld"
"Movie: Dante's Peak"

Is there a way? How would you do it?

--
Sandman[.net]
 
Reply With Quote
 
 
 
 
Glenn Jackman
Guest
Posts: n/a
 
      09-10-2003
Sandman <(E-Mail Removed)> wrote:
> Hello! I have a string like this:
>
> 12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak
>
> And I want to split that in seperate "programs" that I'd like to deal with
> individually. So, I want this in an array like this:
>
> "12.00 Simpsons",
> "12.30 Seindfeld"
> "13.00 Movie: Dante's Peak"


my $str = "12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak";
my $delim = qr/\d\d\.\d\d/;
my @programs = $str =~ m{
$delim # the delimiter
.+? # followed by some characters, until ...
(?= $delim | $) # we can look ahead to see delim or the end of string
}xg;

--
Glenn Jackman
NCF Sysadmin
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      09-10-2003
Sandman wrote:
>
> Hello! I have a string like this:
>
> 12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak
>
> And I want to split that in seperate "programs" that I'd like to deal with
> individually. So, I want this in an array like this:
>
> "12.00 Simpsons",
> "12.30 Seindfeld"
> "13.00 Movie: Dante's Peak"
>
> And the one thing that I can split on is '\d\d\.\d\d', which is the only common
> thing, but using
>
> split /\d\d\.\d\d/
>
> produces
>
> "Simpsons",
> "Seindfeld"
> "Movie: Dante's Peak"
>
> Is there a way? How would you do it?



$ perl -le'
$string = " 12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dantes Peak ";
@array = $string =~ /\d\d\.\d\d.+?(?=\d|$)/g;
print for @array;
'
12.00 Simpsons
12.30 Seinfeld
13.00 Movie: Dantes Peak



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      09-11-2003
Sandman <(E-Mail Removed)> wrote:

> Hello! I have a string like this:
>
> 12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak
>
> And I want to split that in seperate "programs" that I'd like to deal with
> individually. So, I want this in an array like this:
>
> "12.00 Simpsons",
> "12.30 Seindfeld"
> "13.00 Movie: Dante's Peak"



You have workable solutions from several responses. Just one comment: use
split() when you know what to throw away, a regex when you know what to keep.
(I believe I'm quoting someone, but can't remember whom.)

--
David Wall
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      09-11-2003
>>>>> "DKW" == David K Wall <(E-Mail Removed)> writes:

DKW> You have workable solutions from several responses. Just one
DKW> comment: use split() when you know what to throw away, a regex
DKW> when you know what to keep. (I believe I'm quoting someone, but
DKW> can't remember whom.)

if he didn't say it first, he surely publicized the most, randal
schwartz.

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
Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-11-2003
Uri Guttman <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >>>>> "DKW" == David K Wall <(E-Mail Removed)> writes:

>
> DKW> You have workable solutions from several responses. Just one
> DKW> comment: use split() when you know what to throw away, a regex
> DKW> when you know what to keep. (I believe I'm quoting someone, but
> DKW> can't remember whom.)
>
> if he didn't say it first, he surely publicized the most, randal
> schwartz.


....so much so that the saying is sometimes called "Randal's Rule".
 
Reply With Quote
 
Janek Schleicher
Guest
Posts: n/a
 
      09-11-2003
Sandman wrote at Wed, 10 Sep 2003 22:29:57 +0200:

> Hello! I have a string like this:
>
> 12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak
>
> And I want to split that in seperate "programs" that I'd like to deal with
> individually. So, I want this in an array like this:
>
> "12.00 Simpsons",
> "12.30 Seindfeld"
> "13.00 Movie: Dante's Peak"


I wouldn't split it, as it is easier IMHO to describe what you want
instead of what you won't:

my $progstr = "12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak";
my @program = $progstr =~ /\d+\.\d+\s+.*?(?=\s+\d|$)/g;
print join "\n", @program;


Greetings,
Janek
 
Reply With Quote
 
Sandman
Guest
Posts: n/a
 
      09-12-2003
Thanks for all the suggestions!

--
Sandman[.net]
 
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
Re: Splitting text at whitespace but keeping the whitespace in thereturned list MRAB Python 3 01-26-2010 11:36 PM
Problem while splitting the line using a delimiter and pushing itinto an array Prasanth C++ 4 11-22-2008 05:10 AM
Reading files, splitting on a delimiter and newlines. Bruno Desthuilliers Python 8 07-26-2007 06:45 AM
Splitting a sentence with delimiter preserved Ajithkumar Warrier Ruby 0 10-17-2006 08:25 PM
Splitting and keeping key/value Sandman Perl Misc 17 09-27-2006 11:46 AM



Advertisments