- Home /
Iterate an array 4 by 4
Hi guys, i'm trying to create an iteration on a array that need to count 4 by 4. I explain better:
var users = new Array();
var selectDataMessage : String;
selectDataMessage= "selectDataMessage";
selectDatasMessage = URL + "?selectDataMessage="+selectDataMessage;
var dataMessage: WWW = new WWW(selectDatasMessage);
yield dataMessage;
users = dataMessage.text.Split(";"[0]); // this is an array
i get from php a string with all users that left a message on my game, so for example, in users, at position [0][1][2][3] i've all the information i need for a single player (name surname, gender, username)
the problem is the iteration i did something like this:
for(var i : int = 0; i < users.length;i++){
//Debug.Log("User length "+users.length);
firstNames.Push(users[i]);
lastNames.Push(users[++i]);
userNames.Push(users[++i]);
gender.Push(users[++i]);
}
this one get every single position and it put them inside 4 arrays each for every single data i need.
I know it's not really good but it's the only way i found . It doesn't work anyway, it goes off by one. and return this error:
ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
Parameter name: index
1
someone know how to solve this? thanks a lot.
Have you used JSON before? You could send a JSON encoded array from PHP and then have the JSON parser in C# turn it into the array without any manual labor.
i tried to return a json using echo jsonencode(); but then it comes inside a string (www.text) and i don't know how to conver it to an array by using JS. I don't know anything about c#
Answer by BiG · Jul 18, 2013 at 01:41 PM
The way you are accessing the array is a bit "dirty", as you said (it is not a good programming style to increment the cycle index directly into the iteration's body), but it seems fine (only if the array's length is a multiple of 4, obviously).
By the way,
i >= users.length
is wrong! It should be:
i < users.length
It's the real cause of the error.
ye sorry i pasted a test code in real is < . I didn't understand if users = data$$anonymous$$essage.text.Split(";"[0]); put at[0] the ; char and the real array start from [1] anyway it gives the same error.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110351im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[c#] Create visible appearence of a grid using texture or models 1 Answer
Why is iterating an array and destroying objects considered bad practice? 2 Answers
Loop Through Entire Array - Simon Game 1 Answer
Getting information from an array 1 Answer
Ways to optimize this code? iterate through inventory 0 Answers