Hi, I’m trying to split a string into an array and then into a 2D array, however when I split the string it doesn’t store any value after.
userDataString contains 1|password|Staff|Staff|0|STAFF;2|password|Student|AA|11|STUDENT
which I want to split on the “;” so that I then have
users = “1|password|Staff|Staff|0|STAFF” , “2|password|Student|AA|11|STUDENT”
then inside the nested FOR loop I want to split it on the “|” and then have a new row for each different section of user details.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Userdata : MonoBehaviour
{
string url = "http://localhost/Wellbeing/userdata.php";
public string[] users;
public string[,] userArray;
public string[] User = new string[3];
private int nRow;
private string[] Temp;
private string userDataString;
private bool isDone;
//The CallDetail method is used to access the data, split it into each user and then into individual fields.
//Then relevant peices are return to the login script
public string[] CallDetail(string UsernameInput, string PasswordInput)
{
//access the website
WWW userData = new WWW(url);
while (userData.isDone == false)
{
}
//waits until www returns a value into userData
string userDataString = userData.text;
Debug.Log(userDataString);
//splits each section of information into strings of each users data
string[] users = userDataString.Split(';');
Debug.Log(users);
//iterates through the strings and splits the into indiviual fields of data
for (var item = 0; item <= users.Length; item++)
{
Temp = users[item].Split('|');
Debug.Log(Temp);
//stores each of piece of data in the 2D array
for (var field = 0; field <= Temp.Length; field++)
{
/////////////////////////////////////////
userArray[nRow, field] = Temp[field];
/////////////////////////////////////////
}
nRow++;
}
//iterates through each row in the 2D array and searches for the Username and password that has been input
for (var item = 0; item < userArray[item, 0].Length; item++)
{
//if stored userName is equal to the username input and the stored password is equal to the password input
if (userArray[item, 2] == UsernameInput && userArray[item, 1] == PasswordInput)
{
//stores each piece of relevant data into an array
User[1] = userArray[item, 1];
User[2] = userArray[item, 2];
User[3] = userArray[item, 5];
}
}
return User;
}
}
I don’t know the length of the array, so is there a possibility of having a 2Dlist? It definitely needs to be 2 dimensional though because of the way it is searched
To get the data in a 2D array, you can do some Linq:
string[][] result = userDataString.Split(',').Select(str => str.Split('|')).ToArray();
I can’t really see from your code what part of the input you want to have in your arrays, since you have three arrays named “users”, “userArray” and “User” respectively. Don’t name variables like that!
public static void Main(string[] args)
{
string [][] result;
string data = "1|password|Staff|Staff|0|STAFF;2|password|Student|AA|11|STUDENT";
string [] ud = data.Split(';');
result = new string [ud.Length][];
int i = 0;
foreach(string s in ud){
string [] g = s.Split('|');
result[i] = g;
++i;
}
}
I tried to implement this however it still gave me an empty output when I tried to print it.
For the variables that are confusing:
users - is a list where each item in the list is all information for the user
userArray - is a 2d list/array where each item is a different part of the user data and each row is a new user
User - is an array of the values being returned to the program
With the jagged array is it still efficient/effective as the single dimensional aspect will always stay the same (6 values) but the second dimension can potentially become very large
I’d probably build this out as a 2D list, and if you prefer using arrays then just do a ToArray on the list. My guess is that you’ll find it easier to use this way, but probably doesn’t run as fast as doing everything in arrays.
A list was certainly an option. I just wrote one example.
As for explaining the code…hmm.
It splits the first part, and initializes part of the array. Then goes through the # of strings that were the result of the first split, and splits them, again; each of those is assigned to an array.
Hope that helps. If you had a more specific question, feel free to ask.
I just tried to implement this code but it won’t work for me and still doesn’t print anything when I try to. So I am rather lost in what I am doing wrong or what is happening
This is my code with your code for splitting implemented
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using UnityEngine;
public class Userdata : MonoBehaviour
{
string url = "http://localhost/Wellbeing/userdata.php";
public string[] users;
public string[][] userArray;
public string[] User = new string[3];
private int nRow;
private string[] Temp;
private string userDataString;
private bool isDone;
//The CallDetail method is used to access the data, split it into each user and then into individual fields.
//Then relevant peices are return to the login script
public string[] CallDetail(string UsernameInput, string PasswordInput)
{
//access the website
WWW userData = new WWW(url);
while (userData.isDone == false)
{
}
//waits until www returns a value into userData
string userDataString = userData.text;
Debug.Log(userDataString);
string[] ud = userDataString.Split(';');
userArray = new string[ud.Length][];
Debug.Log(userArray);
int i = 0;
foreach (string s in ud)
{
string[] g = s.Split('|');
userArray[i] = g;
++i;
}
print (userArray);
//iterates through each row in the 2D array and searches for the Username and password that has been input
for (var item = 0; item < userArray[item][0].Length; item++)
{
//if stored userName is equal to the username input and the stored password is equal to the password input
if (userArray[item][2] == UsernameInput && userArray[item][1] == PasswordInput)
{
//stores each piece of relevant data into an array
User[0] = userArray[item][1];
User[1] = userArray[item][2];
User[2] = userArray[item][5];
}
}
Debug.Log("here");
return User;
}
}