Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Due to spam on this forum, all posts now need moderator approval.
Entire forum
➜ MUSHclient
➜ Lua
➜ Calling data from table keys with long key names.
Calling data from table keys with long key names.
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Posted by
| Falgor
(36 posts) Bio
|
Date
| Sun 05 Feb 2017 05:00 PM (UTC) Amended on Sun 05 Feb 2017 05:01 PM (UTC) by Falgor
|
Message
| 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?
| Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #1 on Sun 05 Feb 2017 09:22 PM (UTC) Amended on Sun 05 Feb 2017 09:25 PM (UTC) by Nick Gammon
|
Message
|
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
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #2 on Sun 05 Feb 2017 09:51 PM (UTC) |
Message
|
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 |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Falgor
(36 posts) Bio
|
Date
| Reply #3 on Mon 06 Feb 2017 09:14 AM (UTC) |
Message
| 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. | Top |
|
Posted by
| Falgor
(36 posts) Bio
|
Date
| Reply #4 on Mon 06 Feb 2017 03:59 PM (UTC) |
Message
| 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? | Top |
|
Posted by
| Nick Gammon
Australia (23,158 posts) Bio
Forum Administrator |
Date
| Reply #5 on Mon 06 Feb 2017 08:40 PM (UTC) |
Message
| 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. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Falgor
(36 posts) Bio
|
Date
| Reply #6 on Tue 07 Feb 2017 09:51 AM (UTC) |
Message
| Thanks, next up the mapper! :) | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
20,562 views.
It is now over 60 days since the last post. This thread is closed.
Refresh page
top