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

a strange idea

Expand Messages
  • Thomas Köhler
    Hi, I just made up my mind about arrays and such... well. We don t need to implement them in vim s scripting language if there s a good way to simulate them.
    Message 1 of 6 , Aug 28, 2000
    • 0 Attachment
      Hi,
      I just made up my mind about arrays and such... well.
      We don't need to implement them in vim's scripting language if there's a
      good way to simulate them. Now, I had this idea:
      simulate an array (or a hashtable) like this:

      We need a few functions (there may be better names for these):
      Add(arrayname,key,value)
      Remove(arrayname,key,value)
      Delete(arrayname)
      Now, Add() adds a value to an array for a given key, Remove() removes an
      entry, Delete() deletes the whole array.
      How is an array to be simulated now?
      I'd suggest like this:
      An array is a function like this:
      fun! Array_arrayname(key)
      if a:key = "key1"
      return "value1"
      elseif a:key = "key2"
      return "value1"
      [...]
      else
      return ""
      endif
      endfun

      Add, Remove and Delete maniplate the according function (hey, this is a
      tricky bit of vim scripting, using exec and such), and you're done.

      Accessing a value works like this:
      let value=Get(arrayname,key)

      Would this make sense? Better ideas?

      (I just wanted to hear some comments before I even think of starting to
      implement this)

      CU,
      Thomas

      --
      Thomas Köhler Email: jean-luc@... | LCARS - Linux
      <>< WWW: http://jeanluc-picard.de | for Computers
      IRC: jeanluc | on All Real
      PGP public key available from Homepage! | Starships
    • Rafael Garcia-Suarez
      ... This would be damn slow, I think. ... I ve another idea (perhaps not a better one): An array (or an hashtable -- I think that hashtables will be more
      Message 2 of 6 , Aug 28, 2000
      • 0 Attachment
        Thomas Köhler wrote:
        >
        > Hi,
        > I just made up my mind about arrays and such... well.
        > We don't need to implement them in vim's scripting language if there's a
        > good way to simulate them. Now, I had this idea:
        > simulate an array (or a hashtable) like this:
        >
        > We need a few functions (there may be better names for these):
        > Add(arrayname,key,value)
        > Remove(arrayname,key,value)
        > Delete(arrayname)
        > Now, Add() adds a value to an array for a given key, Remove() removes an
        > entry, Delete() deletes the whole array.
        > How is an array to be simulated now?
        > I'd suggest like this:
        > An array is a function like this:
        > fun! Array_arrayname(key)
        > if a:key = "key1"
        > return "value1"
        > elseif a:key = "key2"
        > return "value1"
        > [...]
        > else
        > return ""
        > endif
        > endfun
        >
        > Add, Remove and Delete maniplate the according function (hey, this is a
        > tricky bit of vim scripting, using exec and such), and you're done.

        This would be damn slow, I think.

        > Accessing a value works like this:
        > let value=Get(arrayname,key)
        >
        > Would this make sense? Better ideas?

        I've another idea (perhaps not a better one):

        An array (or an hashtable -- I think that hashtables will be more
        useful) is implemented as a string, lets say:
        "#keyone=valueone#keytwo=valuetwo" etc.
        # and = will need to be escaped in keys and values; the escape()
        function does this.

        To lookup a value, a good combination of matchend() and matchstr() or
        strpart() should work.

        Adding a key would be trivial; removing a key would be not too
        difficult.

        What do you think?

        --
        Rafael Garcia-Suarez
      • Ron Aaron
        ... I still think using a buffer as an array is perfect. Each line is directly addressable by number. If you arranged for a key at the beginning of each
        Message 3 of 6 , Aug 29, 2000
        • 0 Attachment
          Rafael Garcia-Suarez <garcia-suarez@...> writes:
          >Thomas K�hler wrote:
          >
          >> Accessing a value works like this:
          >> let value=Get(arrayname,key)
          >>
          >> Would this make sense? Better ideas?
          >
          >I've another idea (perhaps not a better one):
          >
          >An array (or an hashtable -- I think that hashtables will be more
          >useful) is implemented as a string, lets say:
          > "#keyone=valueone#keytwo=valuetwo" etc.
          ># and = will need to be escaped in keys and values; the escape()
          >function does this.
          >
          >To lookup a value, a good combination of matchend() and matchstr() or
          >strpart() should work.
          >
          >Adding a key would be trivial; removing a key would be not too
          >difficult.
          >
          >What do you think?

          I still think using a buffer as an array is perfect. Each line is directly
          addressable by number. If you arranged for a 'key' at the beginning of each
          line you might do something like
          /^key:/
          to find the first matching key. You can do multivalue entries just as you
          have mentioned above.

          Advantages:
          vim already handles buffers well
          buffers have no real size limits
          easy to code

          Disadvantages:
          might be slower than eval stuff (how much?)
          requires using a scratch buffer which should be invisible to user

          Ron
        • Mike Steed
          ... Would this make it impossible for an array element to contain a newline character? ... -- M.
          Message 4 of 6 , Aug 29, 2000
          • 0 Attachment
            > From: Ron Aaron [mailto:ron@...]
            > Sent: Tuesday, August 29, 2000 8:59 am
            > To: garcia-suarez@...
            > Cc: Thomas Köhler
            > Subject: Re: a strange idea
            >
            > I still think using a buffer as an array is perfect. Each
            > line is directly addressable by number. If you arranged for
            > a 'key' at the beginning of each line you might do something
            > like
            > /^key:/
            > to find the first matching key. You can do multivalue
            > entries just as you have mentioned above.
            >
            > Advantages:
            > vim already handles buffers well
            > buffers have no real size limits
            > easy to code
            >
            > Disadvantages:
            > might be slower than eval stuff (how much?)
            > requires using a scratch buffer which should be
            > invisible to user

            Would this make it impossible for an array element to contain a newline
            character?

            >
            > Ron

            --
            M.
          • Ron Aaron
            ... Not necessarily -- do a substitute on the element to convert newlines to some other character (e.g. #1 or whatever). This is a bit of a pain, but makes
            Message 5 of 6 , Aug 29, 2000
            • 0 Attachment
              Mike Steed <MSteed@...> writes:
              >>
              >> I still think using a buffer as an array is perfect. Each
              >> line is directly addressable by number. If you arranged for
              >> a 'key' at the beginning of each line you might do something
              >> like
              >> /^key:/
              >> to find the first matching key. You can do multivalue
              >> entries just as you have mentioned above.
              >
              >Would this make it impossible for an array element to contain a newline
              >character?
              >

              Not necessarily -- do a 'substitute' on the element to convert newlines to
              some other character (e.g. #1 or whatever).

              This is a bit of a pain, but makes it fairly straightforward.

              Ron
            • Zdenek Sekera
              (...back from vac...) ... [...] ... Forgot to suggest taking a looong coffee before you re done ... :-) [...] ... Yes (most things do) and yes (nothing can
              Message 6 of 6 , Sep 4, 2000
              • 0 Attachment
                (...back from vac...)

                Thomas Köhler wrote:
                >
                > Hi,
                > I just made up my mind about arrays and such... well.
                > We don't need to implement them in vim's scripting language if there's a
                > good way to simulate them.

                [...]

                > Add, Remove and Delete maniplate the according function (hey, this is a
                > tricky bit of vim scripting, using exec and such), and you're done.
                >

                Forgot to suggest taking a looong coffee before "you're done" ... :-)

                [...]
                >
                > Would this make sense? Better ideas?
                >

                Yes (most things do) and yes (nothing can replace a real array syntax,
                look at
                any scripting language worth looking...)

                > (I just wanted to hear some comments

                You've got mine...:)

                > before I even think of starting to
                > implement this)

                I've played with similar ideas a lot (as other people did).
                I have an array implemetantion written exclusively in vim functions (any
                number of indices, any index can be either a number or a string (Korn
                calls those associative arrays)), it works but is slow.
                No, I am not trying to solve a system of linear equations with it, just
                simple
                applications for tables and such.
                That's why I am sceptical.
                Can't beat a clean array syntax IMHO. Idem (and more) for readability.

                Good luck!

                ---Zdenek
              Your message has been successfully submitted and would be delivered to recipients shortly.