Jump to content

autoit: Loop an array and put each element into one variable


lattey
 Share

Recommended Posts

hi,

i have checkboxes and each checkbox that checked, i put in array. 

now, im stuck on how to loop the checked array and store in in one variable. what i can do now, is only write the result into a text file. 

below is the code:

#include <GUIConstantsEx.au3>
;~ #include <MsgBoxConstants.au3>
#include <ButtonConstants.au3>
#include <Array.au3>

Global $Count = 3
Global $CheckBoxP[$Count]
Global $step[$Count]

global $array1[1]
Global $ExitResult

$hGUI = GUICreate("Summary Steps", 500, 400)

GUISetFont(12, 400, "Tahoma")
GUICtrlCreateLabel( "Please Select the Summary Steps for Script Check", 70, 20)

GUISetFont(10, 400, "Tahoma")
Global $array_Pstep[3] = ["fix2","fix1","fix3"]
global $step[3] = ["2","3","4"]

$Spacing = 50
For $i = 0 To  UBound($array_Pstep) - 1
    $CheckBoxP[$i] = GUICtrlCreateCheckbox($array_Pstep[$i], 80, $Spacing + (20 * $i), 65, 17)
Next


$submit = GUICtrlCreateButton("Submit",180, 280, 80, 30)
$exit = GUICtrlCreateButton("Exit",180, 320, 80, 30)


GUISetState()

While 1
    $Msg = GUIGetMsg()
    Select

        case $Msg=$submit

            For $i = 0 To $Count - 1

            If GUICtrlRead($CheckBoxP[$i]) = $GUI_CHECKED Then

            _ArrayAdd($array1, $step[$i])

            EndIf

            Next




            Global $logfilerray = @WorkingDir & "\checkedlist.txt"
            FileDelete ($logfilerray)
            Global $readlogfile = FileOpen($logfilerray,1)


            for $a = 1 to UBound($array1) - 1

;~          $var=$array1[$a]
            FileWriteLine($readlogfile,$array1[$a])
            Next

            FileClose($readlogfile)
            Exit

        case $Msg=$exit
             $ExitResult = MsgBox(1,"Summary Step", "Continue to Exit ?")
                  if $ExitResult = 1 Then ;ok
                    Exit
                  EndIf
            Exit

    EndSelect

WEnd

 

Link to comment
Share on other sites

Do you mean something like:

#include <GUIConstantsEx.au3>
;~ #include <MsgBoxConstants.au3>
#include <ButtonConstants.au3>
#include <Array.au3>

Global $Count = 3
Global $CheckBoxP[$Count]
Global $step[$Count]
Global $ExitResult

$hGUI = GUICreate("Summary Steps", 500, 400)

GUISetFont(12, 400, "Tahoma")
GUICtrlCreateLabel( "Please Select the Summary Steps for Script Check", 70, 20)

GUISetFont(10, 400, "Tahoma")
Global $array_Pstep[3] = ["fix2","fix1","fix3"]
global $step[3] = ["2","3","4"]

$Spacing = 50
For $i = 0 To  UBound($array_Pstep) - 1
    $CheckBoxP[$i] = GUICtrlCreateCheckbox($array_Pstep[$i], 80, $Spacing + (20 * $i), 65, 17)
Next

$submit = GUICtrlCreateButton("Submit",180, 280, 80, 30)
$exit = GUICtrlCreateButton("Exit",180, 320, 80, 30)


GUISetState()

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $submit
            $sResults = ""
            For $i = 0 To UBound($CheckBoxP) - 1
                If GUICtrlRead($CheckBoxP[$i]) = $GUI_CHECKED Then
                    $sResults &= $sResults  = "" ? $step[$i] & "" : "," & $step[$i]
                EndIf
            Next
            MsgBox(4096, "Results", $sResults)
        Case $exit
            $ExitResult = MsgBox(1,"Summary Step", "Continue to Exit ?")
            if $ExitResult = 1 Then ;ok
                Exit
            EndIf
    EndSwitch
WEnd

 

Link to comment
Share on other sites

You could also do everything with a single 2d Array for example:

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

Global $aSteps[4][3] = [[3, "", ""], ["", "fix2", 2],["", "fix1", 3], ["", "fix3", 4]]
Global $aResults[1], $sResults
Global $idExitResult, $y = 50

Global $hGUI = GUICreate("Summary Steps", 500, 400)
    GUISetFont(12, 400, "Tahoma")
    GUICtrlCreateLabel( "Please Select the Summary Steps for Script Check", 70, 20)
    For $i = 1 To  $aSteps[0][0]
        $aSteps[$i][0] = GUICtrlCreateCheckbox($aSteps[$i][1], 80, $y + (20 * $i), 65, 17)
        GUICtrlSetFont(-1, 10, 400, "Tahoma")
    Next
    $idSubmit = GUICtrlCreateButton("Submit",180, 280, 80, 30)
    $idExit = GUICtrlCreateButton("Exit",180, 320, 80, 30)

GUISetState()

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $idSubmit
            ReDim $aResults[1]
            $sResults = ""
            For $i = 1 To $aSteps[0][0]
                If GUICtrlRead($aSteps[$i][0]) = $GUI_CHECKED Then
                    _ArrayAdd($aResults, $aSteps[$i][2])
                    $sResults &= $sResults  = "" ? $aSteps[$i][2] & "" : "," & $aSteps[$i][2]
                EndIf
            Next
            MsgBox(4096, "Results", $sResults)
            ;~ $aResults[0] the number of results found
            $aResults[0] = UBound($aResults) - 1
            _ArrayDisplay($aResults)
        Case $idExit
            $idExitResult = MsgBox(1,"Summary Step", "Continue to Exit ?")
                If $idExitResult = 1 Then Exit
    EndSwitch
WEnd

 

Edited by Subz
Link to comment
Share on other sites

Simplest way would be to use a for loop and append each element to the first.

Local $var = $array[0]

For $i = 1 To UBound($array)
    $var = $var + "," & $array[$i]
Next

If yours is a 2d array, you can do the same and use the following function: https://www.autoitscript.com/autoit3/docs/libfunctions/_ArrayToString.htm

Local $var = $array[0]

For $i = 1 To UBound($array)
    $var = _ArrayToString($array[$i], ",", 1, UBound($array[$i])
Next

 

Link to comment
Share on other sites

  • 5 months later...

The below code only works when I run using windows XP and win7, AutoIt version 3.14.

 If GUICtrlRead($CheckBoxP[$i]) = $GUI_CHECKED Then
                    $sResults &= $sResults  = "" ? $step[$i] & "" : "," & $step[$i]
 EndIf

When I run it t windows NT, AutoIt ver 3.2.12., show error unable to parse line.

Is it because of using the old version? If yes, is there any similar code that does the same thing?

Link to comment
Share on other sites

This line 

$sResults &= $sResults  = "" ? $step[$i] & "" : "," & $step[$i]

uses something called ternary operation, which wasn't present in version 3.2.x, and was only introduced in 3.3.10.0 and later.

You'll need to use a (traditional) conditional statement instead.  For example (untested)

If GUICtrlRead($CheckBoxP[$i]) = $GUI_CHECKED Then
    $sResults &= $sResults  = "" ? $step[$i] & "" : "," & $step[$i]
    If $sResults = "" Then 
        $sResults &= $step[$i]
    Else
        $sResults &= "," & $step[$i]
    EndIf
EndIf

I myself would handle the comma separation a little differently, but I just wanted to show a more-or-less equivalent translation.

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

×
×
  • Create New...