kaddad14
|
  |
| Joined: 13 Apr 2013 |
| Total Posts: 649 |
|
|
| 29 Jan 2017 09:08 AM |
I got a table with a list of all the planets as keys. And I need the following function to find and read the sun first. How would I make it prioritize the sun? local OurSystem = { Sun = nil Mercury= nil Venus= nil Earth= nil Mars= nil Jupiter=nil Saturn=nil Uranus=nil Neptune=nil } function Solar(tarble) for key, value in pairs(tarble) do print(key) end end Solar(OurSystem)
Output:Sun Neptune Venus Saturn Uranus Mercury Jupiter Earth |
|
|
| Report Abuse |
|
|
kaddad14
|
  |
| Joined: 13 Apr 2013 |
| Total Posts: 649 |
|
|
| 29 Jan 2017 09:12 AM |
| sorry, the Output says: Mars Sun Neptune Venus Saturn Uranus Mercury Jupiter Earth |
|
|
| Report Abuse |
|
|
|
| 29 Jan 2017 09:14 AM |
| make another table with only values and not like a dictionary and use that table for the order |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 29 Jan 2017 09:14 AM |
Dictionaries have no order.
Also, that isn't valid table syntax.
|
|
|
| Report Abuse |
|
|
kaddad14
|
  |
| Joined: 13 Apr 2013 |
| Total Posts: 649 |
|
|
| 29 Jan 2017 09:16 AM |
| I just replaced what the keys variables are with nil. They actually hold information that I need. |
|
|
| Report Abuse |
|
|
ChiefDays
|
  |
| Joined: 26 Sep 2016 |
| Total Posts: 502 |
|
|
| 29 Jan 2017 09:16 AM |
You should make a table like this:
local SolarSystem = { {"Sun", nil}, {"Mercury", nil} --bla bla bla }
for i,v in pairs(SolarSystem) do print(v[1]) end
|
|
|
| Report Abuse |
|
|
kaddad14
|
  |
| Joined: 13 Apr 2013 |
| Total Posts: 649 |
|
|
| 29 Jan 2017 09:21 AM |
| I was kinda using an array more. And my array contains tables. So like Sun = {Var,Var,Var,Var,Var}. My script depends on the sun existing before the other keys. So is there any workaround I could use? |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 29 Jan 2017 09:26 AM |
{ {"Sun", {Var,Var,Var,Var,Var}}, ... }
|
|
|
| Report Abuse |
|
|
kaddad14
|
  |
| Joined: 13 Apr 2013 |
| Total Posts: 649 |
|
|
| 29 Jan 2017 09:26 AM |
What if I were to do something like this?: function Solar(tarble) for i=1, #tarble do print(tarble[i]) end end Although when I try it nothing prints I think something like this could work. |
|
|
| Report Abuse |
|
|
kaddad14
|
  |
| Joined: 13 Apr 2013 |
| Total Posts: 649 |
|
|
| 29 Jan 2017 09:27 AM |
| @chimmihc I'm trying to create a way for me to save/load multiple solar systems. If I were to do each individual planet and sun it would take me a very long time. |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 29 Jan 2017 09:29 AM |
It's no different from the way you do in the OP, just organized as an array.
|
|
|
| Report Abuse |
|
|
kaddad14
|
  |
| Joined: 13 Apr 2013 |
| Total Posts: 649 |
|
|
| 29 Jan 2017 09:50 AM |
| I got a new Q now. How do you find the number of keys in an array? I tried #OurSystem and it prints 0. |
|
|
| Report Abuse |
|
|
kaddad14
|
  |
| Joined: 13 Apr 2013 |
| Total Posts: 649 |
|
|
| 29 Jan 2017 09:57 AM |
| Should i do some for loop to check for keys until key[i] = nil? |
|
|
| Report Abuse |
|
|
|
| 29 Jan 2017 10:06 AM |
you mean the number of keys in a dictionary?
local length = 0
for _, _ in next, dictionary do length = length + 1 end
|
|
|
| Report Abuse |
|
|
kaddad14
|
  |
| Joined: 13 Apr 2013 |
| Total Posts: 649 |
|
|
| 29 Jan 2017 10:07 AM |
| Ty, that helps alot. Thx everyone. |
|
|
| Report Abuse |
|
|