Calling data from table keys with long key names.

Posted by Falgor on Sun 05 Feb 2017 05:00 PM — 7 posts, 27,544 views.

#0
I am building a feature to call up individual armour strength and value from a table. I want each table key to be the "name" of that armour, this is so it will be more efficient with triggers to capture what I am wearing, for example (trigger is just an example on what i'll match, not a real trigger):


A shield (worn).
A helmet (worn).

<trigger>
match = "^\W+. \(worn\)\."
regex = "y"
pattern = "
wearing = wildcards[1]
print(armour.wearing.value)
"
<\trigger>


The above just demonstrates why I want to use multi-worded key values, the issue I'm having is the print function won't read the "wearing" string as a table key.

Here is my table:


armour = {}
armour = {
          ["A shield"] = { strength = 100, value = "200" },
          ["A helmet"] = [ strength = 200, value = "150" }
         }


When I try to print a value from the shield or helmet key, it comes up saying the key has a nil value. Here is what I've tried so far:


print(armour.A shield.value)
print(armour."A shield".value)
print(armour.["A shield"].value)

name = "A shield"
print(armour.name.value)

for k,v in pairs(armour) do
print(armour.k.value)
end


Help please, as always thank you in advance.

P.S Am I better off making an excel/access database and setting up a COM object to call the data in from there?


Amended on Sun 05 Feb 2017 05:01 PM by Falgor
Australia Forum Administrator #1

print(armour.wearing.value)


Your problem here is that "wearing" is being taken literally.

In other words, it is looking for armour called "wearing".

What you want is:


print(armour [wearing].value)


Now that uses the contents of the variable "wearing".

The word "value" is OK because you literally have "value" as a key.

In your loop above this works:


for k,v in pairs(armour) do
  print(armour [k].value)
end
Amended on Sun 05 Feb 2017 09:25 PM by Nick Gammon
Australia Forum Administrator #2
Falgor said:

P.S Am I better off making an excel/access database and setting up a COM object to call the data in from there?


No, that would be slower and more complex. What you might do is set up an SQLite3 database (the functions for doing that are built into the client).

That could be handy if you want so share this data between multiple worlds (eg. multiple characters).

For examples of that see http://www.gammon.com.au/db and http://www.gammon.com.au/sql
#3
Thanks Nick!

This is exactly why I came back to MUSHclient from years with zMUD and cMUD! No more stupid bugs and a still developing and supported client.
#4
On the subject of tables, do they save between sessions? My armour table is going to be large, am I best going down the SQL route?
Australia Forum Administrator #5
Not automatically, however see this:

http://www.gammon.com.au/forum/?id=4960

If you use SQL writes happen immediately, if you serialize a table it is only written when you save the world.
#6
Thanks, next up the mapper! :)