Jump to content

Any way to reduce the code?


 Share

Recommended Posts

I have a script where I have a user input up to three ip addresses. I them output them to a file. The entrys need to be comma deliminated. I got the script to work but wonder if there is a better way to do it. He is an example script of what I am tring to do:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)

Global $Input1, $Input2, $Input3, $ip1, $ip2, $ip3
$notepad = "notepad.exe"
$Form2 = GUICreate("Form2", 225,280, 194, 122)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form2Close")
$Input1 = GUICtrlCreateInput("Input1", 24, 40, 145, 24)
GUICtrlSetOnEvent($Input1, "Input1Change")
$Input2 = GUICtrlCreateInput("Input2", 24, 72, 145, 24)
GUICtrlSetOnEvent($Input2, "Input2Change")
$Input3 = GUICtrlCreateInput("Input3", 24, 104, 145, 24)
GUICtrlSetOnEvent($Input3, "Input3Change")
$Button1 = GUICtrlCreateButton("Go", 24, 168, 57, 17)
GUICtrlSetOnEvent($Button1, "Button1Click")
$Button2 = GUICtrlCreateButton("Exit", 144, 168, 57, 17)
GUICtrlSetOnEvent($Button2, "Button2Click")
$Group1 = GUICtrlCreateGroup("Group1", 8, 16, 205, 217)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlSetOnEvent($Button1, "Button1Click")
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
While 1
Sleep(100)
WEnd
Func Button1Click()
MsgBox(0, "Info - GWDocMake", "IP address entrys are item1= " & $ip1 & " Item2= " & $ip2 & " Item3= " & $ip3,4)
SplashTextOn("Info", "Creating Item configuration", 380, 55)
Sleep(3000)
SplashOff()
GWDocMake()
EndFunc ;==>Button1Click
Func Button2Click()
Exit
EndFunc ;==>Button2Click
Func Form2Close()
GUIDelete($Form2)
Exit
EndFunc ;==>Form2Close
Func Input1Change()
$ip1 = GUICtrlRead($Input1)
EndFunc ;==>Input1Change
Func Input2Change()
$ip2 = GUICtrlRead($Input2)
EndFunc ;==>Input2Change
Func Input3Change()
$ip3 = GUICtrlRead($Input3)
EndFunc ;==>Input3Change
Func GWDocMake()
SplashTextOn("Info", "Creating Item List Document", 380, 55)
Sleep(3000)
SplashOff()
$ItemDoc = "c:" & "ItemsList.txt"
$oputfile = FileOpen($ItemDoc, 10) ; => Check if file is opened for writing successfully
If $oputfile = -1 Then
MsgBox(0, "Error", "Unable to create the gateway document." & $oputfile)
Exit
EndIf
If $ip3 = "" And $ip2 = "" And $ip1 = "" Then
MsgBox(0, "Info", "No items were entered!")
Return
ElseIf $ip3 <> "" Then
MsgBox(0, "IP3 is populated", "Item list is " & "set to " & $ip1 & "," & $ip2 & "," & $ip3,4)
FileWrite($oputfile, "########## Three items were entered. They were " & $ip1 & ", " & $ip2 & ", " & $ip3 & "############" & @CRLF)
FileWrite($oputfile, "########## All Done! ############" & @CRLF)
ElseIf $ip2 <> "" Then
MsgBox(0, "ip2 is populated", "Item List is " & "set to " & $ip1 & "," & $ip2,4)
FileWrite($oputfile, "########## Two items were entered. They were " & $ip1 & ", " & $ip2 & " " & "############" & @CRLF)
FileWrite($oputfile, "########## All Done! ############" & @CRLF)
ElseIf $ip1 <> "" Then
MsgBox(0, "ip1 is populated", "Item List is " & "set to " & $ip1,4)
FileWrite($oputfile, "########## One Item was entered. It was " & $ip1 & " " & "############" & @CRLF)
FileWrite($oputfile, "########## All Done! ############" & @CRLF)
Else
;Continue on
;MsgBox(4096, "Warning!", "You did not enter any items!", 4)
FileWrite($oputfile, "########## Test for Item List ############" & @CRLF)
FileWrite($oputfile, "########## No entries were made! ##########" & @CRLF)
EndIf
FileClose($oputfile)
Sleep ( 1000 )
MsgBox(0,"Info", "Here is what was written to the file.",4)
Run($notepad & $oputfile) ;"notepad.exe " & $GWDoc)
If @error Then
Run("notepad.exe " & "c:itemslist.txt")
Else
MsgBox(0, "Error", "Unable to open Notepad!", 4)
EndIf
EndFunc ;==>GWDocMake

Any ideas would be appreciated!

Me

Edited by IvanCodin
Link to comment
Share on other sites

Love the forum!! You guys are great and provide useful ideas!

I have never done a array before.:-( I don't understand then very well. I read the helpfile and interpret your array like this this:

\

For $i = 1 To 3 ; This would be the number of entries I want to test for.
$sVariable = "ip" & $i  ; This would set the list entry This would step me through each of the entires.
$iIPNum = Eval($sVariable) If $iIPNum <> "" Then ;You changed the variable name to test it.  Why do we do this?
;Do whatever ;Add my filewrite here based on entires in the list
EndIf
Next ; Does this exit the test and move on to the next item in the script or does it start the eval over again?
Link to comment
Share on other sites

You got the majority of it down. But the Next statement is used to exit a loop this is what we call recursive programming where you iterate through the same code changing variables aka stepping through and array. If you wish to learn about arrays follow the link below.

http://www.autoitscript.com/wiki/Arrays

I'm glad I could help and comeback if you need more assistance.

*Edit

$iIPNum = Eval($sVariable) If $iIPNum <> "" Then ;You changed the variable name to test it. Why do we do this?

I used an unnecessary variable there in hope that it would be more understandable. Edited by Decipher
Spoiler

censored.jpg

 

Link to comment
Share on other sites

Guess if I can understand your response (clever) I should be able to understand your initial recommendation!

So what you are sating is:

$clue = 1
If $clue = 1 Then
  MsgBox(0,"Yes", "You got it")
ElseIf $clue < 1 Then
MsgBox(0,"Idiot", "Read the help file!")
Else
MsgBox(0, "RTFM", "Read the helpfile")
Endif

Me

Link to comment
Share on other sites

I don't think Decipher called you an idiot.

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

  • Moderators

Decipher,

Take a look at the Recursion tutorial in the Wiki - the final 2 scripts show the difference clearly. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Decipher,

My pleasure. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Decipher,

You are using the eval statment wrong. Perhaps you are trying to do something like this

local $ip1 = '111.111.111', $ip2 = '222.222.222', $ip3 = '333.333.333'

For $i = 1 To 3
    ConsoleWrite(eval('ip' & $i) & @LF)
Next

This

$iIPNum = Eval($sVariable)
resolves to "" and set @error to something other than 0.

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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...