Need help on split-function

Discussion in 'Perl Misc' started by Arjen, Sep 15, 2003.

  1. Arjen

    Arjen Guest

    Hi All,

    What I want to is using a string as PATTERN in a split function. This makes
    it possible for me to change the PATTERN on one place in my script...

    For example:
    $separator = ";";
    $line = "field1;value1";
    local($field, $value) = split(/$separator/, $line);

    How can I make this work ?

    Arjen
     
    Arjen, Sep 15, 2003
    #1
    1. Advertising

  2. Arjen

    Thens Guest

    On Mon, 15 Sep 2003 11:17:25 +0200
    "Arjen" <> wrote:

    # Hi All,
    #
    # What I want to is using a string as PATTERN in a split function. This makes
    # it possible for me to change the PATTERN on one place in my script...
    #
    # For example:
    # $separator = ";";
    # $line = "field1;value1";
    # local($field, $value) = split(/$separator/, $line);

    I dont know why this will fail. But you can also quote the
    meta-characters in your PATTERN using \Q and \E if they have any.

    $separator = "++";
    $line = "field1++value1";
    local($field, $value) = split(/\Q$separator\E/, $line);

    Regards,
    Thens.
     
    Thens, Sep 15, 2003
    #2
    1. Advertising

  3. Arjen

    Arjen Guest

    Hi Matthew,

    You are right. I will give an example:

    Code:
    $line = "field1|value1";
    $separator = "|";
    local($field, $value) = split(/$separator/, $line);

    Gives back:
    $field = f
    $value= v

    Code:
    $line = "field1^value1";
    $separator = "^";
    local($field, $value) = split(/$separator/, $line);

    Gives back:
    $field = field1^value1
    $value=

    Code:
    $line = "field1;value1";
    $separator = ";";
    local($field, $value) = split(/$separator/, $line);

    Gives back:
    $field = field1
    $value= value1

    The last example is working right. The others are not. Even not when i'm
    using "\|" instead of "|" for the seperator.

    Arjen


    "Matthew Browning" <> wrote in message
    news:p...
    > On Mon, 15 Sep 2003 11:17:25 +0200, Arjen wrote:
    > >
    > > For example:
    > > $separator = ";";
    > > $line = "field1;value1";
    > > local($field, $value) = split(/$separator/, $line);
    > >
    > > How can I make this work ?

    >
    >
    > In what sense exactly does that not work?
    >
    >
    > Matthew Browning
     
    Arjen, Sep 15, 2003
    #3
  4. Arjen wrote:
    [snipped]

    You won't believe it but a namesake of yours just posted exactly the same
    question in CLP where I just happened to answer it.
    You may want to check there.

    And you also may want to read the nettiquette why multiposting will put you
    on the fast path into many killfiles.

    jue
     
    Jürgen Exner, Sep 15, 2003
    #4
  5. Arjen

    Arjen Guest


    > You won't believe it but a namesake of yours just posted exactly the same
    > question in CLP where I just happened to answer it.
    > You may want to check there.
    >
    > And you also may want to read the nettiquette why multiposting will put

    you
    > on the fast path into many killfiles.
    >
    > jue


    I'm really sorry, I didn't know that I should not post this message in more
    then one newsgroup. Or is it because comp.lang.perl and comp.lang.perl.misc
    are related that I shouldn't post in both ?!

    Arjen
     
    Arjen, Sep 15, 2003
    #5
  6. Arjen

    Helgi Briem Guest

    On Mon, 15 Sep 2003 12:56:47 +0200, "Arjen" <>
    wrote:

    >I'm really sorry, I didn't know that I should not post this message
    >in more ten one newsgroup. Or is it because comp.lang.perl and
    >omp.lang.perl.misc are related that I shouldn't post in both ?!


    Comp.lang.perl is extinct. It doesn't exist except on
    misconfigured servers. You should never ever post
    to it.

    Cross-posting is OK, but in moderation and only
    with very good cause. Multi-posting is a definite
    no-no.
    --
    Helgi Briem hbriem AT simnet DOT is

    Excuses the munged address. My last
    e-mail address was killed by spammers.
     
    Helgi Briem, Sep 15, 2003
    #6
  7. On Mon, Sep 15, Helgi Briem inscribed on the eternal scroll:

    > Comp.lang.perl is extinct. It doesn't exist except on
    > misconfigured servers. You should never ever post
    > to it.


    Having a regular posting on comp.lang.perl to warn people that it
    doesn't really exist (and with an authoritative link to the original
    control message which should have rmgroup-ed it) would seem to be a
    self-contradictory thing to do, but I'm not sure how else the message
    is ever going to get out. :-{
     
    Alan J. Flavell, Sep 15, 2003
    #7
    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. Arjen

    Need help on split-function

    Arjen, Sep 15, 2003, in forum: Perl
    Replies:
    5
    Views:
    482
    P Payne
    Sep 17, 2003
  2. Replies:
    2
    Views:
    492
  3. Carlos Ribeiro
    Replies:
    11
    Views:
    738
    Alex Martelli
    Sep 17, 2004
  4. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    238
    Florian Gross
    Dec 28, 2004
  5. Sam Kong
    Replies:
    5
    Views:
    277
    Rick DeNatale
    Aug 12, 2006
Loading...

Share This Page