I'd like to make some associative tables e.g. table={x=7,y=90,z=40}
but I want to save tokens by using a string because I'm going to have a lot of them, and make a function that returns a table based on whatever is in the string.
So something like:
string1="x=7,y=90,z=40" string2="empty=100, dinner=casserole, transport=horse, weight=150" table1=assoc_table(string1) table2=assoc_table(string2) ?table1.z -- 40 ?table2.transport -- horse |
how do I go about this? split
wont work. what does my function assoc_table(str)
look like?
There are many options, but I use HTBL()
.
https://www.lexaloffle.com/bbs/?tid=36325
string1=htbl"x=7;y=90;z=40;" string2=htbl"empty=100; dinner=casserole; transport=horse; weight=150;" |
You can also define global variables collectively by using CAT()
to join _ENV
tables.
https://www.lexaloffle.com/bbs/?tid=41798
cat(_ENV,htbl[[ string1{x=7;y=90;z=40;} string2{empty=100; dinner=casserole; transport=horse; weight=150;} ]]) |
you are right, split is the correct starting point - idea is to use 2 split!
— split each key/value local tmp=split(“x=7,foo=bar”) local out={} for kv in all(tmp) do — split a pair + extract table values with unpack local k,v=unpack(split(kv,”=“)) out[k]=v end |
@shiftalow, @freds72,
I'm going to use both of your suggestions and play with them because freds is more what I was expecting (although I didn't know about unpack() and I can't find it in the manual...) but also the cart that shiftalow linked is just too cool and perfect not to give a try. Thanks a bunch!
p.s. shiftalow, I'm amazed you wrote that cart 5 years ago and found my question so quickly :)
[Please log in to post a comment]