Need help on split-function

Discussion in 'Perl' 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 wrote:
    > 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 ?


    You don't tell us which part of your code is "not working". What is the
    expected behaviour and how does it compare to the actual observed behaviour?
    Without this information it is impossible to guess what you mean by "make it
    work".

    jue
     
    Jürgen Exner, Sep 15, 2003
    #2
    1. Advertising

  3. Arjen

    Arjen Guest

    Hi Jurgen,

    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


    "Jürgen Exner" <> wrote in message
    news:Y5g9b.27490$...
    > Arjen wrote:
    > > 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 ?

    >
    > You don't tell us which part of your code is "not working". What is the
    > expected behaviour and how does it compare to the actual observed

    behaviour?
    > Without this information it is impossible to guess what you mean by "make

    it
    > work".
    >
    > jue
    >
    >
     
    Arjen, Sep 15, 2003
    #3
  4. [Do not top post! Rearranged into chronological order]
    Arjen wrote:
    > "Jürgen Exner" <> wrote in message
    > news:Y5g9b.27490$...
    >> Arjen wrote:
    >>> 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...

    [...]
    > 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.


    You got the right idea, but didn't follow through all the way.
    Yes, | and ^ are special characters in a RE and need to be escaped with a
    backslash when you want them to match the literal characters.

    But when you define them in a double quoted string as in "\|" or "\^" then
    Perl will notice that there is no special character in that string (those
    characters are not special in strings, \t or \n are) and simply throw away
    the backslash. That means "|" and "\|" are two different notations for the
    same string.

    What you want is a string that actually contains the backslash and then the
    vertical bar.
    Some solutions:
    my $separator = "\\|";
    my $separator = '\|';
    my $separator = quotemeta ("|");

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

    Arjen Guest

    Thanx for you replies !

    Arjen

    "Jürgen Exner" <> wrote in message
    news:4Jg9b.10159$...
    > [Do not top post! Rearranged into chronological order]
    > Arjen wrote:
    > > "Jürgen Exner" <> wrote in message
    > > news:Y5g9b.27490$...
    > >> Arjen wrote:
    > >>> 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...

    > [...]
    > > 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.

    >
    > You got the right idea, but didn't follow through all the way.
    > Yes, | and ^ are special characters in a RE and need to be escaped with a
    > backslash when you want them to match the literal characters.
    >
    > But when you define them in a double quoted string as in "\|" or "\^" then
    > Perl will notice that there is no special character in that string (those
    > characters are not special in strings, \t or \n are) and simply throw away
    > the backslash. That means "|" and "\|" are two different notations for the
    > same string.
    >
    > What you want is a string that actually contains the backslash and then

    the
    > vertical bar.
    > Some solutions:
    > my $separator = "\\|";
    > my $separator = '\|';
    > my $separator = quotemeta ("|");
    >
    > jue
    >
    >
    >
     
    Arjen, Sep 15, 2003
    #5
  6. Arjen

    P Payne Guest

    What you want is to split on a literal string. The split function
    uses a regular expression, thus when you use separators with special
    characters such as '|' or the like you will get splits that may
    appear different to the results you expected.

    Short answer, replace the split function with:
    split( /\Q$separator\E/, $line );

    The \Q...\E combination forces the regular expression to search
    for quoted text that won't be interpreted as a regular expression
    (I know that doesn't make sense, but just do it an it will work).

    Regards,
    Peter

    Arjen <> wrote:
    > 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);
     
    P Payne, Sep 17, 2003
    #6
    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. Replies:
    2
    Views:
    491
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    735
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

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

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

    Need help on split-function

    Arjen, Sep 15, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    88
    Alan J. Flavell
    Sep 15, 2003
Loading...

Share This Page