Jump to content
Sign in to follow this  
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Jangal
      Hello friends
      This app is slow
      How to increase its speed?
       
      #include <Array.au3> #include <StringConstants.au3> #include <File.au3> #include <String.au3> Global $aWord[][2]  = [[1, "google"],[2,"hello"]]


        Global $sFileName = @ScriptDir & "\1.txt" ; 2MB Text File Local $sFileRead = FileRead($sFileName) Local $res = StringRegExp($sFileRead, "(*UCP)\b[\pL\d]{2,}", 3) _ArrayDisplay($res)   for $sWord in $res     $iIndex = _ArraySearch($aWord, $sWord, 0, 0, 0, 0, 1, 2)     ;MsgBox(0,0,$iIndex)     if $iIndex == -1 Then         Local $aFill = [[0,$sWord]]         _ArrayAdd($aWord,$aFill)        ;      Else         $aWord[$iIndex][0] +=1     EndIf   Next _ArrayDisplay($aWord)

      1.txt
    • By Colduction
      Hi dear friends!, i'm sorry for creating a new thread (a new problem), i have over than 9 lists that i want to combine them to be this (in this example, there are 3 test files):


      I've written a little code for splitting main information, but i really confused how to make results as "Output.txt", here is that code:
       
      $sRegex_1 = StringRegExp(FileRead("1.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) $sRegex_2 = StringRegExp(FileRead("2.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) $sRegex_3 = StringRegExp(FileRead("3.txt"), '(?s:(?<=\=\=\r\n)(.*?)(?=\r\n\=\=))', 3) For $i = 0 To UBound($sRegex_1) - 1 ConsoleWrite($sRegex_1[$i] & @CRLF) For $j = 0 To UBound($sRegex_2) - 1 ConsoleWrite($sRegex_2[$j] & @CRLF) For $k = 0 To UBound($sRegex_3) - 1 ConsoleWrite($sRegex_3[$k] & @CRLF) Next Next Next  
    • By nacerbaaziz
      hello evrybody
      here is an example about how to split your texts using a delimiter with the ability to select how much of delimiters shows in each colum  with $i_number
      e.g
      you have a long text and you want to split it in an array
      that evry colum have a number (n) of lines
      i made a function that do that for you
      just call it with a three params
      $s_text
      your text
      $i_number
      the number that you want to put in each col
      $s_siparator
      the siparator
      default is "|"
      here is the function with example
      i hope that it will be useful for you
       
      ****
       
      #include <Array.au3> $s_txt = "some text1some text2|some text3|some text4|some text5|some text6" $array = splitText($s_txt, 2) _ArrayDisplay($array) Func splitText($s_text, $i_number, $s_siparator = "|") Local $a_TXT = StringSplit($s_text, $s_siparator) Local $a_Return[$a_TXT[0] + 1] If ($a_TXT[0] <= $i_number) Or ($i_number <= 0) Then ReDim $a_Return[2] $a_Return[0] = 1 $a_Return[1] = $s_text Return $a_Return EndIf Local $i_Processed = 1, $i_arrayProcessed = 1 Do For $i = $i_Processed To ($i_Processed + $i_number) - 1 If ($a_TXT[0] < $i) Then ExitLoop If Not ($a_Return[$i_arrayProcessed]) Then $a_Return[$i_arrayProcessed] = $a_TXT[$i] Else $a_Return[$i_arrayProcessed] &= $s_siparator & $a_TXT[$i] EndIf $i_Processed += 1 Next $i_arrayProcessed += 1 Until ($a_TXT[0] < $i_Processed) ReDim $a_Return[$i_arrayProcessed] $a_Return[0] = $i_arrayProcessed - 1 Return $a_Return EndFunc ;==>splitText
      accept my greetings
      thanks to
      @Dan_555
      for his notes
       
    • By MesterPerfect
      good morning
      this is the first post here in the autoit forums
      i hope that you can help me in my problem
      i have a JSON encoded
      it a map of my forums
      where i want to make a treeview that have the same type of map
      e.g
      a system (as category)
      windows (as sub category)
      software (as an child item in the windows category)
      .....
      i don't know how to do that
      so, i know that i can do that using the json functions
      but i need your help about how we can do it as the type that i told you
      by the way i need to put the sub info for each item in an array that give me the ability to manage my items
      e.g
      can post thread
      can reply
      message cound ...
      you just give me a small example and i can continue.
      am sorry if this against the rules of the forum.
      but i realy searched a lot but i couldn't
      i hope some one give me the way.
      thank you very much in advance
       
      here is the link of json forum
      https://www.autoitscript.com/forum/topic/148114-a-non-strict-json-udf-jsmn/
      and here is my encoded json file
       
      { "tree_map": { "0": [ 1, 5, 6, 7 ], "1": [ 2 ], "2": [ 4 ], "5": [ 3 ], "6": [ 8 ], "8": [ 9, 10 ] }, "nodes": [ { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 1, "node_id": 1, "node_name": null, "node_type_id": "Category", "parent_node_id": 0, "title": "Main category", "type_data": {} }, { "breadcrumbs": [ { "node_id": 1, "title": "Main category", "node_type_id": "Category" } ], "description": "", "display_in_list": true, "display_order": 1, "node_id": 2, "node_name": null, "node_type_id": "Forum", "parent_node_id": 1, "title": "Main forum", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 1, "title": "Main category", "node_type_id": "Category" }, { "node_id": 2, "title": "Main forum", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 1, "node_id": 4, "node_name": null, "node_type_id": "Forum", "parent_node_id": 2, "title": "my forums1", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 2, "node_id": 5, "node_name": null, "node_type_id": "Category", "parent_node_id": 0, "title": "Perfect", "type_data": {} }, { "breadcrumbs": [ { "node_id": 5, "title": "Perfect", "node_type_id": "Category" } ], "description": "", "display_in_list": true, "display_order": 2, "node_id": 3, "node_name": null, "node_type_id": "Forum", "parent_node_id": 5, "title": "ahmed", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 3, "node_id": 6, "node_name": null, "node_type_id": "Forum", "parent_node_id": 0, "title": "autoit", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 6, "title": "autoit", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 3, "node_id": 8, "node_name": null, "node_type_id": "Forum", "parent_node_id": 6, "title": "examples", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 6, "title": "autoit", "node_type_id": "Forum" }, { "node_id": 8, "title": "examples", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 3, "node_id": 9, "node_name": null, "node_type_id": "Forum", "parent_node_id": 8, "title": "GUI", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [ { "node_id": 6, "title": "autoit", "node_type_id": "Forum" }, { "node_id": 8, "title": "examples", "node_type_id": "Forum" } ], "description": "", "display_in_list": true, "display_order": 31, "node_id": 10, "node_name": null, "node_type_id": "Forum", "parent_node_id": 8, "title": "windowEX", "type_data": { "allow_poll": true, "allow_posting": true, "can_create_thread": true, "can_upload_attachment": true, "discussion_count": 0, "last_post_date": 0, "last_post_id": 0, "last_post_username": "", "last_thread_id": 0, "last_thread_prefix_id": 0, "last_thread_title": "", "message_count": 0, "min_tags": 0, "require_prefix": false } }, { "breadcrumbs": [], "description": "", "display_in_list": true, "display_order": 4, "node_id": 7, "node_name": null, "node_type_id": "Category", "parent_node_id": 0, "title": "vbs", "type_data": {} } ] }  
    • By nooneclose
      I need to dynamically resize my 2d array while looping. 
      I know this code:
      ReDim $rArray[UBound($rArray) + 1] works for the rows, however, I also need to increase the columns. How would i go about increasing both rows and columns while looping? 
×
×
  • Create New...