way1000

array variable error

4 posts in this topic

i have a script that works but gives results and errors randomly

dzuwrp.png

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <File.au3>
#include <Date.au3>

Global $result1s[5]=["a", "b", "c", "d", "e"]
Global $result2s[19]=["text1", "text2", "text3", "text4", "text5", "text6", "text7", "text8", "text9", "text10", "text11", "text12", "text13", "text14", "text15", "text16", "text17", "text18", "text19"]
Global $result3s[19]=["text1", "text2", "text3", "text4", "text5", "text6", "text7", "text8", "text9", "text10", "text11", "text12", "text13", "text14", "text15", "text16", "text17", "text18", "text19"]
Global $result4s[19]=["text1", "text2", "text3", "text4", "text5", "text6", "text7", "text8", "text9", "text10", "text11", "text12", "text13", "text14", "text15", "text16", "text17", "text18", "text19"]
Global $result5s[19]=["text1", "text2", "text3", "text4", "text5", "text6", "text7", "text8", "text9", "text10", "text11", "text12", "text13", "text14", "text15", "text16", "text17", "text18", "text19"]
_Main()

Func _Main()

    Local $button1
    Local $output, $die, $msg, $results1, $results2, $results3, $results4, $results5
    Local $file = FileOpen("test.txt", 1)

    GUICreate("test", 600, 200, -1, -1)
    $button1 = GUICtrlCreateButton("Result", 460, 110, 50, 30)
    $output1 = GUICtrlCreateInput("", 60, 10, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("1", 5, 12)
    $output2 = GUICtrlCreateInput("", 60, 30, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("2", 5, 12)
    $output3 = GUICtrlCreateInput("", 60, 50, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("3", 5, 32)
    $output4 = GUICtrlCreateInput("", 60, 70, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("4", 5, 52)
    $output5 = GUICtrlCreateInput("", 60, 90, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("5", 5, 72)

    GUICtrlCreatePic("", 0, 0, 600, 200)
    GUICtrlCreateLabel("Sample Pic", 75, 1, 53, 15)
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    GUICtrlSetColor(-1, 0xFFFFFF)

    $die = GUICtrlCreateLabel("", 700, 500, 700, 20, $SS_SUNKEN)
    GUICtrlSetFont($output, 8, 800, "", "Verdana")
    GUISetState()



    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $button1
                $results1 = Random(1, 5, 1)
                GUICtrlSetData($output1, $result1s[$results1])
                $read1 = GUICtrlRead($output1)

                $results2 = Random(1, 19, 1)
                GUICtrlSetData($output2, $result2s[$results2])
                $read2 = GUICtrlRead($output2)

                $results3 = Random(1, 19, 1)
                GUICtrlSetData($output3, $result3s[$results3])
                $read3 = GUIctrlRead($output3)

               $results4 = Random(1, 19, 1)
                GUICtrlSetData($output4, $result4s[$results4])
                $read4 = GUICtrlRead($output4)

                $results5 = Random(1, 19, 1)
                GUICtrlSetData($output5, $result5s[$results5])
                $read5 = GUICtrlRead($output5)

            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read1)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read2)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read3)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read4)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read5)

            FileClose($file)
        EndSelect
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
EndFunc   ;==>_Main

 

 

how to fix it?

Share this post


Link to post
Share on other sites



 

AuitoIt  arrays are zero based so for example your $result1s array has valid index values $result1s[0] to $result1s[4]

A quick simple way to fix your code is to subtract 1 from the value returned by the random function as shown below or alternatively change $results1 = Random(1, 5, 1) to $results1 = Random(0, 4, 1)

 

; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $button1
                $results1 = Random(1, 5, 1)
                GUICtrlSetData($output1, $result1s[$results1 - 1])
                $read1 = GUICtrlRead($output1)

                $results2 = Random(1, 19, 1)
                GUICtrlSetData($output2, $result2s[$results2 - 1])
                $read2 = GUICtrlRead($output2)

                $results3 = Random(1, 19, 1)
                GUICtrlSetData($output3, $result3s[$results3 - 1])
                $read3 = GUIctrlRead($output3)

               $results4 = Random(1, 19, 1)
                GUICtrlSetData($output4, $result4s[$results4 - 1])
                $read4 = GUICtrlRead($output4)

                $results5 = Random(1, 19, 1)
                GUICtrlSetData($output5, $result5s[$results5 - 1])
                $read5 = GUICtrlRead($output5)

            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read1)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read2)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read3)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read4)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read5)

            FileClose($file)
        EndSelect
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd

 


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

This returns a value between 1 and 5:

$results1 = Random(1, 5, 1)

and this creates an Array of 5 elements 0 to 4: 

Global $result1s[5]=["a", "b", "c", "d", "e"]

so whenever the first line's result = 5 the error will occur.

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Please check the Arrays section in the wiki for a very detailed description.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-02-03 - Version 1.4.7.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-03 - Version 1.2.4.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
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

  • Similar Content

    • dexto
      By dexto
      Why idea why would I get an error (Autoit Beta 3.3.15.0):
      Local $json[] Local $map[] $map['test'] = 'hello' Local $array[5] = [1, 2, 3, 4, 5] $json["data"] = $map $json["data"]["arr"] = $array ConsoleWrite($json["data"]["arr"][0] & @CRLF)  
    • cookiemonster
      By cookiemonster
      Im trying to read a JSON file into an array.
       
      The JSON looks like this:
       
      { "project_info": { "project_number": "123456789", "url": "https://www.website.com", "project_id": "PRJ08", "Bucket": "Buk09" }, "client": [ { "client_info": { "id": "1:1001306455", "info": { "name": "banana" } }, "oauth_client": [ { "client_id": "1001306455694-m3h6v", "client_type": 3 } ], "a_key": [ { "key": "dkldkdkd" } ], "Avail": { "ana": { "status": 1 }, "vit": { "status": 1, "other": [] }, "ad": { "status": 2 } } }, { "client_info": { "id": "1:1838346", "info": { "name": "orange" } }, "oauth_client": [ { "client_id": "2145696315633-dmdhe", "client_type": 3 } ], "a_key": [ { "key": "osikdme" } ], "Avail": { "ana": { "status": 1 }, "vit": { "status": 1, "other": [] }, "ad": { "status": 2 } } }, ], "configuration_version": "1" } What I want to do is read it all into one single large array, can anyone help? 
      All I have so far is:
      #include "JSON.au3" #include "array.au3" $file = fileread("C:\file.json") JsonToArray($file) Func JsonToArray($JSON) $JSON = StringRegExpReplace($JSON, "[\[\]{}]", "") $sBreak = StringSplit($JSON, ",") For $a = 1 To $sBreak[0] $t = _JSONDecode("{" & $sBreak[$a] & "}") _ArrayDisplay($t, "multi " & $a & " of " & $sBreak[0]) Next EndFunc ;==>JsonToArray  
    • SkysLastChance
      By SkysLastChance
      I was wondering if it is possible to make this code skip to the next row if it reads a blank line?
      I tried this.
      If $aArray = "" Then $r += 1 EndIf #include <Excel.au3> Local $r = 1 Local $aArray = _Excel_RangeRead($oWorkbook) ;~ If $iBox is greater than no. of rows in $aArray then $iBox equals the number of rows in $aArray If $iBox > (UBound($aArray) - 1) Then $iBox = UBound($aArray) - 1 For $i = 1 To UBound($aArray) - 1 ;$i =0 Start from row A If $aArray = "" Then $r += 1 EndIf $sR0 = $aArray[$i][0] ;status $sR1 = $aArray[$i][1] ;first name $sR2 = $aArray[$i][2] ;Last name $sR3 = $aArray[$i][3] ;Last 4 SSN $sR4 = $aArray[$i][4] ;DOB $sR5 = $aArray[$i][5] ;Email Address $sR6 = $aArray[$i][6] ;Mailling Address $sR7 = $aArray[$i][7] ;City $sR8 = $aArray[$i][8] ;state $sR9 = $aArray[$i][9] ;Zip Code $sR10 = $aArray[$i][10] ;Gender $sR11 = $aArray[$i][11] ;Phone WinSetState ("ADM.MCK - Registration Management Desktop","",@SW_MAXIMIZE) WinWaitActive("[CLASS:Notepad]", "", 10) ControlSend("[CLASS:Notepad]", "", "", ("{F8}")) Sleep (500) ControlSend("[CLASS:Notepad]", "", "", ("{F8}")) Sleep (500) ControlSend("[CLASS:Notepad]", "", "", ("N")) ControlSend("[CLASS:Notepad]", "", "", ("{TAB}")) Sleep (3000) ControlSend("[CLASS:Notepad]", "", "Edit1", $sR1 & @CR) Sleep (200) ControlSend("[CLASS:Notepad]", "", "", ("{TAB}")) Sleep (200) ControlSend("[CLASS:Notepad]", "", "Edit1", $sR2 & @CR) Sleep (200) ControlSend("[CLASS:Notepad]", "", "", ("{TAB}")) Sleep (200) ControlSend("[CLASS:Notepad]", "", "Edit1", $sR6& @CR) Sleep (200) ControlSend("[CLASS:Notepad]", "", "", ("{TAB}")) Sleep (200) ControlSend("[CLASS:Notepad]", "", "Edit1", $sR7 & @CR) Sleep (200) ControlSend("[CLASS:Notepad]", "", "Edit1", $sR8 & @CR) Sleep (200) ControlSend("[CLASS:Notepad]", "", "Edit1", $sR9 & @CR) Sleep (200) ControlSend("[CLASS:Notepad]", "", "Edit1", $sR11& @CR) Sleep (200) ControlSend("[CLASS:Notepad]", "", "Edit1", $sR5 & @CR) ;Email Sleep (200) ControlSend("[CLASS:Notepad]", "", "Edit1", $sR5 & @CR) ;Email Sleep (200) ControlSend("[CLASS:Notepad]", "", "", (FormatDate($sR4) & @CR)) $r += 1  
       
    • nss
      By nss
      Hi all.
       
      Because of me wondering if I could access the key/value pair arrays with the numbers as indexes, I have found out that the zeroth element for some reason doesn't return anything.
      Here's the example:
       
      local $r[2] $r["test1"]="hello" $r["test2"]="how are you" msgbox(64, $r[0], $r[1]) ; prints the ["test2"] but not ["test1"]. Is this even supposed to be a thing?  
       
      BTW, I haven't seen Autoit get updated since 2015; is it abandened or something?
      Any help/clarification appreciated.
    • Starf0x
      By Starf0x
      Hi,
       
      I'm a bit stuck (my brains might be hanging in a loop).
      Here's my dillema, what if there is no command line parameters are given?
       
      Global $aCmdLine = _WinAPI_CommandLineToArgv($CmdLineRaw) I've tried this, and more:
      If UBound($aCmdLine) - 1 < 1 Then ConsoleWriteError(@CRLF & "For more information use: " & @ScriptName & " /?" & @CRLF) Exit EndIf This works, the program doesn't continue, but doesn't show the ConsoleWrite, I've tried sleeps etc...
      Cheers, Starf0x