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 ➜ Making Variables from a table

Making Variables from a table

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


Posted by FenceWalker   USA  (8 posts)  Bio
Date Sun 17 Oct 2010 08:39 PM (UTC)

Amended on Sun 17 Oct 2010 09:06 PM (UTC) by FenceWalker

Message
I have a table right now that goes something like this:

1 = "dogs=2"
2 = "cats=3"
3 = "dogs_and_cats=2 3"


Is there a way to somehow loop through this table and set anything in the value field, before the = sign to the name of a variable and anything after no matter what it is to the value?
Top

Posted by Worstje   Netherlands  (899 posts)  Bio
Date Reply #1 on Sun 17 Oct 2010 08:54 PM (UTC)
Message
What you want is possible. However, I am not going to give you the answer. (Sorry, mean streak of mine.)

Instead, I will ask you - how are you getting this table with this particular listing? Is it a part of some persistence mechanism you are working on to save variables across sessions?

Template:post=4960 Please see the forum thread: http://gammon.com.au/forum/?id=4960.


If that is not the case, I'll see about providing some snippet that matches what you ask for - but I think in this case the real problem might be that you are trying to implement something that has already been implemented once and seen a fair share of use. :)
Top

Posted by FenceWalker   USA  (8 posts)  Bio
Date Reply #2 on Sun 17 Oct 2010 09:18 PM (UTC)
Message
The table is actually one large string I have broken down to table format. I don't care about the variables saving across sessions I just want them assigned for use when the script is running. I just can't quite figure out how to separate the values of the table into new key = value pairs.
Top

Posted by Nick Gammon   Australia  (23,158 posts)  Bio   Forum Administrator
Date Reply #3 on Sun 17 Oct 2010 10:15 PM (UTC)
Message
http://www.gammon.com.au/scripts/doc.php?lua=string.match

That would be one way.

Something like:


key, value = string.match (s, "(%a+)=(.+)")


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by FenceWalker   USA  (8 posts)  Bio
Date Reply #4 on Mon 18 Oct 2010 01:12 AM (UTC)
Message
So far I've come up with this:

function makeVars (k, v)
		for key, value in  string.gmatch (v, "(.+)=(.+)") do
		newtable [key] = value
		end
	end 

Then i use:

newtable = {}
table.foreach (t,makeVars)

Where t is the original table. This gives me a new table called newtable with the k, v pairs separated correctly.

Thanks for the help.
Top

Posted by Twisol   USA  (2,257 posts)  Bio
Date Reply #5 on Mon 18 Oct 2010 02:00 AM (UTC)
Message
table.foreach is actually deprecated. You can exactly duplicate its functionality with this:
for k,v in pairs(t) do
  makeVars(k, v)
end

And when you're at that point, you may as well do this:
local newtable = {}
for k,v in pairs(t) do
  for key, value in string.gmatch(v, "(.+)=(.+)") do
    newtable[key] = value
  end
end


One final note: When your table has ascending numeric keys, as yours does in your first post, you might want to consider using ipairs() instead. (The old-style equivalent would be table.foreachi; note the 'i')
local newtable = {}
for k,v in ipairs(t) do
  for key, value in string.gmatch(v, "(.+)=(.+)") do
    newtable[key] = value
  end
end

ipairs() just goes over the table items in order (i.e. t[1], t[2], t[3], etc.), and excludes non-number keys.

'Soludra' on Achaea

Blog: http://jonathan.com/
GitHub: http://github.com/Twisol
Top

Posted by FenceWalker   USA  (8 posts)  Bio
Date Reply #6 on Mon 18 Oct 2010 02:04 PM (UTC)
Message
Thanks for letting me know about table.foreach being deprecated. I missed that in my reading some how. Also thanks for helping me clean up what I already had.
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.


22,655 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.