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

Re: split() behavior and empty items

Expand Messages
  • Bram Moolenaar
    ... I now wonder why empty items are removed at all. This only makes sense at the start and end. Empty items halfway would not occur if you choose the
    Message 1 of 1 , May 20, 2005
    • 0 Attachment
      Johnny Blaze wrote:

      > Can you make split()'s behavior of omitting empty items optional?
      >
      > An example of why this might be useful:
      >
      > let lines = readfile('/etc/passwd')
      > for line in lines
      > let fields = split(line, ':')
      > echo "Home Directory: " . fields[9] " guessing
      > endfor
      >
      > If there are blank fields (full name, office, etc), the number is not
      > fixed..
      >
      > I have tried being tricky with \ze and \zs ('\ze:'), but it will produce
      > list items with the separator... how about split(string, [pattern],
      > [empties])

      I now wonder why empty items are removed at all. This only makes sense
      at the start and end. Empty items halfway would not occur if you choose
      the separation pattern properly.

      The password file contains lines like:

      toor:*:0:0:Bourne-again Superuser:/root:
      operator:*:2:5::/:/usr/sbin/nologin
      :*:8:9:bogus entry:/:/usr/sbin/nologin

      In this case you don't want to omit any empty field, otherwise the item
      count changes.

      When splitting a line into words you probably do want to omit empty
      items. Especially when there is white space at the start or end.

      So how about this: The default would be to omit empty strings at the
      start and end. With an optional argument you can indicate to keep
      everything.

      --
      Biting someone with your natural teeth is "simple assault," while biting
      someone with your false teeth is "aggravated assault."
      [real standing law in Louisana, United States of America]

      /// Bram Moolenaar -- Bram@... -- http://www.Moolenaar.net \\\
      /// Sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
      \\\ Project leader for A-A-P -- http://www.A-A-P.org ///
      \\\ Buy LOTR 3 and help AIDS victims -- http://ICCF.nl/lotr.html ///
    Your message has been successfully submitted and would be delivered to recipients shortly.