Wednesday, March 28, 2012

Simple Arraylist Compare not working

I'm in the process of building a simple search engine, and I'm stuck on comparing two arrays.
this is what I've done.
1. pass in string
2. break string apart based on " "
3. remove duplicate words
4. TRY to compare allowed words to NOT ALLOWED words
I want to compare words in the string to not allowed words and remove them, thus leaving only good words.

I get error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

Line 34: if wordlist(i) = xword(j) then
Line 35: wordlist.Remove(wordlist(i))
Line 36: end if

Dim mystring as string = "100 watt Peavy watt watt watt Guitar Amp. Pedel to switch back from clean to clean distorted"
Dim mysubstr() As String

Dim wordlist = New Arraylist()
dim mywordlist = New Arraylist()

dim j, p, i

'// words not allowed

dim xword = New Arraylist()
xword.Add("clean")
xword.add("switch")

' // split string into words based on " "

mysubstr = Split(lcase(mystring)," ")

'// loop through words and delete any duplicates, add to wordlist collection

for j = 0 to ubound(mysubstr)
if not wordlist.contains( mysubstr(j) )then
wordlist.add(mysubstr(j))
end if
next

'// compare wordlist collection to xword(not allowed) and remove them from word list

for i = 0 to wordlist.count - 1
for j = 0 to xword.count - 1

if wordlist(i) = xword(j) then
wordlist.Remove(wordlist(i))
end if

next

next

'// return results to see if it worked.

for p = 0 to wordlist.count - 1
response.write( wordlist(p) & "<br>")
next

hopefully someone can help. I'm not sure what it means to get that error.

thanks
mattI know what's causing the error.

because I'm looping through two arrays, then removing elements from them dynamically, it just barfs. the for loop statement gets confused on where it left off or something.

isnt their a way to compare arrays in .NET? it's such a basic task, or you would think. maybe the dictionary object is more suitable.

thanks
here's what I did that fixed it

for i = 0 to wordlist.count - 1

if instr(xword, wordlist(i)) = 0 then
mywordlist.add(wordlist(i))
end if

next

for p = 0 to mywordlist.count - 1
response.write(mywordlist(p) & "<br>")
next

0 comments:

Post a Comment