Need help on split-function

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

  1. #1

    Default Need help on split-function

    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 Guest

  2. Similar Questions and Discussions

    1. #39838 [NEW]: split function correctly
      From: lu at icst dot org dot tw Operating system: Fedora Core 3 and 6 PHP version: 5.2.0 PHP Bug Type: Unknown/Other Function...
    2. #39838 [Com]: split function correctly
      ID: 39838 Comment by: judas dot iscariote at gmail dot com Reported By: lu at icst dot org dot tw Status: Open...
    3. javascript split function
      I'm using the split method to put values into an array. When I pass in the argument to the function and then use the split method, the delimiters...
    4. Problem with split function
      Hi all, I am having a problem with the split function. Here is my code: $_ = "123 a 456 b 789 c"; my @words = split /+/, $_; print...
    5. note 33878 added to function.str-split
      str_split for older versions of PHP, it should behave just the same (let's hope that code indentation remains the same ^^). function...
  3. #2

    Default Re: Need help on split-function

    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
    Matthew Browning Guest

  4. #3

    Default Re: Need help on split-function

    On Mon, 15 Sep 2003 11:17:25 +0200
    "Arjen" <mijn_postbak@msn.com> 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 Guest

  5. #4

    Default Re: Need help on split-function

    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" <usenet@NOSPAM.matthewb.org> wrote in message
    news:pan.2003.09.15.10.16.29.11643@NOSPAM.matthewb .org...
    > 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 Guest

  6. #5

    Default Re: Need help on split-function

    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 Guest

  7. #6

    Default Re: Need help on split-function

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

  8. #7

    Default Re: Need help on split-function

    On Mon, 15 Sep 2003 12:56:47 +0200, "Arjen" <mijn_postbak@msn.com>
    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 Guest

  9. #8

    Default Re: Need help on split-function

    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 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