Register forum user name Search FAQ

Gammon Forum

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 ➜ Lua Table

Lua Table

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Tkl1129   Hong Kong  (43 posts)  Bio
Date Wed 10 Aug 2011 02:26 AM (UTC)
Message
I'm try to use Lua table instead of SQLite, I've got a problem.

Case here:
I wanna search for some specific key inside.


require("tprint")

Map = {}
	Map.sz = {}
		Map.sz.sz01 = {	Code = "sz01",
						Area = "sz",
						Location = "Tower",
						CP = 1,
						Range = 3,
					}
		Map.sz.sz02 = {	Code = "sz02",
						Area = "sz",
						Location = "Hotel",
						CP = 0,
						Range = 4,
					}
	Map.yz = {}
		Map.yz.yz01 = {	Code = "yz01",
						Area = "yz",
						Location = "Center",
						CP = 1,
						Range = 3,
					}
					
tprint(Map)




By this table, If I only got 1 information, Location = "Center", how can I search back which Code of this area?
For example, "Search Location "Center" and then return the code "yz01"....that is what I want to get, but don't know how to do it in Lua, Thanks.
Top

Posted by WillFa   USA  (525 posts)  Bio
Date Reply #1 on Wed 10 Aug 2011 03:31 AM (UTC)

Amended on Wed 10 Aug 2011 03:33 AM (UTC) by WillFa

Message

function ScanLocation(locStr)
  for area,areaTable in pairs(Map)do
    for code, codeTable in pairs(areaTable) do
      if codeTable.Location == locStr then
        return code
      end 
    end
  end
end
 


Since codeTable.Code is the same as the keyname of codeTable it'll work quicker. If you want to hard code it for clarity or fringe cases where your t.Code will be different, then return codeTable.Code instead... Also, if you start putting values in there there other than tables. (i.e. sz.Name = "Foo", sz.Builder = "Nick" or something) you'd want to wrap the second for in a if type(areaTable) == "table" check.
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.


12,019 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.