Ask a Question related to PERL Miscellaneous, Design and Development.
-
Sandman #1
Splitting and keeping the delimiter
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]
Sandman Guest
-
#39306 [NEW]: Changing Delimiter
From: ctingley at mediagrids dot com Operating system: Windows PHP version: 5.1.6 PHP Bug Type: MySQL related Bug... -
Delimiter for string..
Friends, I am running a perl script as below which is working perfectly and want to replace the hardcoded values with variables. (the script... -
[PHP] Delimiter WITHOUT lots of IF's
Using a cookie (I know, I know...) I plant a cookie in the visitor's browser when he opens the site with a random generated number which expires... -
Delimiter Split
Hello, The string.Split method is very useful for splitting strings that use a single character as a delimiter. i.e.: string str =... -
Regex help (? as delimiter)
Lawrence Tierney wrote: --------------^ No it doesn't. Exchange \d for \w and it matches. -- Gunnar Hjalmarsson -
Tina Mueller #2
Re: Splitting and keeping the delimiter
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:just a note: what if you have:> "12.00 Simpsons",
> "12.30 Seindfeld"
> "13.00 Movie: Dante's Peak"
20:00 24 - 06:00 - 07:00
?
the '06:00 - 07:00' belongs to the title of the program
> 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/> producestry> "Simpsons",
> "Seindfeld"
> "Movie: Dante's Peak"
split / ?(?=\d\d\.\d\d)/
hth, tina
--
[url]http://www.tinita.de/[/url] \ enter__| |__the___ _ _ ___
[url]http://Movies.tinita.de/[/url] \ / _` / _ \/ _ \ '_(_-< of
[url]http://www.perlquotes.de/[/url] \ \ _,_\ __/\ __/_| /__/ perception
- the above mail address expires end of december 2003 -
Tina Mueller Guest
-
Glenn Jackman #3
Re: Splitting and keeping the delimiter
Sandman <mr@sandman.net> wrote:
my $str = "12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak";> 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 $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
[email]glennj@ncf.ca[/email]
Glenn Jackman Guest
-
John W. Krahn #4
Re: Splitting and keeping the delimiter
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
John W. Krahn Guest
-
David Oswald #5
Re: Splitting and keeping the delimiter
"Sandman" <mr@sandman.net> wrote in message
news:mr-66A60F.22295710092003@news.fu-berlin.de...<snip>> 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"
>
> Is there a way? How would you do it?
Yes, there is a way. Actually as with most things in Perl there is more
than one way.
The easiest involves using an assertion that doesn't consume anything.
my $string = "12.00 Simpsons 12.30 Seinfeld 13.00 Movie: Dante's Peak";
@array = split /(?=\d+\.\d+\s)/, $string;
Another possibility would be to come up with pairs, and then join them like
this (untested):
while ($string) {
my ($time,$name,$string) = split /(\d+\.\d+)\s/,$string,3;
push @array, join " ",($time, $name);
}
This relies on paranthesis to capture the delimiter, and counts on the limit
parameter of split to limit its output to three strings; in this case the
delimiter, the name of the show, and a string containing everything else,
ready for the next split call.
Eventually $string is empty, and the while loop runs out.
I like the first method better though.
Dave
David Oswald Guest
-
David K. Wall #6
Re: Splitting and keeping the delimiter
Sandman <mr@sandman.net> 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
David K. Wall Guest
-
Uri Guttman #7
Re: Splitting and keeping the delimiter
>>>>> "DKW" == David K Wall <usenet@dwall.fastmail.fm> 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 ------ [email]uri@stemsystems.com[/email] -------- [url]http://www.stemsystems.com[/url]
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- [url]http://jobs.perl.org[/url]
Damian Conway Class in Boston - Sept 2003 -- [url]http://www.stemsystems.com/class[/url]
Uri Guttman Guest
-
Anno Siegel #8
Re: Splitting and keeping the delimiter
Uri Guttman <uri@stemsystems.com> wrote in comp.lang.perl.misc:
....so much so that the saying is sometimes called "Randal's Rule".>> >>>>> "DKW" == David K Wall <usenet@dwall.fastmail.fm> 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.
Anno Siegel Guest
-
Janek Schleicher #9
Re: Splitting and keeping the delimiter
Sandman wrote at Wed, 10 Sep 2003 22:29:57 +0200:
I wouldn't split it, as it is easier IMHO to describe what you want> 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"
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
Janek Schleicher Guest
-
Sandman #10
Re: Splitting and keeping the delimiter
Thanks for all the suggestions!
--
Sandman[.net]
Sandman Guest



Reply With Quote

