- Home /
Put user list in 2d array
Hello,
For a current application I am working on, I want to retrieve a list of users from a php website. The website currently displays the following as a user list:
Name1 | Email1 | Status1 | Name2 | Email2 | Status2 | Name3 | Email3 | Status3 |
Now what I would like to achieve is to put this information in an array, so I could just print ( for example ) the email adress of user 2.
I am able to put it into a built in array using the following:
public string[] userInfo;
WWW hs_get = new WWW( post_url );
userInfo = hs_get.text.Split("|"[0]); // put the information of the user in an array
It currently puts every word before a "|" into a new row.
I've been busy trying to use a 2d array, but I'm new to this and I cannot seem to figure it out. I have the following so far:
string[ , ] userInfo = new string[3,3];
WWW hs_get = new WWW( post_url );
I have no idea how to put the hs_get.text into this array now. It would be great to make it the following:
userInfo[0,1] = Name1, userInfo[0,2] = Email1, userinfo[0,3] = Status1
userinfo[1,1] = Name2, userInfo[1,2] = Email2, userInfo[1,3] = Status2
etc.
I have no idea if I'm on the right track or if this is the best way to do this, I'm fairly new with all this.
I hope I explained my problem well enough since english is not my native language.
Thanks in advance!
Answer by whydoidoit · Jun 02, 2012 at 10:45 AM
You could put your name etc into a struct and just read the data into that - would be much easier to follow after:
public struct UserInfo
{
public string name;
public string email;
public string status;
}
public UserInfo [] userInfo;
public void GetData(string post_url)
{
WWW hs_get = new WWW(post_url);
var data = hs_get.text.Split("|"[0]);
var numUsers : int = Mathf.Floor(data.Length/3);
userInfo = new UserInfo[numUsers];
for (int i = 0, user = 0; user < numUsers && i+2 < data.Length; i+=3, user++)
{
userInfo[user].name = data[i];
userInfo[user].email = data[i + 1];
userInfo[user].status = data[i + 2];
}
}
Hey $$anonymous$$ike, thanks for your answer. Hadn't heard of struct yet, but as far as I understand it, it seems like a great sollution. I've used your code. I do get an error tho.
IndexOutOfRangeException: Array index is out of range.
It has something to do with the "userInfo[user].name = data[i];" line, I did a bit of trial & error the past 30 $$anonymous$$, but I can't seem to figure out what the problem is. Do you perhaps have an idea about what might be wrong?
Sounds like there isn't an even group of three of the data elements co$$anonymous$$g in. Will modify my answer to account for that eventuality.
Updated. BTW please use the add new comment link hidden at the right of the screen rather than new answers when making a comment :).
Edit
For some reason I can't post another comment! Per @mdreptile (below) the answer is updated to force numUsers to be an int. thx
Hey $$anonymous$$ike, thanks for the update.
Somehow the line
"userInfo = new UserInfo[numUsers];"
doesn't work. I get the following error:
"Cannot implicitly convert type float' to
int'. An explicit conversion exists"
When I make the line for example, it does work: "userInfo = new UserInfo[3];"
And when I print the following, I do get an int: "var numUsers = $$anonymous$$athf.Floor(data.Length/3);"
So I don't understand why numUsers isn't allowed in the UserInfo[].
Thanks for helping me.
this must mean userInfo is an integer, while numUsers in a float I believe. (ignore bad advice following) You could use $$anonymous$$ath.RoundToInt() to convert the float to an integer, and then set the userInfo to that int.
something like:
int numUserInt = $$anonymous$$athf.RoundToInt(numUsers);
then, change to this:
userInfo = numUserInt;
(/end bad advice)
Or actually... you could in javascript do it different, sorry im used to C#...
just when you set "var numUsers ..." ins$$anonymous$$d do "var numUsers : int =..." to specifically set it as an integer, again im not so hot with unity javascript so I dont know if thats how its done exactly :P but I think it is assu$$anonymous$$g a float for that var, so you need to specify that it is a integer ins$$anonymous$$d. Im basing this all on theory I might be completely wrong, but if that doesnt work perhaps someone will come along and correct me!
@whydoidoit no problem hope things work for you yorickke
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to send data form to server using json in unity 1 Answer
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer