Jump to content

Answers to Exercises for AutoIt


Recommended Posts

6 minutes ago, czardas said:

Why do I get that funny feeling that someone's thinking of completely redrafting the Help File

If you say that 3 times into a silvered mirror then @guinness will appear and smite you.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

I think topic about answer is going out of the scope (no variables one haha) Because answer is already answered.
If were talking about an answer. czardas's one is clear and correct at least for me.
the one with ternary operator is correct too. But We're no talking about make our code shorter.

 

talking about out topic (variable scope) I usually do something like this.

;~ when I do  Scripts with many functions I put all my global variables at script's top with the Global keyword.
#Region Sample-1
Global $g_sSomething="Something"
;do something here

Func _Func1()
    ;probably use my $g_sSomething
EndFunc

Func _Func2()
    ;probably use my $g_sSomething
    Local $sSomething="Something local here" ;similar name as global one but using variable naming
EndFunc
#EndRegion

 

;But when I create Scripts (usally small scripts) without functions(Predefined) I use always Local keyword
#Region Sample-2
local $g_sSomething="Something"
ConsoleWrite($g_sSomething & @CRLF)
#EndRegion

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

Hmmm. So from now on I will explain to beginners : "you need a global variable ? then use Local, because Global is not reliable"
And when they will ask me : "so what is the usefulness of this Global keyword mentioned a lot of times in the help file ?"
I will answer : "I have a temporary headache. Please ask czardas"  :)

Link to comment
Share on other sites

reminding your future self (or whomever is unlucky enough to maintain your code) of what you were doing.

I believe I have also heard: "Just in case future AutoIt devs make changes, you might want to follow the same conventions they are going to."

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

I apologise @czardas for hijacking this thread, but thought I would clear a few things up ...

  1. I am not working on a new version of the Help File, as the current one seems just fine to me
  2. My opinions about Local being used in the help files still stand as correct, especially when you look at it from the point of view that the variable(s) are "local to the scope" i.e. imagine that big space outside of all the functions defined, is wrapped in an imaginary Main function. Since the likes of C/C++/C# require a function entry point normally labelled "main"
    ; This line is used elsewhere
    Global $sGlobalVar = ''
    
    ; This line can be seen as ...
    Local $sLanguage = 'JavaScript'
    
    ; ...  this line, since the variable is not used anywhere else
    Func Main()
        Local $sLanguage = 'JavaScript'
        ConsoleWrite($sLanguage)
    EndFunc
    
    Function SomeOtherFunc()
        $sGlobalVar = 'A truly globally variable'
    EndFunc


    Disclaimer: I am mainly using JavaScript these days anyway, in which I have created my own Promise and Observable polyfills, plus I use the highly respected Airbnb Style Guide for writing clean and understandable JavaScript.

Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

@guinness Please tell me if I have misunderstood you. I see variables which ought to be local (or inside a function) as distinct from those which ought to be global. I generally declare them according to these principles, and nearly all my variables end up inside functions: making everything pretty much watertight. I try to follow your lead (secretly :whisper:).

So one quick question: would you have used the Global keyword or the Local keyword for the declaration of $iHour in the first post?

Edited by czardas
Link to comment
Share on other sites

In that example I would use Local, unless that variable is used also within another scope i.e. a function declaration, which in that case it should be Global.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

The following is my suggested solution for the second exercise I posted.



Exercise (see link) - post #15

The most obvious problem affecting the countdown is the instruction to exit the loop at the end of the first run. After removing the ExitLoop instruction, you will see that it made no difference and that the countdown is still broken. It is not uncommon to find more than one issue causing the same, similar or related problems. In this case there are two issues preventing the countdown. If you look at Help File example for 'Do . . . Until', you will notice that the last line of the loop ends with an expression using the comparison operator '=' (NOTE: the equals sign is also used as an assignment operator - these are two different things). So what must countdown equal when we blast off?

#include <MsgBoxConstants.au3>

Local $iCountDown = 5

Do
    MsgBox($MB_OK, "Houston", $iCountDown, 1)
    Sleep(1000)

    $iCountDown -= 1
Until $iCountDown = 0

MsgBox($MB_OK, "Houston", "Blast Off", 1)

Now we have fixed the problem, we send the module back to Nasa but they are still not satisfied. Why is that?

Further exercises
1. Rewrite the code using a While . . . WEnd loop.
2. Implement an option to abort the countdown using a hotkey.

Use the Help File to assist you.

Edited by czardas
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...