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 ➜ Plugins ➜ do I need pairs or ipairs for this?

do I need pairs or ipairs for this?

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


Posted by Kevnuke   USA  (145 posts)  Bio
Date Sun 31 Mar 2019 12:11 AM (UTC)

Amended on Sun 31 Mar 2019 12:13 AM (UTC) by Kevnuke

Message
I'm taking JSON data from IRE GMCP messages and am trying to get the named index from the table. I keep getting the value and not the key AND value. I tried to test it in immediate execution but MUSHclient yelled at me for trying to
require "ipairs"


I want to have whatever the index names are in the JSON data be what the index is named in the Lua table.
For example, if I get this from the Achaea:

Char.Name { "name": "myname", "fullname": "myfullname" }


What I'll end up with is:

stats = {
    name      = "myname",
    fullname  = "myfullname"
}

I've tried this and a few variations of it so far just to see if I can get the name of the index to print:

string = [[{"name": "myname", "fullname": "myfullname"}]] -- many values for fullname have single quotes in them.
test = json.decode('[' .. string .. ']')[1]
for k,v in pairs (test) do
print (test[k])
end
Top

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #1 on Sun 31 Mar 2019 12:45 AM (UTC)
Message
Omg I'm dumb, I was doing too much. This worked:

function test_data()
	string = [[{"name": "myname","fullname": "myfullname"}]]
	test = json.decode('[' .. string .. ']')[1]
	for k,v in pairs (test) do
		print (k .. " " .. v)
	end
end
Top

Posted by Nick Gammon   Australia  (23,163 posts)  Bio   Forum Administrator
Date Reply #2 on Sun 31 Mar 2019 06:47 AM (UTC)
Message
It looks like you've solved it, but for reference ipairs only returns numerically keyed items starting at 1 (and stopping if it hits a gap).

So, for alpha-keyed items pairs is more appropriate.

- Nick Gammon

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

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #3 on Sun 31 Mar 2019 06:49 AM (UTC)

Amended on Sun 31 Mar 2019 06:51 AM (UTC) by Kevnuke

Message
ohhh so that wouldn't have worked anyway. Thanks I always forget the difference. :D

But why did it throw a bunch of errors when i tried to require ipairs? looked like one for each dll. My MUSHclient folder is in my OneDrive folder, if that makes a difference
Top

Posted by Nick Gammon   Australia  (23,163 posts)  Bio   Forum Administrator
Date Reply #4 on Sun 31 Mar 2019 08:25 AM (UTC)

Amended on Sun 31 Mar 2019 08:26 AM (UTC) by Nick Gammon

Message
You don't need to require ipairs. It is a built-in function.

It would be like doing this:


require "print"


You don't need to do that, and there would be no file print.lua in your directory.

- Nick Gammon

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

Posted by Kevnuke   USA  (145 posts)  Bio
Date Reply #5 on Sun 31 Mar 2019 08:30 AM (UTC)
Message
Hmm I see. I just thought it was weird when I tried ipairs and it did nothing. No output, no errors, nothing. I'm not really used to that. lol
Top

Posted by Nick Gammon   Australia  (23,163 posts)  Bio   Forum Administrator
Date Reply #6 on Sun 31 Mar 2019 08:38 AM (UTC)
Message
It did nothing because there were no numeric keys.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
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.


19,339 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.