On 13/07/10 15:18, epanda wrote:
Hi,

Is it possible to declare a dynamic key in a dictionary ?

eg :

let mycounter = 0
let mycounter += 1


let myDict = { mycounter : "foo" }
let mycounter += 1

the above works. myDict is now {'1': 'foo'}


let myDict += { mycounter : "foo" }

but this doesn't (the += operator is only defined for Numbers or Floats). What you should use is (using a different value for the new key):

        :let myDict[mycounter] = "bar"
or
        :call extend(myDict, { mycounter : "bar" })

which both result in
        :echo myDict
{'0': 'foo', '1': 'bar'}


There is no "dynamic key" involved: the key stored in the dictionary is the value of the variable at the time the key-value pair was stored. You can just as well retrieve the value corresponding to a key held in a variable, as in

        :let value = dictionary[key]

where value and key are String variables (or integer variables which Vim will use as if Strings) while dictionary is a Dictionary variable.



Thanks


Best regards,
Tony.
--
"A raccoon tangled with a 23,000 volt line today.  The results blacked
out 1400 homes and, of course, one raccoon."
                -- Steel City News

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Reply via email to