computergroove

Comparing the data in a 2d array with a for loop error

10 posts in this topic

I have a GUI and I am trying to get user input and compare the input to the first column in a 2d array. 

#include <GUIConstants.au3>
#include <File.au3>
#include <Array.au3>

Global $aRead[][] = [[0],[0]]
Global $Path = @ScriptDir & "\data.ini"
_FileReadToArray($Path,$aRead,0,";");Reads the contents of data.ini to $aRead.

HotKeySet("{ENTER}", "ProcessData");Enter a number and hit enter

$Form1 = GUICreate("Form1",800,600,100,100);GUI Main window
$edit_field = GUICtrlCreateInput("", 30, 25, 150, 15)
        GUISetState(@SW_SHOW)
        GUISetBkColor(0x000000);sets background color on GUI window
GUISetState(@SW_SHOW)

Func ProcessData()
    $Input = GUICtrlRead($edit_field)
    If $Input <> "" Then
        For $i = 1 To UBound($aRead - 1)
            If $Input == $aRead[$a] Then
                $Data = $aRead[$a]
                _ArrayDisplay($Data)
                                ExitLoop
                EndIf
        Next
        GUICtrlSetData($edit_field,"");Clear the input box
    EndIf
EndFunc

data.ini looks like:

1;1
2;2
3;3
4;4

I am trying to use a for loop to compare the user input value to each item in the forst column of my 2d array.

  1. If a match is found in $aRead[5] then I want to assign a variable to the data in $aRead[5][2] and exit the for loop and make the input box blank and wait for another user input.
  2. If a match isnt found then blank out the user input box.

The gui crashes when I enter 1 and hit return and I get an error. What am I doing wrong?


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites



Array $aRead is 0-based because of parameter 3 you pass to _FileReadToArray so the statement

For $i = 1 To UBound($aRead - 1)

should be

For $i = 0 To UBound($aRead, 1) - 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.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
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

That still fails. I can enter a value in the input box and when I hit enter the script stops.

Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

You need to post the error message you get in ScITE.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.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
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Func ProcessData();Listens to the input box
    $Input = GUICtrlRead($edit_field)
    If $Input <> "" Then
        For $i = 0 To UBound($aRead,1) - 1
            If $Input == $aRead[$i] Then;<== This is line 38
                MsgBox(0,0,$Input)
                ExitLoop
        EndIf
        Next
        GUICtrlSetData($edit_field,"");Clear the input box
    EndIf
EndFunc

Console output:

>"C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "C:\Users\user\Desktop\BCVLC.au3" /UserParams    
+>17:00:35 Starting AutoIt3Wrapper v.14.801.2025.0 SciTE v.3.4.4.0   Keyboard:00000409  OS:WIN_7/Service Pack 1  CPU:X64 OS:X64    Environment(Language:0409)
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\user\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\user\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.12.0)  from:C:\Program Files (x86)\AutoIt3  input:C:\Users\user\Desktop\BCVLC.au3
+>17:00:35 AU3Check ended.rc:0
>Running:(3.3.12.0):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\Users\user\Desktop\BCVLC.au3"    
--> Press Ctrl+Alt+Break to Restart or Ctrl+Break to Stop
"C:\Users\user\Desktop\BCVLC.au3" (38) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
If $Input == $aRead[$i] Then
If $Input == ^ ERROR
->17:00:36 AutoIt3.exe ended.rc:1
+>17:00:36 AutoIt3Wrapper Finished.
>Exit code: 1    Time: 1.215
Edited by computergroove

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Func ProcessData();Listens to the input box
    $Input = GUICtrlRead($edit_field)
    If $Input <> "" Then
        For $i = 0 To UBound($aRead) - 1
            If $Input == $aRead[$i][0] Then;<== This is line 38
                MsgBox(0,0,$Input)
                ExitLoop
        EndIf
        Next
        GUICtrlSetData($edit_field,"");Clear the input box
    EndIf
EndFunc

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

I got it. I needed to add the column number in the array:

Func ProcessData();Listens to the input box
    $Input = GUICtrlRead($edit_field)
    If $Input <> "" Then
        For $i = 0 To UBound($aRead,1) - 1
            $Temp = $aRead[$i][1]
            MsgBox(0,0,$Temp)
        Next
        GUICtrlSetData($edit_field,"");Clear the input box
    EndIf
EndFunc

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

don't use == this is autoit, it's just =

Why did you suddenly change to $a instead of using $i?

#include <GUIConstants.au3>
#include <File.au3>
#include <Array.au3>

Global $aRead[][] = [[0],[0]]
Global $Path = @ScriptDir & "\data.ini"
_FileReadToArray($Path,$aRead,0,";");Reads the contents of data.ini to $aRead.
HotKeySet("{ENTER}", "ProcessData");Enter a number and hit enter

$Form1 = GUICreate("Form1",800,600,100,100);GUI Main window
$edit_field = GUICtrlCreateInput("", 30, 25, 150, 15)
        GUISetState(@SW_SHOW)
        GUISetBkColor(0x000000);sets background color on GUI window
GUISetState(@SW_SHOW)

while 1
        Sleep(10)
WEnd

Func ProcessData()
    $Input = GUICtrlRead($edit_field)
    If $Input <> "" Then
        For $i = 0 To UBound($aRead - 1)
            If $Input = $aRead[$i] Then
                $Data = $aRead[$i]
                _ArrayDisplay($Data)
                                ExitLoop
                EndIf
        Next
        GUICtrlSetData($edit_field,"");Clear the input box
    EndIf
EndFunc

It's still giving me

(27) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
If $Input = $aRead[$i] Then
If $Input = ^ ERROR


If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Share this post


Link to post
Share on other sites

I didn't notice that it was 2 dimensional, that would cause it.

If you need anything else, post away.


If you need help with your stuff, feel free to get me on my Skype.

I often get bored and enjoy helping with projects.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

don't use == this is autoit, it's just =

 

Please see the Help file for Operators...

== Tests if two strings are equal. Case 
sensitive. The left and right values are converted to strings if they 
are not strings already. This operator should only be used if string comparisons 
need to be case sensitive.
Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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