Splitting and keeping the delimiter

Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default 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

  2. Similar Questions and Discussions

    1. #39306 [NEW]: Changing Delimiter
      From: ctingley at mediagrids dot com Operating system: Windows PHP version: 5.1.6 PHP Bug Type: MySQL related Bug...
    2. 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...
    3. [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...
    4. 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 =...
    5. Regex help (? as delimiter)
      Lawrence Tierney wrote: --------------^ No it doesn't. Exchange \d for \w and it matches. -- Gunnar Hjalmarsson
  3. #2

    Default 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"
    just a note: what if you have:
    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/
    > produces
    > "Simpsons",
    > "Seindfeld"
    > "Movie: Dante's Peak"
    try
    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

  4. #3

    Default 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"
    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
    [email]glennj@ncf.ca[/email]
    Glenn Jackman Guest

  5. #4

    Default 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

  6. #5

    Default Re: Splitting and keeping the delimiter


    "Sandman" <mr@sandman.net> wrote in message
    news:mr-66A60F.22295710092003@news.fu-berlin.de...
    > 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"
    >
    <snip>
    > 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

  7. #6

    Default 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

  8. #7

    Default 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

  9. #8

    Default Re: Splitting and keeping the delimiter

    Uri Guttman <uri@stemsystems.com> wrote in comp.lang.perl.misc:
    > >>>>> "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.
    ....so much so that the saying is sometimes called "Randal's Rule".
    Anno Siegel Guest

  10. #9

    Default Re: Splitting and keeping the delimiter

    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
    Janek Schleicher Guest

  11. #10

    Default Re: Splitting and keeping the delimiter

    Thanks for all the suggestions!

    --
    Sandman[.net]
    Sandman Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139