Jump to content

array variable error


Recommended Posts

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?

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

Link to comment
Share on other sites

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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...