Page 1 of 1
strange behavior iterating through getAllRooms array
Posted: 29 Jun 2009, 13:11
by marcos
I have found a strange behavior when I iterate through the array retrieved from getAllRooms. I noticed the following:
1) If I use a for in loop, I can trace the real number of available rooms.
Code: Select all
room_arr = sfs.getAllRooms();
for (var r:String in room_arr)
{
var room:Room = room_arr[r];
trace("Room: "+room.getName());
}
In this case, there are 6 available rooms, and all are traced properly.
2) If I use a standard for loop, I get different results:
Code: Select all
room_arr = sfs.getAllRooms();
nRooms = room_arr.length;
trace("nRooms: "+nRooms);
trace("Room List: "+room_arr.toString());
for (i=0; i<nRooms; i++)
{
roomTemp = room_arr[i];
trace("updating room: "+roomTemp.getName());
room_lst.addItem({icon: 'IconRoom', label:roomTemp.getName(), idRoom:roomTemp.getId()});
}
In the second case, I get 7 rooms, and the first one is empty, so when I call getName method, I get an error from the compiler. I can use a for in loop, but I would like to know what is the reason behind this behaviour in standar for loops usage.
Thanks in advance,
Posted: 29 Jun 2009, 15:31
by marcos
Reading the documentation, I have found that for getUserList method of Room class it specifies that is an asociative array with userIds as key values, so the for in or for each approach is the best way to do it.
I suppose the getAllRooms has the same kind of array response.
But I'm still wondering myself which is the reason because the array contains a empty first element.
Posted: 30 Jun 2009, 12:05
by Lapo
That's correct. Use
for in for looping.
Code: Select all
But I'm still wondering myself which is the reason because the array contains a empty first element.
By empty you mean undefined/null?
Posted: 30 Jun 2009, 12:39
by marcos
If you try to do this:
you get this:
Code: Select all
",firstRoom,secondRoom,thirdRoom..."
As you can see, there is a empty first element in the array, maybe is a problem with the toString, but I don't think so, because room_arr.length returns the numbers of rooms + 1.
Thanks and congrats for your great support Lapo,
marcos.
Posted: 30 Jun 2009, 15:30
by Lapo
That's not a problem unless the empty room appears in the
for in loop.
The array representation always shows commas for every empty cell.
Try this experiment:
Code: Select all
var arr:Array = []
arr[0] = "zero"
arr[5] = "five"
arr[10] = "ten"
trace(arr)
see what I mean?
cheers
Posted: 30 Jun 2009, 15:55
by marcos
Hi Lapo,
Yeap I know that the coma is because there is an empty element, and that's the question / problem.
getAllRooms() returns an array with the first element empty, so if you get the length or you walk through it with a classic for loop, you find unexpected behaviors. With a for-in loop you have not this problem, but I think it's not too correct that getAllRooms returns an empty element in the first position.
Anyway, knowing this info, it is not a problem.
Thanks a lot.
Posted: 01 Jul 2009, 06:16
by Lapo
With a for-in loop you have not this problem, but I think it's not too correct that getAllRooms returns an empty element in the first position.
No, that's perfectly normal.
Evidently your rooms start with index 1, but this continually changing as you create and remove other rooms so you will notice even more "holes" in the list.
If I have two Zones (Zone A and B) both containing 5 rooms, the user connecting in Zone A will have an initial room list with the following ids --> 0,1,2,3,4 ... all contiguos.
Zone B will have a room list with ids 5,6,7,8,9... which means that the initial room list will have 5 holes in it

Posted: 01 Jul 2009, 06:26
by marcos
Hi Lapo,
Now it's totally clear. Another thing that I have learned in these intensive smart fox server developing days
Thanks a lot for your patience,
Marcos