We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Splitting a String into more arrays.
Page Index Toggle Pages: 1
Splitting a String into more arrays. (Read 345 times)
Splitting a String into more arrays.
Nov 18th, 2008, 12:38pm
 
Hai there!

I'm having a text file that looks like this:

banana,chocolate,apple
kiwi,fresh,green

Then i'm loading this into a array.
But i want to have every second/third value, into a new array.

so that you have:
String fruit[] = {banana,kiwi};
String fruit2[] = {chocolate, fresh};
String fruit3[] = {apple, green};

How can i split those values, into a new array?

Thanks.
Re: Splitting a String into more arrays.
Reply #1 - Nov 18th, 2008, 1:58pm
 
read the file using loadStrings(), which will return an array of the lines.

then for each line in the array, use split(), with ',' as the delimiter, to get an array of the words.
Re: Splitting a String into more arrays.
Reply #2 - Nov 18th, 2008, 2:36pm
 
let's take the following string:

banana,chocolate,apple

with the split method, you can split those 3 words, in to position 0/1/2 of an array.

But i want to have 3 seperate arrays

String yellow[] = "banana";
String brown[] = "chocolate";
String green[] = "apple";
Re: Splitting a String into more arrays.
Reply #3 - Nov 18th, 2008, 4:58pm
 
Use a temporary array to get the result of the split, and use append() to add the resulting strings to their respective arrays.
Re: Splitting a String into more arrays.
Reply #4 - Nov 24th, 2008, 2:23pm
 
i know have the following:
Code:

void setup(){
size(screen.width,screen.height);
background(0);
smooth();

String[] regels = loadStrings("test.txt");

for(int i=0;i<=3;i++){
String regel = regels[i];
String[] lijst = split(regel, ',');
String lijst_a = lijst[0];
String[] a = append(a,lijst_a);
print(a[0]+" ");
}
}




But then it says, that the local variable 'a' may not have been intialized.
I don't know why the appending doesn't work...


Re: Splitting a String into more arrays.
Reply #5 - Nov 24th, 2008, 11:42pm
 
You should put the final variable (a) declaration (and initialization) outside of the loop. Here, you reset it on each iteration.
Page Index Toggle Pages: 1