New topic: addRow in For...Next loop dumb problem...
<http://forums.realsoftware.com/viewtopic.php?t=30656> Page 1 of 1 [ 5 posts ] Previous topic | Next topic Author Message rlinsurf Post subject: addRow in For...Next loop dumb problem...Posted: Sun Oct 25, 2009 8:24 pm Joined: Tue Mar 21, 2006 12:49 am Posts: 40 In have the following code under a âGoâ button. Itâs meant to open a webpage and parse out a unique ID (of which there are 20), which is then used to create a new url, and then downloads another html file. It then parses that page to get a date and a text note out of it, then adds those to two cells of a new row in the listbox. Code: Dim f as FolderItem Dim stream as TextInputStream Dim count, i as Integer Dim s, searchStr, url as String f=GetFolderItem("/Users/jeffrey/Downloads/html/247.html",_ FolderItem.PathTypeShell) If f.exists then stream=f.OpenAsTextFile s=stream.ReadAll() searchStr="_jottId"""+" value=""" count=CountFields(s, searchStr) For i=2 to 21 url=trim("http://jott.com/show.aspx?id="+Mid(s.NthField(searchStr,i), 1, 36)) TCPsocket1.setPostContent bodyFld.text,"" TCPsocket1.post(url) Next stream.Close else MsgBox "The folderitem does not exist." End if Under the TCPSocket1âs Page Received event I have Code: dim c as string c = chr(9)+chr(9)+chr(9)+replace(Mid(content, instr(1,content,"_jotttext")+11), Mid(content, instr(1,content,"</textarea>")),"")+chr(9)+replace(Mid(content, instr(1,content,"tm=new Date(")+13, 36), "');docu","") jottList.addrow "" jottList.Cell(jottList.LastIndex, -1)=c This unfortunately only works for the very last iteration, where i=21. Obviously, Iâm missing why this isnât adding rows for every iteration. Top sbeach Post subject: Re: addRow in For...Next loop dumb problem...Posted: Sun Oct 25, 2009 10:34 pm Joined: Wed May 10, 2006 1:27 pm Posts: 23 Sounds like you're only getting the last result because you aren't giving the socket enough time to transmit or the server enough time to receive and respond to your request before telling the socket to try the next address. The clean solution is to subclass the TCPSocket to do the receiving, make an array of that class in your window properties and add sockets as needed, destroying them when they're done. BUT a quick and dirty way to do it, since it seems TCPSocket1 is a physical object in your window, and it seems you will always be making 21 requests... is to make TCPSocket1 a control array: So first set it's index to 0 in the property editor, then duplicate it until you get one that has an index of 21. then change your code Code:For i=2 to 21 url=trim("http://jott.com/show.aspx?id="+Mid(s.NthField(searchStr,i), 1, 36)) TCPsocket1.setPostContent bodyFld.text,"" TCPsocket1.post(url) Next to... Code:For i=2 to 21 url=trim("http://jott.com/show.aspx?id="+Mid(s.NthField(searchStr,i), 1, 36)) TCPsocket1(i).setPostContent bodyFld.text,"" TCPsocket1(i).post(url) Next _________________ -Sean Top rlinsurf Post subject: Re: addRow in For...Next loop dumb problem...Posted: Sun Oct 25, 2009 10:44 pm Joined: Tue Mar 21, 2006 12:49 am Posts: 40 Hi, Sean-- Wow. That first seems incredibly interesting -- and complex. Um, could you step me through how I would do that (Please feel free to say no.)? When I put in the code you posted, I get this: Code, mainWindow.PushButton1.Action, line 17, Type mismatch error. Expected Ptr, but got mainWindow.TCPSocket1, TCPsocket1(i).setPostContent bodyFld.text,"") Top sbeach Post subject: Re: addRow in For...Next loop dumb problem...Posted: Sun Oct 25, 2009 11:31 pm Joined: Wed May 10, 2006 1:27 pm Posts: 23 rlinsurf wrote:When I put in the code you posted, I get this: Code, mainWindow.PushButton1.Action, line 17, Type mismatch error. Expected Ptr, but got mainWindow.TCPSocket1, TCPsocket1(i).setPostContent bodyFld.text,"") Strange- Did you make TCPSocket1 a control array and duplicate it 21 times? The index property (second item down in the properties list on the right of your ide) needs to be set to zero before you start duplicating. rlinsurf wrote:Wow. That first seems incredibly interesting -- and complex. Um, could you step me through how I would do that (Please feel free to say no.)? Sure, I'd gladly walk you through it, though dont misread me- the control array solution isn't a bad solution, but it doesn't allow for the number of sockets to change over time or with different result sets (though your current code and description indicate this wont happen). So I'm treating this response as more of a practice/learning situation than a solution. Also, the following assumes you only have one instance of mainWindow. If this isn't the case, then the following solution would not be complete or ideal. There are a few ways to do it, some more complex than others. For the sake of making this somewhat simple we'll do it by: creating a new classmaking an empty array of that class in the windowadding sockets to the array as neededpurging unused sockets at the top of each button push A more 'complex' solution would involve having the sockets remove themselves from memory when they're no longer useful, but this way is a tad simpler. So make a new class in your project, lets name it jeffSocket and set it's super class to HTTPSocket (or whatever the super of TCPSocket1 is). Double click it to edit it and you'll see basically the same thing as you did when you double clicked the one in mainWindow. First lets add a boolean property cmd-opt-p or click the Add Property toolbar. Lets name it something fun like "useless" and make sure its set to false when this socket is created. So the property declaration would be: useless as boolean = false Now, lets add your code to the PageReceived event: Code:dim c as string c = chr(9)+chr(9)+chr(9)+replace(Mid(content, instr(1,content,"_jotttext")+11), Mid(content, instr(1,content,"</textarea>")),"")+chr(9)+replace(Mid(content, instr(1,content,"tm=new Date(")+13, 36), "');docu","") mainWindow.jottList.addrow "" mainWindow.jottList.Cell(jottList.LastIndex, -1)=c useless = true Note that this code is out of scope from your listbox jottList. So I added the reference to mainWindow on the second-to-last two lines so we could still access the jottList control. I also added the last line "useless = true" this is so our script knows that we can delete the socket, its no longer any good. You'll also want to add the "useless = true" code to the "Error" event, in case our socket didn't complete its task. While we're here, lets make a constructor for our new class. So add a method, cmd-opt-m or Add Method from the toolbar. Name the method "Constructor" and in the parameters field put "pc as string, url as string". Now put the following code in the method: Code:me.setPostContent(pc,"") me.post(url) Lets go back to mainWindow and change a few things. First in the editor, lets create an array to hold our new jeffSocket. So add a property with the declaration "socks(-1) as jeffSocket". Next lets add a method to the window called "cleanSocks", no parameters or return value are neccessary. Put the following code: Code:dim i as integer = 0 do until i > socks.ubound if socks(i).useless then socks.remove i else i = i + 1 end if loop Now lets modify the push button's code: Code:Dim f as FolderItem Dim stream as TextInputStream Dim count, i as Integer Dim s, searchStr, url as String //get rid of the old dirty socks... sockets. cleanSocks f=GetFolderItem("/Users/jeffrey/Downloads/html/247.html",_ FolderItem.PathTypeShell) If f.exists then stream=f.OpenAsTextFile s=stream.ReadAll() searchStr="_jottId"""+" value=""" count=CountFields(s, searchStr) For i=2 to 21 url=trim("http://jott.com/show.aspx?id="+Mid(s.NthField(searchStr,i), 1, 36)) socks.append new jeffSocket(bodyFld.text,url) Next stream.Close else MsgBox "The folderitem does not exist." End if _________________ -Sean Top rlinsurf Post subject: Re: addRow in For...Next loop dumb problem...Posted: Sun Oct 25, 2009 11:49 pm Joined: Tue Mar 21, 2006 12:49 am Posts: 40 That. Is. Amazing. I understood about 1/8 of it, but it works! Thank you Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 5 posts ]
-- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
