LisHawj

Need advice on writing better codes using array or ini.

12 posts in this topic

#1 ·  Posted

I'm looking for advices and alternatives to what I have done so far.  The script itself currently work but I am looking for advices on how to write some of the sections much better.  I am not very good at all with arrays and have shy away as much as possible from utilizing arrays because of my lack of knowledge on how to use it properly and how to grab data from it.  Below is a snip of a much large piece of coding that I am working on and would like some advices on how to handle the array piece in the If...End section and the Ini section.  Your experience is much appreciated and thank you very much in advance.

;Read the program.txt file into an array.
    If Not FileExists($Array_File) Then
        _FileCreate($Array_File)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "small_plus.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "yellow.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "green.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "blue.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "red.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "plus.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "widget.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "time.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "purple.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "elrond.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "orange.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "avocado.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "fruit.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "small_plus.ico" & @CRLF)
        FileWrite($Array_File, "Empty" & "," & "Empty" & "," & $Icon_dir & "\" & "Add_more.ico" & @CRLF)
    ElseIf FileExists($Array_File) Then
        Sleep(10)
    EndIf

    ;Read to array, buttons/apps will grab variable from array table.
    _FileReadToArray($Array_File, $aArray, "", ",")
    ;_ArrayDisplay($aArray)
    
    ;Check the status of a button.  
    $x1 = IniRead($B_state, "Enabled", "Button 1", "")
    $x2 = IniRead($B_state, "Enabled", "Button 2", "")
    $x3 = IniRead($B_state, "Enabled", "Button 3", "")
    $x4 = IniRead($B_state, "Enabled", "Button 4", "")
    $x5 = IniRead($B_state, "Enabled", "Button 5", "")
    $x6 = IniRead($B_state, "Enabled", "Button 6", "")
    $x7 = IniRead($B_state, "Enabled", "Button 7", "")
    $x8 = IniRead($B_state, "Enabled", "Button 8", "")
    $x9 = IniRead($B_state, "Enabled", "Button 9", "")
    $x10 = IniRead($B_state, "Enabled", "Button 10", "")
    $x11 = IniRead($B_state, "Enabled", "Button 11", "")
    $x12 = IniRead($B_state, "Enabled", "Button 12", "")
    $x13 = IniRead($B_state, "Enabled", "Button 13", "")
    $x14 = IniRead($B_state, "Enabled", "Button 14", "")

    If IsArray($aArray) Then
        If $x1 = "Enabled" Then
            GUICtrlSetState($Button1, $GUI_ENABLE)
            $Icon = $aArray[0][2]
            GUICtrlSetImage($Button1, $Icon, 1)
        EndIf
        If $x2 = "Enabled" Then
            GUICtrlSetState($Button2, $GUI_ENABLE)
            $Icon = $aArray[1][2]
            GUICtrlSetImage($Button2, $Icon, 1)
        EndIf

        If $x3 = "Enabled" Then
            GUICtrlSetState($Button3, $GUI_ENABLE)
            $Icon = $aArray[2][2]
            GUICtrlSetImage($Button3, $Icon, 1)
        EndIf

        If $x4 = "Enabled" Then
            GUICtrlSetState($Button4, $GUI_ENABLE)
            $Icon = $aArray[3][2]
            GUICtrlSetImage($Button4, $Icon, 1)
        EndIf

        If $x5 = "Enabled" Then
            GUICtrlSetState($Button5, $GUI_ENABLE)
            $Icon = $aArray[4][2]
            GUICtrlSetImage($Button5, $Icon, 1)
        EndIf

        If $x6 = "Enabled" Then
            GUICtrlSetState($Button6, $GUI_ENABLE)
            $Icon = $aArray[5][2]
            GUICtrlSetImage($Button6, $Icon, 1)
        EndIf

        If $x7 = "Enabled" Then
            GUICtrlSetState($Button7, $GUI_ENABLE)
            $Icon = $aArray[6][2]
            GUICtrlSetImage($Button7, $Icon, 1)
        EndIf

        If $x8 = "Enabled" Then
            GUICtrlSetState($Button8, $GUI_ENABLE)
            $Icon = $aArray[7][2]
            GUICtrlSetImage($Button8, $Icon, 1)
        EndIf

        If $x9 = "Enabled" Then
            GUICtrlSetState($Button9, $GUI_ENABLE)
            $Icon = $aArray[8][2]
            GUICtrlSetImage($Button9, $Icon, 1)
        EndIf

        If $x10 = "Enabled" Then
            GUICtrlSetState($Button10, $GUI_ENABLE)
            $Icon = $aArray[9][2]
            GUICtrlSetImage($Button10, $Icon, 1)
        EndIf

        If $x11 = "Enabled" Then
            GUICtrlSetState($Button11, $GUI_ENABLE)
            $Icon = $aArray[10][2]
            GUICtrlSetImage($Button11, $Icon, 1)
        EndIf

        If $x12 = "Enabled" Then
            GUICtrlSetState($Button12, $GUI_ENABLE)
            $Icon = $aArray[11][2]
            GUICtrlSetImage($Button12, $Icon, 1)
        EndIf

        If $x13 = "Enabled" Then
            GUICtrlSetState($Button13, $GUI_ENABLE)
            $Icon = $aArray[12][2]
            GUICtrlSetImage($Button13, $Icon, 1)
        EndIf

        If $x14 = "Enabled" Then
            GUICtrlSetState($Button14, $GUI_ENABLE)
            $Icon = $aArray[13][2]
            GUICtrlSetImage($Button14, $Icon, 1)
        EndIf
    EndIf

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

There is a very good tutorial about arrays in the wiki. Worth reading it!

As a start:

#include <File.au3>
;Read the program.txt file into an array.#
Local $aIcons[] = ["small_plus", "yellow", "green", "blue", "red", "plus", "widget", "time", "purple", "elrond", "orange", "avocado", "fruit", "small_plus", "Add_more"]
Global $Array_File = "C:\temp\Array.txt", $Icon_dir = "C:\temp"
If Not FileExists($Array_File) Then
    _FileCreate($Array_File)
    For $i = 0 To UBound($aIcons) - 1
        FileWrite($Array_File, "Empty,Empty," & $Icon_dir & "\" & $aIcons[$i] & ".ico" & @CRLF)
    Next
Else
    Sleep(10)
EndIf

 

Edited by water
1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

Thanks, Water.  I sent you a private message with a question on your example. 

 

Share this post


Link to post
Share on other sites

#4 ·  Posted

@LisHawj for future reference, if you stick to the forum for any follow up questions, you will have many more eyes on the issue.

1 person likes this

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#5 ·  Posted

@JLogan3o13, you're absolutely correct.  I will post the same questions to members of the community.

 

Thanks, Water.  I am going to run through the wiki again and hope to absorb more than I did the first time around.  I took away just enough information to know how to get data into an array table and retrieve those data using the most rudimentary method.  However, I don't understand how to use it in the fashion that you provided in the example.  I think my issue is not understanding the format or the logic that is actually utilize and therefore am confusing myself.  Let me see if I understand your code example correctly and I apologize for being such a noob at this.  Your few lines of codes does the same thing my rudimentary logic does and that's what I want to understand and do better.  Is my understanding of your example correct?  Thank you.

#include <File.au3>
;Read the program.txt file into an array.#

;$aIcons[] is the array to store the icon values into temp memory instead of writing it outright to a file.  Yes/no?
Local $aIcons[] = ["small_plus", "yellow", "green", "blue", "red", "plus", "widget", "time", "purple", "elrond", "orange", "avocado", "fruit", "small_plus", "Add_more"]

;This part I understand that you're declaring the value for $Array_File and $Icon_dir and checking that $Array_file exist.
Global $Array_File = "C:\temp\Array.txt", $Icon_dir = "C:\temp"
If Not FileExists($Array_File) Then
    _FileCreate($Array_File)
    
    ;This is the part I am totally confuse about.  I am guessing you have declare a variable called $i and you're searching the array from top to bottom for the array declared as $aIcons and associating the returned value from the array to $i as it loop through and write the data to Array_file using $aIcons[$i] that contains the value from the array table.  Is that correct?
    For $i = 0 To UBound($aIcons) - 1
        FileWrite($Array_File, "Empty,Empty," & $Icon_dir & "\" & $aIcons[$i] & ".ico" & @CRLF)
    Next
Else
    Sleep(10)
EndIf

Share this post


Link to post
Share on other sites

#6 ·  Posted

  1. Correct. With $aIcons[3] I would define an array with 3 elements. As counting in arrays starts with 0 I would need to use an index from 0 to 2 to access all elements of the array. As the above definition does not contain a counter the array's size is defind by the number of elemens in the following list (["small_plus", "yellow" ...]). If you need to add another icon simply extend the list.
  2. Correct. You do not need to define all variables (AutoIt does it for you when you access a varaible for the first time). But defining the variables leads to cleaner code.
  3. $i is the variable I (and most other coders) use for the index variable. This variable defines which row of the array to access. As I mentioned above the index of an array starts with 0 and ends with the number of rows minus 1. UBound($aIcons) by default returns the number of rows in an array. To get the highest index I have to subtract 1. The next statement then accesses row by row and creates the output lin.

Imagine a one-dimensional (1D) array as a list of values each one in its own row.
Imagine a two-dimensional (2D) array as an Excel worksheet with rows and columns. Excel uses a column/row notation (e.g. B4 - column B (= column 2) row 4). An array uses a row/column notation. So in an outer loop you specify which row to process (as we did above) and in an inner loop you specify which column of the current row to process.

Hope I could shed some light on the subject ;)

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#7 ·  Posted

Wow! I honestly just gained 1000 EXP from your explanation on how to access data from an array.  I knocked off 142 lines from my codes incorporating your sample code and tweaking it to fit my script.  This is continuing education!  Thank you!!

Share this post


Link to post
Share on other sites

#8 ·  Posted

The second section of your script could be enhanced and shortened as well. Needs a bit more effort but will make your code easier to read and much shorter.
You have a section in your ini file named "Enabled". Do you have a section "Disabled" as well or do you disable all buttons by default and only enable those from the "enabled" section?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

You could set the state of the buttons in a loop and check which button to process in a loop as well. If needed even the creation of the buttons could be done in a loop:

#include <GUIConstantsEx.au3>
#include <Array.au3>

Global $aButtonLabel = IniReadSection(@ScriptDir & "\test.ini", "BUTTON-LABEL") ; Read section with button labels
Global $aButtonState = IniReadSection(@ScriptDir & "\test.ini", "BUTTON-STATE") ; Read section with button states
Global $aButtonID[3] ; IDs of the buttons in the GUI

; Create a GUI with various controls.
Local $hGUI = GUICreate("Example", 300, 200)

; Create a button controls (could be done in a loop as well reading all neded data (label, position from the ini file)
$aButtonID[0] = GUICtrlCreateButton($aButtonLabel[1][1], 20, 20, 85, 25) ; Button 1
$aButtonID[1] = GUICtrlCreateButton($aButtonLabel[2][1], 20, 50, 85, 25) ; Button 2
$aButtonID[2] = GUICtrlCreateButton($aButtonLabel[3][1], 20, 80, 85, 25) ; Button 3
; Set the state of the buttons
For $i = 0 To UBound($aButtonID) - 1
    If $aButtonState[$i+1][1] = "Enabled" Then
        GUICtrlSetState($aButtonID[$i], $GUI_ENABLE)
    Else
        GUICtrlSetState($aButtonID[$i], $GUI_DISABLE)
    EndIf
Next
Local $idCloseButton = GUICtrlCreateButton("Close", 210, 170, 85, 25)

; Display the GUI.
GUISetState(@SW_SHOW, $hGUI)

; Loop until the user exits.
While 1
    $iMsg = GUIGetMsg() ; returns the ID of the button pressed
    If $iMsg = $GUI_EVENT_CLOSE Or $iMsg = $idCloseButton Then Exit
    ; Check which button was pressed
    For $i = 0 To UBound($aButtonID) - 1
        If $iMsg = $aButtonID[$i] Then MsgBox(0, "Info", "Button '" & $aButtonLabel[$i+1][1] & "' pressed!")
    Next
WEnd

Test.ini:

Quote

[BUTTON-LABEL]
Button1=Button 1
Button1=Button 2
Button3=Button 3
[BUTTON-STATE]
Button1=Enabled
Button2=Disabled
Button3=Enabled

 

Edited by water
1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Example where the buttons get created and the icons get set in a loop:

#include <GUIConstantsEx.au3>
#include <Array.au3>
#include <Math.au3>

Global $aButtonLabel = IniReadSection(@ScriptDir & "\test.ini", "BUTTON-LABEL") ; Read section with button labels
Global $aButtonState = IniReadSection(@ScriptDir & "\test.ini", "BUTTON-STATE") ; Read section with button states
Global $aButtonIcon  = IniReadSection(@ScriptDir & "\test.ini", "BUTTON-ICON")  ; Read section with button icons
; Make sure all arrays have the same number of rows. Resize if needed
Global $iMax = _Max(UBound($aButtonLabel), UBound($aButtonState))
$iMax = _Max($iMax, UBound($aButtonIcon))
ReDim $aButtonLabel[$iMax][2]
ReDim $aButtonState[$iMax][2]
ReDim $aButtonIcon[$iMax][2]

Global $aButtonID[$aButtonLabel[0][0]] ; IDs of the buttons in the GUI. The number of rows in this array is determined by the number of buttons from the ini file
Global $iButtonPos = 20 ; Top position of the first button
Global $iButtonOffset = 30 ; Offset of the next button

; Create a GUI with various controls.
Local $hGUI = GUICreate("Example", 300, 200)

; Create button controls and set state
For $i = 1 To $aButtonLabel[0][0] ; Index starts with 1 as all arrays returned by IniRead* functions have the row/column count in row 0
    $aButtonID[$i-1] = GUICtrlCreateButton($aButtonLabel[$i][1], 20, $iButtonPos, 85, 25)
    ; Use an icon for the button
    GUICtrlSetImage($aButtonID[$i-1], $aButtonIcon[$i][1], 1)
    ; Set the state of the button
    If $aButtonState[$i][1] = "Enabled" Then
        GUICtrlSetState($aButtonID[$i-1], $GUI_ENABLE)
    Else
        GUICtrlSetState($aButtonID[$i-1], $GUI_DISABLE)
    EndIf
    $iButtonPos = $iButtonPos + $iButtonOffset
Next
; Create close button
Global $idCloseButton = GUICtrlCreateButton("Close", 210, 170, 85, 25)

; Display the GUI
GUISetState(@SW_SHOW, $hGUI)

; Loop until the user exits
While 1
    $iMsg = GUIGetMsg() ; returns the ID of the button pressed
    If $iMsg = $GUI_EVENT_CLOSE Or $iMsg = $idCloseButton Then Exit
    ; Check which button was pressed
    For $i = 0 To UBound($aButtonID) - 1
        If $iMsg = $aButtonID[$i] Then MsgBox(0, "Info", "Button " & $i+1 & " labeled '" & $aButtonLabel[$i+1][1] & "' pressed!")
    Next
WEnd

Example of the test.ini:

[BUTTON-LABEL]
Button1=Button 1
Button2=Button 2
Button3=Button 3

[BUTTON-STATE]
Button1=Enabled
Button2=Disabled
Button3=Enabled

[BUTTON-ICON]
Button1=C:\temp\alarmGreen.ico
Button2=C:\temp\alarmRed.ico
Button3=C:\temp\alarmYellow.ico

 

Edited by water
1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#11 ·  Posted

The buttons are disabled by default and is enabled when the script polls the INI file (certain criteria must be met).  I am going to rewrite all the sections that could benefit from utilizing an array instead of declaring every variables with their associated values.  I am seeing where your code make so much more sense vs. what I currently have in place.  Thank you for the knowledge flow or sharing because this have made a huge impact on my understanding of arrays and how to use them.

 

Thank you!

Lis Hawj

 

Share this post


Link to post
Share on other sites

#12 ·  Posted

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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