Need help on split-function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arjen

    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


  • Jürgen Exner

    #2
    Re: Need help on split-function

    Arjen wrote:[color=blue]
    > 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 ?[/color]

    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


    Comment

    • Arjen

      #3
      Re: Need help on split-function

      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" <jurgenex@hotma il.com> wrote in message
      news:Y5g9b.2749 0$pd5.19207@nwr ddc03.gnilink.n et...[color=blue]
      > Arjen wrote:[color=green]
      > > 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 ?[/color]
      >
      > 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[/color]
      behaviour?[color=blue]
      > Without this information it is impossible to guess what you mean by "make[/color]
      it[color=blue]
      > work".
      >
      > jue
      >
      >[/color]


      Comment

      • Jürgen Exner

        #4
        Re: Need help on split-function

        [Do not top post! Rearranged into chronological order]
        Arjen wrote:[color=blue]
        > "Jürgen Exner" <jurgenex@hotma il.com> wrote in message
        > news:Y5g9b.2749 0$pd5.19207@nwr ddc03.gnilink.n et...[color=green]
        >> Arjen wrote:[color=darkred]
        >>> 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...[/color][/color][/color]
        [...][color=blue]
        > 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.[/color]

        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



        Comment

        • Arjen

          #5
          Re: Need help on split-function

          Thanx for you replies !

          Arjen

          "Jürgen Exner" <jurgenex@hotma il.com> wrote in message
          news:4Jg9b.1015 9$1D5.1602@nwrd dc02.gnilink.ne t...[color=blue]
          > [Do not top post! Rearranged into chronological order]
          > Arjen wrote:[color=green]
          > > "Jürgen Exner" <jurgenex@hotma il.com> wrote in message
          > > news:Y5g9b.2749 0$pd5.19207@nwr ddc03.gnilink.n et...[color=darkred]
          > >> 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...[/color][/color]
          > [...][color=green]
          > > 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.[/color]
          >
          > 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[/color]
          the[color=blue]
          > vertical bar.
          > Some solutions:
          > my $separator = "\\|";
          > my $separator = '\|';
          > my $separator = quotemeta ("|");
          >
          > jue
          >
          >
          >[/color]


          Comment

          • P Payne

            #6
            Re: Need help on split-function

            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 <mijn_postbak@m sn.com> wrote:[color=blue]
            > 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...[/color]
            [color=blue]
            > For example:
            > $separator = ";";
            > $line = "field1;value1" ;
            > local($field, $value) = split(/$separator/, $line);[/color]


            Comment

            Working...