Question Why does this loop not work?

StyIe

New member
Joined
Jul 19, 2008
Messages
2
Programming Experience
Beginner
I made a button with this code. It should read a website line by line(loop for each line) and do this loop for each counter. For example I have a listbox1 containing satan; n44bj; jfa88; acerobert then I want the button to search this site and add the lines ending with lowercased listbox1 items found to listbox2.
VB.NET:
Dim request1 As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("http://xwis.net/ladders/ra2/?pure")
Dim response1 As System.Net.HttpWebResponse = request1.GetResponse()
Dim sr1 As System.IO.StreamReader = New System.IO.StreamReader(response1.GetResponseStream())
Dim MatchedLine As String = ""
Dim counter As Integer = -1

ListBox2.Items.Clear()

For counter = 0 To (ListBox1.Items.Count - 1)
    Do While sr1.Peek <> -1
        MatchedLine = sr1.ReadLine()
        If MatchedLine.EndsWith(LCase(ListBox1.Items.Item(counter))) Then ListBox2.Items.Add(MatchedLine)
    Loop
Next counter

When I use this code it only looks for the first item on listbox1 and it doesnt continue to do the loops for the other indexes on listbox1. Any1 have a solution for me?

Thanks in advance
 
Last edited:
Swap the loops. You can only read the stream once, so instead read line by line from stream and in an inner loop compare with all list items.

You can also first read all lines, close the server connection, then do your processing comparing the two arrays. This will both help preserve server resources and minimize the possibility for connection breaking in middle of loop, but it will also require more memory usage at client.
 
Thanks it is now working
I have made it like this...

VB.NET:
        Do While sr1.Peek <> -1
            MatchedLine = sr1.ReadLine()
            For counter = 0 To (ListBox1.Items.Count - 1)
                If MatchedLine.EndsWith(LCase(ListBox1.Items.Item(counter))) Then ListBox3.Items.Add("#" + MatchedLine + " (Ra2)")
            Next counter
        Loop

How do I change prefix to awnsered?
 
This will both help preserve server resources and minimize the possibility for connection breaking in middle of loop,.

i'd imagine that the CPU's ability to perform an EndsWith would far exceed the internet connecction's ability to provide data, so it wont be the bottleneck.. Reading incrementally should add only a miniscule amount of processing time
 
Back
Top