Forum

> > CS2D > Scripts > For loop running in a certain order
Forums overviewCS2D overview Scripts overviewLog in to reply

English For loop running in a certain order

13 replies
To the start Previous 1 Next To the start

old For loop running in a certain order

Mami Tomoe
User Off Offline

Quote
Hi I have a table and the order of the items is very important.
When I run a for loop through it I get random items instead of it running in the order I declared it as.

Example:
IMG:https://i.imgur.com/aoK5DEm.png


Is it possible to fix this?

Sorry for double post the page got stuck loading and I didn't see a new topic created

old Re: For loop running in a certain order

Starkkz
Moderator Off Offline

Quote
@user Mami Tomoe: You got it sorted that way because Lua tables are half arrays/half hash maps. When you use strings you're placing data on the hash part, and the library is going to try to place them in a position where the accessing is more efficient.

@user Yates: IIRC, ipairs is for numerical indices.

@user Mami Tomoe: I suggest you declare the order in another table.
MAP.ORDER = {"OCEAN", "DESERT", "MARBLE", "SNOW", ...}


And then use it to iterate over the other table.
1
2
3
4
5
6
for _, k in pairs(MAP.ORDER) do
	local v = MAP.SPAWNS[k]

	print(k, v)
	
end
edited 1×, last 06.06.17 03:21:23 pm

old Re: For loop running in a certain order

DC
Admin Off Offline

Quote
Waiiit... nooo! That's NOT how Lua works guys!
1
2
3
4
5
6
7
8
spawns={
     "OCEAN",
     "SHIT",
     "PRINT",
     "CLITR",
     "CENTRE",
     "SATANSVILLE"
}
is in fact how you declare the array part of a Lua table! So you are already declaring it like an array. You just chose the wrong way to iterate (even though I didn't expect it to be mixed up... kind of weird).

It's equivalent to
1
2
3
4
5
6
7
8
spawns={
     [1] = "OCEAN",
     [2] = "SHIT",
     [3] = "PRINT",
     [4] = "CLITR",
     [5] = "CENTRE",
     [6] = "SATANSVILLE"
}
You can try it!
print(spawns[2])

This will ALWAYS print "SHIT" - with the first and the second way to declare it.

user Mora already posted a way to iterate an array in Lua. It's the good old loop with index variable:
1
2
3
for i=1,#spawns do
	print(spawns[i])
end
Always the right order.

I don't get what your "reserved" stuff is about.

http://lua-users.org/wiki/TablesTutorial > Tables as arrays section

You probably change your table somehow at at some place if you get different orders with this method.

old Re: For loop running in a certain order

DC
Admin Off Offline

Quote
*sigh* .. nobody will copy your Lua. And if someone tries be proud. Why do people keep everything secret?

What you shared with me has a completely different structure from what you posted here.

@everyone: what he actually has looks more like
1
2
3
4
5
6
SPAWNS   = {
     OCEAN                         = { ... stuff ... },
     DESERT                         = { ... stuff ... },
     MARBLE                         = { ... stuff ... },
  ....
}
So this is completely different because you don't have an array anymore but indeed a hashmap without order.

I would suggest this notation:
1
2
3
4
5
6
SPAWNS = {
     { name="OCEAN", ... stuff ... },
     { name="DESERT", ... stuff ... },
     { name="MARBLE", ... stuff ... },
  ....
}

Then you should still be able to iterate with proper order and you can use .name to get the name of an entry.

Other solutions might be better though. It really depends on how you want to access the stuff.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview