Jump to content
lattey

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

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

 

Share this post


Link to post
Share on other sites

expected result:

 

for example,

$array1 contains element 2 and 3. 

i need to loop $array1 and set varaiable, $var = "2,3"

Share this post


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

 

Share this post


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

Share this post


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

 

Share this post


Link to post
Share on other sites

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?

Share this post


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


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

  • Similar Content

    • By sksbir
      Hi
      Trying this from autoit v3.3.14.5 and SCITE 3.19.102.1901.0 :
      help file , page "Language Reference - Variables" , sample autoit script about maps:

      Maps must be declared before use by defining their scope using the 'Global/Local/Static' keywords. Local $mControls[]
      So is my test script : only with this local declation.
      -check syntax is OK
      - running script : 

      test.au3" (13) : ==> Variable subscript badly formatted.: Local $mControls[] Local $mControls[^ ERROR ->14:51:49 AutoIt3.exe ended.rc:1
      any clue ?
    • By Blitzkid
      Hello, i want to search several directories for files with the largest numbers behind them (Like "video123") . They dont have a datatype. But there are also files with longer names and datatypes in these folders (Like "video778.mp4"). Is it possible to filter the _FileListToArray Syntax from
      to smth. like
       
      Here is my Code
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <array.au3> #include <File.au3> $filedir = @ScriptDir & "\" _checkfile() Func _checkfile() ConsoleWrite("______________________" & @CRLF) Local $arr[3] = ["music", "picture", "video"] For $i = 0 To UBound($arr) - 1 Local $arrayfiles = _FileListToArray($filedir & $arr[$i], $arr[$i] & "*", 1) If @error = 1 Then ConsoleWrite($arr[$i] & "Error 1") EndIf If @error = 4 Then ConsoleWrite($arr[$i] & "Error 2") ;Exit EndIf $arrayfilter = _ArrayMax($arrayfiles, 0, 1) Global $stringfiles = StringReplace($arrayfilter, $arr[$i], "") ConsoleWrite($arrayfilter & @CRLF) Next EndFunc ;==>_checkfile  
    • By jmp
      Script running good but error in line 7.
      When i run this script :
      #include <IE.au3> #include <Array.au3> $oIE = _IEAttach ("Shop") $oTable = _IETableGetCollection ($oIE, 1) $aTableData = _IETableWriteToArray ($oTable) For $inumber = 1 To UBound($aTableData) -1 $table = $aTableData[4][$inumber] MsgBox(0, "", $table) Next I got Error: array variable has incorrect number of subscripts or subscript dimension range exceeded
    • By nacerbaaziz
      Good morning guys, i hope that you're all well.
      guys, i have a problem and i hope that you can help me
      i've created an 3d array
      the array Contain a Categories info
      as folow
      $array[n][0][0] = Categorie name
      $array[n][0][1] = Categorie file path
      $array[n][0][2] = Categorie contents number
      $array[n][m][0] = link name
      $array[n][m][1] = link url
      $array[n][m][2] = link section name
      in my tool i want to add an option to delete a Category
      as you know the Categorie mean that must delete a region from the array
      when i tried to use _arrayDelete
      with the 2d array it work well
      but here i couldn't find any way to do that, can any one help me please?
      thanks in advance.
    • By nacerbaaziz
      hello sirs, please help me
      i tried to create a function that read a folder files to 3d array
      e.g
      $array[n][0][0] = ctName
      $array[n][0][1] = ctFilePath
      $array[n][0][2] = crtsections number
      $array[n][m][0] = KeyName
      $array[n][m][1] = KeyVal
      $array[n][m][2] = keySectionName
       
      that the array
      when i put one file into the folder all things work fine
      but when i put more than one file
      the last file worked fine but the others only the first key is showing
      please can you help me to correct this problem
      here is the example with the folder
      please accept my greetings
      and thanks in advance
       
      array3d.zip
×
×
  • Create New...