Loading ...
Sorry, an error occurred while loading the content.

vim7: changed behavior of split()

Expand Messages
  • Mikolaj Machowski
    Hello, ... Is really an empty string such special case it is treated differently than other cases? m.
    Message 1 of 8 , Oct 5, 2005
    • 0 Attachment
      Hello,

      Changed behaviour of split(). Try::

      :echo split("")[0]
      :echo split("asdf")[0]
      :echo split("asdf qwer")[0]

      Is really an empty string such special case it is treated differently
      than other cases?

      m.
    • A. J. Mechelynck
      ... [] ... [ asdf ] ... [ adsf , qwer ] ... E684: list index out of range: 0 E15: invalid expression: split( )[0] ... asdf ... asdf Explanation: - split( )
      Message 2 of 8 , Oct 5, 2005
      • 0 Attachment
        Mikolaj Machowski wrote:
        > Hello,
        >
        > Changed behaviour of split(). Try::
        >
        > :echo split("")[0]
        > :echo split("asdf")[0]
        > :echo split("asdf qwer")[0]
        >
        > Is really an empty string such special case it is treated differently
        > than other cases?
        >
        > m.
        >
        >
        >
        >
        I don't see anything wrong:

        :echo split("")
        []
        :echo split("asdf")
        ['asdf']
        :echo split("asdf qwer")
        ['adsf', 'qwer']
        :echo split("")[0]
        E684: list index out of range: 0
        E15: invalid expression: split("")[0]
        :echo split("asdf")[0]
        asdf
        :echo split("asdf qwer")[0]
        asdf

        Explanation:

        - split("") is the empty list, it has no element. It's first element
        (element 0) doesn't exist.
        - split("asdf") has one element, numbered zero.
        - split("asdf qwer") has two elements, numbered zero and one.

        If you were to ask for element 2 (or higher) of any of those lists, or
        for split("asdf")[1], you would get the same errors, for the same
        reason: list index out of range.

        In general, when counting from the left, a list index must be less than
        the list length and not less than zero. When counting from the right, it
        must be negative, and its absolute value must not be greater than the
        list length. Note that (in a list of two or more elements) list[1] is
        the second element from the left, but list[-2] is the second from right.

        See also ":help E684".

        Best regards,
        Tony.
      • James Vega
        ... I think Mikolaj was suggesting that split( ) should return [ ], similar to how split( asdf ) returns [ asdf ]. James -- GPG Key: 1024D/61326D40
        Message 3 of 8 , Oct 5, 2005
        • 0 Attachment
          On Wed, Oct 05, 2005 at 08:35:04PM +0200, A. J. Mechelynck wrote:
          > Mikolaj Machowski wrote:
          > > Hello,
          > >
          > > Changed behaviour of split(). Try::
          > >
          > > :echo split("")[0]
          > > :echo split("asdf")[0]
          > > :echo split("asdf qwer")[0]
          > >
          > > Is really an empty string such special case it is treated differently
          > > than other cases?
          > >
          > > m.
          > >
          > I don't see anything wrong:
          >
          > :echo split("")
          > []
          > :echo split("asdf")
          > ['asdf']
          > :echo split("asdf qwer")
          > ['adsf', 'qwer']
          > :echo split("")[0]
          > E684: list index out of range: 0
          > E15: invalid expression: split("")[0]
          > :echo split("asdf")[0]
          > asdf
          > :echo split("asdf qwer")[0]
          > asdf
          >
          > Explanation:
          >
          > - split("") is the empty list, it has no element. It's first element
          > (element 0) doesn't exist.

          I think Mikolaj was suggesting that split("") should return [""],
          similar to how split("asdf") returns ["asdf"].

          James
          --
          GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@...>
        • A. J. Mechelynck
          ... split( ) returns a list of zero elements split( asdf ) returns a list of one element split( asdf qwer ) returns a list of two elements split( asdf qwer
          Message 4 of 8 , Oct 5, 2005
          • 0 Attachment
            James Vega wrote:
            > On Wed, Oct 05, 2005 at 08:35:04PM +0200, A. J. Mechelynck wrote:
            >> Mikolaj Machowski wrote:
            >>> Hello,
            >>>
            >>> Changed behaviour of split(). Try::
            >>>
            >>> :echo split("")[0]
            >>> :echo split("asdf")[0]
            >>> :echo split("asdf qwer")[0]
            >>>
            >>> Is really an empty string such special case it is treated differently
            >>> than other cases?
            >>>
            >>> m.
            >>>
            >> I don't see anything wrong:
            >>
            >> :echo split("")
            >> []
            >> :echo split("asdf")
            >> ['asdf']
            >> :echo split("asdf qwer")
            >> ['adsf', 'qwer']
            >> :echo split("")[0]
            >> E684: list index out of range: 0
            >> E15: invalid expression: split("")[0]
            >> :echo split("asdf")[0]
            >> asdf
            >> :echo split("asdf qwer")[0]
            >> asdf
            >>
            >> Explanation:
            >>
            >> - split("") is the empty list, it has no element. It's first element
            >> (element 0) doesn't exist.
            >
            > I think Mikolaj was suggesting that split("") should return [""],
            > similar to how split("asdf") returns ["asdf"].
            >
            > James
            > --
            > GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@...>

            split("") returns a list of zero elements
            split("asdf") returns a list of one element
            split("asdf qwer") returns a list of two elements
            split("asdf qwer uiop") returns a list of three elements
            split("asdf qwer uiop hjkl") returns a list of four elements
            etc.

            Returning a list of one empty element, rather than zero element, _that_
            would be "special" and "abnormal" handling. Or else split() should
            return an additional empty element at the end of the list in all cases,
            but there is no need for that since we can test l(list).

            If you want to avoid errors at all costs without testing l(list), read
            the help text I mentioned in the text you snipped (":help E684") which
            mentions the get() function.

            Best regards,
            Tony.
          • A. J. Mechelynck
            ... Oops ... Tony.
            Message 5 of 8 , Oct 5, 2005
            • 0 Attachment
              A. J. Mechelynck wrote:
              > James Vega wrote:
              >> On Wed, Oct 05, 2005 at 08:35:04PM +0200, A. J. Mechelynck wrote:
              >>> Mikolaj Machowski wrote:
              >>>> Hello,
              >>>>
              >>>> Changed behaviour of split(). Try::
              >>>>
              >>>> :echo split("")[0]
              >>>> :echo split("asdf")[0]
              >>>> :echo split("asdf qwer")[0]
              >>>>
              >>>> Is really an empty string such special case it is treated differently
              >>>> than other cases?
              >>>>
              >>>> m.
              >>>>
              >>> I don't see anything wrong:
              >>>
              >>> :echo split("")
              >>> []
              >>> :echo split("asdf")
              >>> ['asdf']
              >>> :echo split("asdf qwer")
              >>> ['adsf', 'qwer']
              >>> :echo split("")[0]
              >>> E684: list index out of range: 0
              >>> E15: invalid expression: split("")[0]
              >>> :echo split("asdf")[0]
              >>> asdf
              >>> :echo split("asdf qwer")[0]
              >>> asdf
              >>>
              >>> Explanation:
              >>>
              >>> - split("") is the empty list, it has no element. It's first element
              >>> (element 0) doesn't exist.
              >> I think Mikolaj was suggesting that split("") should return [""],
              >> similar to how split("asdf") returns ["asdf"].
              >>
              >> James
              >> --
              >> GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@...>
              >
              > split("") returns a list of zero elements
              > split("asdf") returns a list of one element
              > split("asdf qwer") returns a list of two elements
              > split("asdf qwer uiop") returns a list of three elements
              > split("asdf qwer uiop hjkl") returns a list of four elements
              > etc.
              >
              > Returning a list of one empty element, rather than zero element, _that_
              > would be "special" and "abnormal" handling. Or else split() should
              > return an additional empty element at the end of the list in all cases,
              > but there is no need for that since we can test l(list).
              >
              > If you want to avoid errors at all costs without testing l(list), read
              > the help text I mentioned in the text you snipped (":help E684") which
              > mentions the get() function.
              >
              > Best regards,
              > Tony.
              >
              >
              >
              >

              Oops

              :s/l(list)/len(list)/g

              Tony.
            • James Vega
              ... is a string with no whitespace just as asdf is a string with no whitespace. I hadn t actually looked at the help for or used Vim s split() before, so
              Message 6 of 8 , Oct 5, 2005
              • 0 Attachment
                On Wed, Oct 05, 2005 at 09:20:28PM +0200, A. J. Mechelynck wrote:
                > James Vega wrote:
                > > On Wed, Oct 05, 2005 at 08:35:04PM +0200, A. J. Mechelynck wrote:
                > >> Mikolaj Machowski wrote:
                > >>> Hello,
                > >>>
                > >>> Changed behaviour of split(). Try::
                > >>>
                > >>> :echo split("")[0]
                > >>> :echo split("asdf")[0]
                > >>> :echo split("asdf qwer")[0]
                > >>>
                > >>> Is really an empty string such special case it is treated differently
                > >>> than other cases?
                > >>>
                > >>> m.
                > >>>
                > >> I don't see anything wrong:
                > >>
                > >> :echo split("")
                > >> []
                > >> :echo split("asdf")
                > >> ['asdf']
                > >> :echo split("asdf qwer")
                > >> ['adsf', 'qwer']
                > >> :echo split("")[0]
                > >> E684: list index out of range: 0
                > >> E15: invalid expression: split("")[0]
                > >> :echo split("asdf")[0]
                > >> asdf
                > >> :echo split("asdf qwer")[0]
                > >> asdf
                > >>
                > >> Explanation:
                > >>
                > >> - split("") is the empty list, it has no element. It's first element
                > >> (element 0) doesn't exist.
                > >
                > > I think Mikolaj was suggesting that split("") should return [""],
                > > similar to how split("asdf") returns ["asdf"].
                > >
                > > James
                > > --
                > > GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@...>
                >
                > split("") returns a list of zero elements
                > split("asdf") returns a list of one element
                > split("asdf qwer") returns a list of two elements
                > split("asdf qwer uiop") returns a list of three elements
                > split("asdf qwer uiop hjkl") returns a list of four elements
                > etc.
                >
                > Returning a list of one empty element, rather than zero element, _that_
                > would be "special" and "abnormal" handling. Or else split() should
                > return an additional empty element at the end of the list in all cases,
                > but there is no need for that since we can test l(list).

                "" is a string with no whitespace just as "asdf" is a string with no
                whitespace. I hadn't actually looked at the help for or used Vim's
                split() before, so I was basing my expectations on other split
                implementations. From that reference point, this behavior is
                "abnormal". The help does state, though, that empty elements are
                dropped unless {keepempty} is non-zero.

                :echo split("", "", 1)
                ['']

                James
                --
                GPG Key: 1024D/61326D40 2003-09-02 James Vega <jamessan@...>
              • Mikolaj Machowski
                ... OK. I was asking because previously split( ) was returning [ ]. m.
                Message 7 of 8 , Oct 5, 2005
                • 0 Attachment
                  Dnia środa, 5 października 2005 21:20, A. J. Mechelynck napisał:
                  >
                  > split("") returns a list of zero elements
                  > split("asdf") returns a list of one element
                  > split("asdf qwer") returns a list of two elements
                  > split("asdf qwer uiop") returns a list of three elements
                  > split("asdf qwer uiop hjkl") returns a list of four elements
                  > etc.
                  >
                  > Returning a list of one empty element, rather than zero element, _that_
                  > would be "special" and "abnormal" handling. Or else split() should
                  > return an additional empty element at the end of the list in all cases,
                  > but there is no need for that since we can test l(list).

                  OK. I was asking because previously split("") was returning [""].

                  m.
                • A. J. Mechelynck
                  ... [ asdf ] ... [ , asdf ] ... [ asdf , ] ... [ , asdf , ] See :help split() . Best regards, Tony.
                  Message 8 of 8 , Oct 5, 2005
                  • 0 Attachment
                    Mikolaj Machowski wrote:
                    > Dnia środa, 5 października 2005 21:20, A. J. Mechelynck napisał:
                    >> split("") returns a list of zero elements
                    >> split("asdf") returns a list of one element
                    >> split("asdf qwer") returns a list of two elements
                    >> split("asdf qwer uiop") returns a list of three elements
                    >> split("asdf qwer uiop hjkl") returns a list of four elements
                    >> etc.
                    >>
                    >> Returning a list of one empty element, rather than zero element, _that_
                    >> would be "special" and "abnormal" handling. Or else split() should
                    >> return an additional empty element at the end of the list in all cases,
                    >> but there is no need for that since we can test l(list).
                    >
                    > OK. I was asking because previously split("") was returning [""].
                    >
                    > m.


                    As was said not very long ago, to get [""], use split("","",1). But beware:

                    :echo split("asdf","",1)
                    ['asdf']
                    :echo split(" asdf","",1)
                    ['', 'asdf']
                    :echo split("asdf ","",1)
                    ['asdf', '']
                    :echo split(" asdf ","",1)
                    ['', 'asdf', '']

                    See ":help split()".

                    Best regards,
                    Tony.
                  Your message has been successfully submitted and would be delivered to recipients shortly.