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 & HelpPrograms › Filter one array through another
Page Index Toggle Pages: 1
Filter one array through another (Read 648 times)
Filter one array through another
Sep 11th, 2006, 8:21pm
 
I am trying to filter out an array of stopwords from an array of source words to make an array of clean words. This code does not work but I hope illustrates what I am trying to do.
Any ideas anyone how to do this?

Quote:


String mywords[] = {
 "yes","nice","happy","stop","lovely","now"};
String stopwords[] = {
 "no","stop","now"};
String cleanWords[] = {
};

for(int i=0; i < mywords.length; i++) {
 for(int p=0; p < stopwords.length; p++) {

   //This checks if the source word and stopword match
   if(mywords[i].equals(stopwords[0]) == false) {  
     cleanWords = append(cleanWords, mywords[i]);

   }
 }
}


println(cleanWords);


Re: Filter one array through another
Reply #1 - Sep 12th, 2006, 12:45am
 
you need to change stopwords[0] to stopwords[p] I think.
Re: Filter one array through another
Reply #2 - Sep 12th, 2006, 1:35am
 
You'll also want to check all the stopwords before appending to the clean list.  Otherwise the clean list will come out saying:

yes yes yes nice nice nice happy happy happy stop stop lovely lovely lovely now now

Try implementing a find function, so it's clear you only check each word once per stopword.  Something like:

Code:

boolean find(String toFind, String[] words) {
for(int i=0; i < words.length; i++) {
if(toFind.equals(words[i])) {
return true;
}
}
return false;
}


Then your loop becomes:

Code:

String mywords[] = {
"yes","nice","happy","stop","lovely","now"};
String stopwords[] = {
"no","stop","now"};
String cleanWords[] = {
};

for(int i=0; i < mywords.length; i++) {
if(!find(mywords[i],stopwords)) {
cleanWords = append(cleanWords, mywords[i]);
}
}

println(cleanWords); // yes nice happy lovely?



All untested, but you get the idea.
Re: Filter one array through another
Reply #3 - Sep 12th, 2006, 12:25pm
 
Thanks so much that works great!
Page Index Toggle Pages: 1