[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Making Variables from a table
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Making Variables from a table

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


Posted by FenceWalker   USA  (8 posts)  [Biography] 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?
[Go to top] top

Posted by Worstje   Netherlands  (899 posts)  [Biography] 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. :)
[Go to top] top

Posted by FenceWalker   USA  (8 posts)  [Biography] 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.
[Go to top] top

Posted by Nick Gammon   Australia  (21,322 posts)  [Biography] 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
[Go to top] top

Posted by FenceWalker   USA  (8 posts)  [Biography] 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.
[Go to top] top

Posted by Twisol   USA  (2,257 posts)  [Biography] 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
[Go to top] top

Posted by FenceWalker   USA  (8 posts)  [Biography] 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.
[Go to top] 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.


4,446 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]