Splitting and keeping the delimiter

Discussion in 'Perl Misc' started by Sandman, Sep 10, 2003.

  1. Sandman

    Sandman Guest

    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, Sep 10, 2003
    #1
    1. Advertising

  2. 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"


    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
    Glenn Jackman, Sep 10, 2003
    #2
    1. Advertising

  3. 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, Sep 10, 2003
    #3
  4. 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"



    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, Sep 11, 2003
    #4
  5. Sandman

    Uri Guttman Guest

    >>>>> "DKW" == David K Wall <> 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 ------ -------- 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
    Uri Guttman, Sep 11, 2003
    #5
  6. Sandman

    Anno Siegel Guest

    Uri Guttman <> wrote in comp.lang.perl.misc:
    > >>>>> "DKW" == David K Wall <> 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, Sep 11, 2003
    #6
  7. 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, Sep 11, 2003
    #7
  8. Sandman

    Sandman Guest

    Thanks for all the suggestions!

    --
    Sandman[.net]
    Sandman, Sep 12, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bruno Desthuilliers
    Replies:
    8
    Views:
    316
    Hendrik van Rooyen
    Jul 26, 2007
  2. Prasanth
    Replies:
    4
    Views:
    396
    Prasanth
    Nov 22, 2008
  3. MRAB
    Replies:
    3
    Views:
    368
  4. Ajithkumar Warrier

    Splitting a sentence with delimiter preserved

    Ajithkumar Warrier, Oct 17, 2006, in forum: Ruby
    Replies:
    0
    Views:
    89
    Ajithkumar Warrier
    Oct 17, 2006
  5. Sandman

    Splitting and keeping key/value

    Sandman, Sep 26, 2006, in forum: Perl Misc
    Replies:
    17
    Views:
    170
    Sandman
    Sep 27, 2006
Loading...

Share This Page