timmy2 Posted September 27, 2013 Share Posted September 27, 2013 (edited) I'm in the mood to learn how best to handle a simple coding challenge instead of repeating my old brute force approach. (But I am not seeking to be criticized and told to "read the help file", "take the tutorials", or "don't ask people to do your programming for you." There's art in good programming, which is what I'm hoping to get a glimpse of.) Call ("Routertest1") ; ping the router to see if it responds If $results1 > 0 Call("Routertest2") Else ;if first test fails, try something else MsgBox(1,"First test results","Routertest1 passed.") ;if the first test passed, inform user EndIf If $results2 > 0 Call("PowerCycleRouter") Else ;if second attempt failed tell user how to power-cycle the router MsgBox(1,"Second test results","Releasing and renewing IP fixed the problem.") ;or, if the second attempt worked, tell them the good news EndIf ; continue with remainder of program ; functions Func Routertest1 ;display a graphic about what's going to happen ;ping the router ;$results1 variable returns with 0 if there was a ping response or > 0 if no response Endfunc Func Routertest2 ;display a graphic about what's going to happen ;release and renew the IP address ;ping the router ;$results2 variable returns with 0 if there was a ping response or > 0 if no response EndFunc Func PowerCycleRouter ;display a graphic about power cycling a router ;after waiting a suitable period ping the router ;if pinging fails this time display a graphic about calling for help ;if pinging succeeds display a graphic about that and return to resume rest of program EndFunc Edited September 27, 2013 by timmy2 Link to comment Share on other sites More sharing options...
FireFox Posted September 27, 2013 Share Posted September 27, 2013 Call("myfunc") equals to: myfunc()For the rest take a look at the Switch/Select statements.Br, FireFox. Link to comment Share on other sites More sharing options...
JohnOne Posted September 27, 2013 Share Posted September 27, 2013 (edited) I'm in the mood to learn how best to handle a simple coding challenge instead of repeating my old brute force approach. Here's what I've come up with: Call ("Routertest1") if $results1 > 0 Call("Routertest2") Else To much information there. see if you can hold back a little. Edited September 27, 2013 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
timmy2 Posted September 27, 2013 Author Share Posted September 27, 2013 (edited) Call("myfunc") equals to: myfunc() For the rest take a look at the Switch/Select statements. Br, FireFox. Thanks for the tip re myfunc(), FireFox. I've seen the latter approach if a function is used to return a value or be employed in a conditional statement but not just on a line by itself. May play with it but unless I'm missing something it doesn't seem to be a profound solution to my immediate goal. As to Switch/Select, I've studied examples of its use in the past but I haven't grasped it's value or applicability, at least not in this particular case. I think with a language one must see some non-trivial uses of a statement to appreciate how it might be employed. The example in the Help file is obvious, and not inspiring enough to lead to appreciating Switch/Select's usability. Edited September 27, 2013 by timmy2 Link to comment Share on other sites More sharing options...
FireFox Posted September 27, 2013 Share Posted September 27, 2013 (edited) It was to tell you that you don't need to use the Call function unless you don't know in advance the function to be called (by a variable). Then tell what you want to do with more details. Forget, just saw you edited your 1st post. I see Melba here so I guess he's cooking something for you. Br, FireFox. Edited September 27, 2013 by FireFox Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 27, 2013 Moderators Share Posted September 27, 2013 timmy2,I would structure your code like this:If Routertest1() > 0 Then ; ping the router to see if it responds ; if first test fails, try something else If Routertest2() > 0 ; if second attempt failed tell user how to power-cycle the router If PowerCycleRouter() Then ; display a graphic about that and return to resume rest of program Else ; display a graphic about calling for help Exit ; presumably? EndIf Else ; or, if the second attempt worked, tell them the good news MsgBox(1,"Second test results","Releasing and renewing IP fixed the problem.") EndIf Else ; if the first test passed, inform user MsgBox(1,"First test results","Routertest1 passed.") EndIf ; continue with remainder of program ; functions Func Routertest1 ; Return with 0 if there was a ping response or > 0 if no response Endfunc Func Routertest2 ; Return with 0 if there was a ping response or > 0 if no response EndFunc Func PowerCycleRouter ;display a graphic about power cycling a router ;after waiting a suitable period ping the router ;Return True/False EndFuncI hope that helps. Switch/Select basically replace a whole string of If statments - does this make it any clearer? If $x = 0 Then ; ElseIf $x = 1 ; ElseIf $x = 2 ; EndIf ; ------------- Select Case $x = 0 ; Case $x = 1 ; Case $x = 2 ; EndSelect ; ------------- Switch $x Case 0 ; Case 1 ; Case 2 ; EndSwitchPersonally I always go for Switch if at all possible - much clearer and easier to debug. M23 timmy2 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
timmy2 Posted September 27, 2013 Author Share Posted September 27, 2013 It was to tell you that you don't need to use the Call function unless you don't know in advance the function to be called (by a variable). Br, FireFox. I guess I'm also in a mindset to be dense today. Sorry! In what situation would I not "know in advance the function to be called"? Link to comment Share on other sites More sharing options...
timmy2 Posted September 27, 2013 Author Share Posted September 27, 2013 (edited) Melba23, thank you! Before starting this thread I had considered nesting the IF statements but before doing so I figured I should see if there's an entirely different and more artful way to accomplish my goal. I've only skimmed your example but I wanted to thank you for adding some illumination to the Switch and Select statements. I'll study your examples and see if I can figure out how to apply it to my goal. Edited September 27, 2013 by timmy2 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 27, 2013 Moderators Share Posted September 27, 2013 timmy2,Glad it was useful. In what situation would I not "know in advance the function to be called"?I have a toolbar script which adds functionality to SciTE - the names of the apps it calls are stored in an array. When the toolbar is clicked, it is easy to get the index of the clicked item and then look up the app name in the array - which needs to be run with Call as it is a text string. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
timmy2 Posted September 27, 2013 Author Share Posted September 27, 2013 (edited) Switch/Select basically replace a whole string of If statments - does this make it any clearer? If $x = 0 Then ; ElseIf $x = 1 ; ElseIf $x = 2 ; EndIf ; ------------- Select Case $x = 0 ; Case $x = 1 ; Case $x = 2 ; EndSelect ; ------------- Switch $x Case 0 ; Case 1 ; Case 2 ; EndSwitch Personally I always go for Switch if at all possible - much clearer and easier to debug. M23 I've really tried to rewrite my code to use Switch or Select instead of using If statements, but both Switch and Select seem applicable only when considering multiple possible results for an expression. There's the ContinueCase statement that lets you take on another expression in the process, but I'm not seeing any benefit here because the process I'm conducting seems like a series of expressions each having a case or two, at most. Ping Router... Did Pinging Router Work? If Yes, you're done. If No, release/renew IP, then Ping again Did Ping Work now? If Yes, you're done. If No, here's how to power cycle the router. Afterwards, ping again. Did ping work? If Yes, you're done. If No, here's a handkerchief. Edited September 27, 2013 by timmy2 Link to comment Share on other sites More sharing options...
Solution JohnOne Posted September 27, 2013 Solution Share Posted September 27, 2013 Will a While...WEnd loop help you? timmy2 1 AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
timmy2 Posted September 28, 2013 Author Share Posted September 28, 2013 (edited) Will a While...WEnd loop help you? Thanks for the suggestion, JohnOne. At first glance it sounded perfect because I figured I'll make the While expression the result of ping tests, so as soon as a ping works the expression will go false and I'll fall out of the loop. But it turns out that even after one of the ping tests works and the expression goes false ALL of the statements until the WEnd are executed. Ping Router... Did Pinging Router Work? If Yes, you're done. If No, release/renew IP, then Ping again Did Ping Work now? If Yes, you're done. If No, here's how to power cycle the router. Afterwards, ping again. Did ping work? If Yes, you're done. If No, here's a handkerchief. The above sequence of steps doesn't need to be looped; it's a one shot series of messages, commands and ping tests. I realize I could insert an ExitLoop statement after each ping test, but then I'm back to using If statements so what's the advantage of putting it in a While/Wend loop? What needs to happen is as soon as a ping test works and "If Yes, you're done." is valid it's time to jump out of the above sequence. Edited September 28, 2013 by timmy2 Link to comment Share on other sites More sharing options...
mrider Posted September 28, 2013 Share Posted September 28, 2013 (edited) I suspect algorithm purists might not like this - but I'd be tempted to code the problem like this: algorithms = [Ping Router, Release/Renew and Ping, Power Cycle] success = false index = 0 while index < algorithms.count and success == false success = call algorithm[index] index += 1 wend if not success then cry endif For extra bonus points, you could call "Ping" from "Release/Renew", since you'll already have one of those anyway. Basically something like: func ReleaseRenew release() renew() return Ping() endfunc Edited September 28, 2013 by mrider How's my riding? Dial 1-800-Wait-There Trying to use a computer with McAfee installed is like trying to read a book at a rock concert. Link to comment Share on other sites More sharing options...
JohnOne Posted September 28, 2013 Share Posted September 28, 2013 (edited) Still not exactly sure what it is you're doing. From my guess, here is how I would start. expandcollapse popup$IP = "127.0.0." $oct = 1 While 1 If _Ping($IP & String($oct)) = 7 Then ConsoleWrite("+ Ping Success" & @LF) ExitLoop Else ConsoleWrite("! Ping Fail" & @LF) EndIf _IPRenew() If _Ping($IP & String($oct)) = 7 Then ConsoleWrite("+ Ping Success" & @LF) ExitLoop Else ConsoleWrite("! Ping Fail" & @LF) EndIf _PowerCycle() Sleep(1000) WEnd MsgBox(0, "yay", $IP & $oct) Func _IPRenew() ConsoleWrite("Renewing IP" & @LF) $oct += 1 Return EndFunc ;==>_IPRenew Func _PowerCycle() ConsoleWrite("Power Cycle" & @LF) Return EndFunc ;==>_PowerCycle Func _Ping($IP) ConsoleWrite("Pinging: " & $IP & @LF) Return Random(0, 20, 1) ;real return Return Ping($IP) EndFunc ;==>_Ping Off course you get tour ping IP's from somewhere else I imagine, and the result of Ping would not be 7. That's just to make example runable. Edited September 28, 2013 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
timmy2 Posted September 28, 2013 Author Share Posted September 28, 2013 (edited) Thank you to all who responded. After additional thought and another post I came up with the following code. It was inspired by JohnOne's suggestion to use While/WEnd. Note that I chose to use batch files to ping and release/renew because past readings of this forum suggested that the internal commands aren't reliable. That could've been obsolete info but I'd rather not chance it. . expandcollapse popupWhile 1 DisplayMsg("Testing connection to router.") If PingRouter() = 0 Then ExitLoop DisplayMsg("No initial response from router." & @LF & "Standby while I try fixing the problem.") Call ("IPConfig") If PingRouter() = 0 Then ExitLoop DisplayMsg("Router still not responding." & @LF & "Please power-cycle it.") MsgBox(0,"","After you've power-cycled it, wait 30 seconds, then click OK.") DisplayMsg("Testing connection to router.") If PingRouter() = 0 Then ExitLoop DisplayMsg("The router still didn't respond." & @LF & "Please call me.") MsgBox(0,"","Click OK to exit.") Exit WEnd MsgBox(0,"","The router responded!") Exit ;function to ping router Func PingRouter() Return Int(RunWait(@COMSPEC & " /c routertest.bat","",@SW_MINIMIZE)) EndFunc ;function to display messages Func DisplayMsg($msg) SplashTextOn("",$msg,300,100,-1,-1,1) Sleep(2000) SplashOff() EndFunc Func IPConfig() ;release IP address RunWait (@ComSpec & " /c " & "IPCONFIG.EXE /release",@SystemDir, @SW_HIDE) ;renew IP address Run(@ComSpec & ' /c ' & 'IPCONFIG /renew', @SystemDir, @sw_hide) ProgressOn("IP Release/Renew", "", "0 percent") For $i = 10 To 100 Step 10 Sleep(500) ProgressSet($i, $i & " percent") Next ProgressSet(100, "Done", "Complete") Sleep(500) ProgressOff() EndFunc Edited September 28, 2013 by timmy2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now