So I’m making a script and I got a function:
loot()
that returns:
{"3 gold coins"}
{"3 gold coins"}
{"nothing"}
{"6 gold coins", "a hand axe"}
{"12 gold coins", "a hand axe"}
I want that function to be included in a table, and the table should read “a”, “an”, “without a number before the word” = 1
so the table reads and does a count example:
table = {"gold coins"=24,"nothing"=1,"hand axe"=2}
This is the kind of table I’m searching for, but I didn’t have any success doing it at the moment. Like I said before my function loot() returns those messages, these aren’t the only messages that loot reads, but I want to save their number or “a”, “an” as number values for 1, so if it’s “6 gold coins” divides this 6 and increase it in the table when it belongs to the message “gold coins”. I hope you understand my point over here.
The purpose of this is to display the table values so I can do:
table["gold coin"] = 24
or
table = {["item"]="count",}
so I can request the key. I really want a table that increases a key, rather than a regular table, but I just can’t figure out how to do this…
thanks in advance
and if you can explain me every part of it I’ll be really thankful 🙂
some other msgs :
{"11 gold coins", "a leather helmet", "meat", "a spear", "a gold coin"}
{"a gold coin"}
btw this program, which I’m using to do this its very restricted. All I can use for it, it’s under the lua manual 5.1 link : http://www.lua.org/manual/5.1/manual.html
metatables aren’t an option :/
The first thing you need is a function that extracts the numbers and items names from strings. The following one will read things like “a spear” or “11 spears” and return 1, “spear” and 11, “spear”.
Then you need to accumulate those values. This should work:
A couple warnings: