Sign in to follow this  
Followers 0
JohnWilson

Dynamic Variables from an Array

9 posts in this topic

Hello Everyone, 

I currently have a project that I am working on that takes each item from an array and then converts it to a checkbox that allows the user to install a program or programs depending on what boxes they have checked. So far the code displays the array, and shows the check boxes, but all of them are being given the same path to run. Below you will see an example script. 

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

Local $arr[6] ; Make room for three elements
 ;Assign some data
 $arr[0]="Notepad.exe"
 $arr[1]="Calculator.exe"
 $arr[2]="Notepad.exe"
 $arr[3]="Calculator.exe"
 $arr[4]="Notepad.exe"
 $arr[5]="Notepad.exe"

_ArrayDisplay ($arr)

GUICreate("Installs for Computers", 600, 300)
GUICtrlCreateLabel("Installs for Computers", 30, 10)

$coordsy = 0

For $item in $arr
    $parse = $item
    $checker = GUICtrlCreateCheckbox ($item, 10, $coordsy, 120, 20)
    $coordsy += 20

Next

$okbutton = GUICtrlCreateButton("OK", 70, 275, 60)
GUISetState(@SW_SHOW)

While 1
$msg = GUIGetMsg()

Select
Case $msg = $okbutton
MsgBox(0, "GUI Event", "The Installs will now begin")


If GUICtrlRead($checker) = $GUI_CHECKED THEN <-- PART FIXED

     RunWait(@ComSpec & ' /c  start ' & $parse, '', @SW_HIDE)

Exit


Case $msg = $GUI_EVENT_CLOSE
MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
ExitLoop
EndSelect
WEnd

This is just a simple sample, the actual working code is on my work computer. You will see when you run the above script that the script will give all checkboxes the same executable path. So for instance, even the checkboxes marked for calculator.exe will still run notepad. I believe there must be a way for me to create dynamic variables from the array that can then be assigned properly, but I can not figure out how to do it. I have searched all over the place and seen some similar ideas, but nothing that I could get working. I would appreciate any help you guys could give. 

Thank you in advance!!!

Share this post


Link to post
Share on other sites



Try this modification of your script. It uses 2 arrays, one to hold the programs to run, the other holds the control IDs of the checkboxes created.

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

Local $arr[6] ; Make room for three elements
;Assign some data
$arr[0] = "Notepad.exe"
$arr[1] = "Calculator.exe"
$arr[2] = "Notepad.exe"
$arr[3] = "Calculator.exe"
$arr[4] = "Notepad.exe"
$arr[5] = "Notepad.exe"
Global $ControlIDs[UBound($arr)]
_ArrayDisplay($arr)

GUICreate("Installs for Computers", 600, 300)
GUICtrlCreateLabel("Installs for Computers", 30, 10)


For $item = 0 to UBound($arr) -1
    $ControlIDs[$Item] = GUICtrlCreateCheckbox($arr[$item], 10, $item * 20, 120, 20) ; create checkboxes and store their control IDs
Next

$okbutton = GUICtrlCreateButton("OK", 70, 275, 60)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $okbutton
            MsgBox(0, "GUI Event", "The Installs will now begin")

            For $Loop = 0 To UBound($arr) - 1 ; Loop through the checkboxes
                If GUICtrlRead($ControlIDs[$Loop]) = $GUI_CHECKED Then ; Find if it's checked

                    RunWait(@ComSpec & ' /c  start ' & $arr[$Loop], '', @SW_HIDE) ; run the program stored in the same position in the $arr array
                EndIf
            Next
            Exit
        Case $msg = $GUI_EVENT_CLOSE
            MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
            ExitLoop
    EndSelect
WEnd
1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Here you go.

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

Local $arr[6][2] ; Make room for three elements
 ;Assign some data
 $arr[0][0]="Notepad.exe"
 $arr[1][0]="Calculator.exe"
 $arr[2][0]="Notepad.exe"
 $arr[3][0]="Calculator.exe"
 $arr[4][0]="Notepad.exe"
 $arr[5][0]="Notepad.exe"

_ArrayDisplay ($arr)

GUICreate("Installs for Computers", 600, 300)
GUICtrlCreateLabel("Installs for Computers", 30, 10)

$coordsy = 10

For $i = 0 To UBound($arr)-1
    $arr[$i][1] = GUICtrlCreateCheckbox ($arr[$i][0], 10, $coordsy, 120, 20)
    $coordsy += 20
Next

$okbutton = GUICtrlCreateButton("OK", 70, 275, 60)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()

    Select


    Case $msg = $okbutton
    MsgBox(0, "GUI Event", "The Installs will now begin")

    For $i = 0 to UBound($arr)-1
        If GUICtrlRead($arr[$i][1]) = 4 THEN

            ConsoleWrite(@ComSpec & ' /c  start ' & $arr[$i][0] & @CRLF)
        EndIf
    Next

    Exit


    Case $msg = $GUI_EVENT_CLOSE
        MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
        ExitLoop
    EndSelect
WEnd

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

Or you could do it that way. :)


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Thank you, thank you, thank you! You guys are awesome, I appreciate your help. One question though, will there be any modifications I need to make if my array is dynamically created from search results? In the example I gave, I declared the number of elements, but in the working script, the elements will be added to the array when found in a file. If you need any more information, let me know. Thanks again!

Share this post


Link to post
Share on other sites

I realized I should give more details to really get this resolved. Here is my code.

#include <Array.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstants.au3>
#include <File.au3>
#include <InetConstants.au3>
#include <GuiButton.au3>
#include <WindowsConstants.au3>

#RequireAdmin

Func Octet()

; Creates array called parsed ip, with each octet of the IP address as different items
Global $parsedip = StringSplit(@IPAddress1, ".")

; Creates variable called third which grabs only the third octet
Global $third = $parsedip[3]

EndFunc

Octet()

Local $sFileName = "C:\Testing\test.txt"
Local $searchStr = $third

$arr = _LineNumsOfSearchStr($sFileName, $searchStr, False)


Global $ControlIDs[UBound($arr)]
; _ArrayDisplay($arr)

GUICreate("Installs for Computers", 600, 300)
GUICtrlCreateLabel("Installs for Computers", 30, 10)


For $item = 0 to UBound($arr) -1
   $tester = FileReadLine ( $sFileName, $arr[$item] )
   $firstpart = StringSplit($tester, ",")
   $final = $firstpart[1]
   $ControlIDs[$Item] = GUICtrlCreateCheckbox($arr[$item], 10, $item * 20, 120, 20) ; create checkboxes and store their control IDs
Next

$okbutton = GUICtrlCreateButton("Start", 70, 275, 60)
GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $okbutton
            ; MsgBox(0, "GUI Event", "The Installs will now begin")

            For $Loop = 0 To UBound($arr) - 1 ; Loop through the checkboxes
                If GUICtrlRead($ControlIDs[$Loop]) = $GUI_CHECKED Then ; Find if it's checked
                    RunWait ('C:\Temp\' & $arr[$Loop] & '.exe', '', @SW_SHOWNORMAL ) ; run the program stored in the same position in the $arr array
                EndIf
            Next
            Exit
        Case $msg = $GUI_EVENT_CLOSE
            MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
            ExitLoop
    EndSelect
 WEnd


 Func _LineNumsOfSearchStr($sFileName, $searchString, $bDeleteLine = False)
    Local $location, $aCurrentLineNum, $iCurrentLineNum, $sFile, $iOccur = 1, $sRes = ""
    If FileExists($sFileName) = 0 Then Return 1
    Do
        $sFile = FileRead($sFileName)
        $location = StringInStr($sFile, $searchString, 0, $iOccur) ; Find the $iOccur occurrence of the "substring"

        If $location > 0 Then
            $aCurrentLineNum = StringRegExp(StringRegExpReplace($sFile, "(?s)(.{" & $location & "})(.*)$", "\1"), "(?s)(\v+)", 3) ;Find line number
            $iCurrentLineNum = UBound($aCurrentLineNum) + 1 ; Line number

            ;ConsoleWrite("CharPos: " &  $location & "   Ln: " & $iCurrentLineNum & @CRLF)
            $sRes &= $iCurrentLineNum & "|"
            If $bDeleteLine Then
                _FileWriteToLine($sFileName, $iCurrentLineNum, "", 1) ; Remove found line from file.
            Else
                $iOccur += 1
            EndIf
        Else
            ExitLoop
        EndIf
        Sleep(10)
    Until 0
    ;ShellExecute($sFileName)
    Return StringSplit(StringTrimRight($sRes, 1), "|")
EndFunc   ;==>_LineNumsOfSearchStr
;

The code produces the array properly, and displays the names of the printer install files that are to run, but in the end, they all end up installing the same package. I am sure I have to do something different with my loop, but I can't figure it out and know that you Autoit wizards could be of help.

Share this post


Link to post
Share on other sites

NEVERMIND! I am slow. I figured it out, wow. Thank you again.

Share this post


Link to post
Share on other sites

This scenario is almost exactly the same as mine! I couldn't figure a way to dynamically assign checkboxes based upon a previously unknown group of items! It was the final piece of the puzzle. I just wanted to post my thanks :D:D:D. Thank you!

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
Sign in to follow this  
Followers 0