Hello, I’m very new to openFrameworks and C++ as well. I’ve only done some work with Processing before.
What I’m trying to figure out, and can’t seem to find the answer, is how to split one string into multiple strings. For instance, in Processing, I would do it roughly like this:
String[] all = loadStrings("data.txt");
fruits = new String[all.length];
vegetables = new String[all.length];
for(int i=0; i<all.length; i++) {
String[] list = split(all[i], ";");
fruits[i] = list[0];
vegetables[i] = list[1];
}
So if say, the text inside data.txt went something like this:
apple; lettuce; pear; onion; banana; spinach;
then the two new strings would look something like this:
I’m sorry if this is a silly question or something that’s been answered before and I just couldn’t get it. I might just be searching with the wrong keywords. If it already has been answered, than a point in the right direction would be appreciated. openFrameworks/C++ just seems so much harder to get the hang of than Processing, but Processing can’t handle the amount of data I want to load.
@siephe, OF has it’s own function for splitting a body of text into strings ofSplitString( string& str, string& delimiter). It returns a vector of string, which is pretty similar to an array of string.
But here’s an example of what you might do with your list of fruits.
string myString = "apple;lettuce;pear;onion;banana;spinach;"
// divide the string using a ';' as a delimiter
// notice that i removed the spaces after the individual words
vector<string> splitString = ofSplitString( myString, ";");
// loop through the results
for(int i=0; i<splitString.size(); i++){
printf( "element %i is %s\n", i, splitString[i].c_str() );
}
for(int i=0; i<splitString.size(); i++){
printf( "element %i is %s\n", i, splitString[i].c_str() );
}
is just printing out the list of fruits and vegetables. I have no trouble up till that part. What I’m having trouble with is how to divide the list so that it prints out two lists, such as:
If that’s the case that every first word is a fruit and every second is a vegetable, then you can change the way that you step through the list (by changing the values in the for loop).
to count the fruits (0, 2, 4, 6 … )
for(int i=0; i<splitString.size(); i+=2){
printf( "fruit is %s\n", i, splitString[i].c_str() );
}
to count the vegetables (1, 3, 5, 7 … )
for(int i=1; i<splitString.size(); i+=2){
printf( "vegetable is %s\n", i, splitString[i].c_str() );
}
when you’re looping trough the list and extracting your individual fruit and veg names, you can then add them to separate lists of fruits and vegetables. you’ll want to create a new vector for fruits and another for vegetables to store this info.
string myString = "apple;lettuce;pear;onion;banana;spinach;";
// divide the string using a ';' as a delimiter
// notice that i removed the spaces after the individual words
vector<string> splitString = ofSplitString( myString, ";");
// loop through the results
for(int i=0; i<splitString.size(); i+=2){
fruits = ofSplitString(splitString[i], ";");
cout << fruits[i] << endl;
}
However, only apple is printed out and I’m getting the following error:
on this line: { return _M_rep()->_M_length; } down in the code below:
public:
// Capacity:
/// Returns the number of characters in the string, not including any
/// null-termination.
size_type
size() const
{ return _M_rep()->_M_length; }
the array fruits isn’t large enough for the element in the vector that you’re requesting. remember the fruits vector is a different size than the splitString vector … so your loop goes out of bounds and you get that nasty EXC_BAD_ACCESS error.
since your string has already been split up (above), you don’t need to do this again in the for loop
instead try adding the string located at splitString[i] to a new vector that’s declared outside of the for loop.
// loop through the results
vector<string>fruits;
for(int i=0; i<splitString.size(); i+=2){
cout << splitString[i] << endl;
fruits.push_back( splitString[i] );
}