Info

Pass a two dimensional array to a function

6 posts in this topic

#1 ·  Posted

Global $array[3][10]

f($array[0])

Func f($a)
    MsgBox(0,"",UBound($a))
EndFunc

I want to access the first column of $array inside f(). The desired MsgBox message is 10, as there are 10 rows in the first column. (Or is it the opposite? I'm not sure :P)

There script, however, returns an error:

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
f($array[0])
f(^ ERROR

 

Share this post


Link to post
Share on other sites



#2 ·  Posted

As far as I know you'll either have to pass the full array:

#include <AutoItConstants.au3>

Global $array[3][10]

f($array)

Func f($a)
    MsgBox(0,"",UBound($a, $UBOUND_COLUMNS))
EndFunc

Or figure out what specific value array value ($array[2][1] for example) you want to pass before passing it to the function


My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

MY PROJECTS


Active: IRC UDF, WindowEx UDF
Discontinued: GithubBubbleSort UDF

Share this post


Link to post
Share on other sites

#3 ·  Posted

That, and if you need to pass the full array, declare the function parameter as ByRef to pass a reference and not a copy.

Func f(ByRef $a)
... ; the rest is unchanged


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#4 ·  Posted

That I know. Thanks!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

So you want the message box to display how many columns your array has?
If so then this should do the trick, unless I misunderstood you.

 

Global $array[3][10]

f($array)

Func f(ByRef $a)
    MsgBox(0, "", UBound($a, 2))
EndFunc

 

Edit: Whoops, just noticed that rcmaehl's code does the exact same thing :>

Edited by Floops

Share this post


Link to post
Share on other sites

#6 ·  Posted

On 5/9/2017 at 8:26 AM, Info said:
Global $array[3][10]

f($array[0])

Func f($a)
    MsgBox(0,"",UBound($a))
EndFunc

I want to access the first column of $array inside f(). ....

 

$aArray[][0] - If would be nice to be able to use this type of array syntax in AutoIt to get the entire first column of a 2D array.
f("$aArray[][0]") - This is the next best thing.

#include <Array.au3>

Local $aArray[3][10] = [ _ ; 3 rows, 10 columns
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], _
        [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], _
        [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]]
_ArrayDisplay($aArray, "Original Array")

$aCol0 = f("$aArray[][0]") ; First column (Col0)- All the rows of the first column.
ConsoleWrite(UBound($aCol0, 1) & " row x " & UBound($aCol0, 2) & " col" & @CRLF)
_ArrayDisplay($aCol0, "First column")

$aRow0 = f("$aArray[0][]") ; First row (Row[0]) - All the columns of the first row.
ConsoleWrite(UBound($aRow0, 1) & " row x " & UBound($aRow0, 2) & " col" & @CRLF)
_ArrayDisplay($aRow0, "First row")

$aPart2D = f("$aArray[1:2][4:7]") ; Rows 1 and 2, with columns 4 to 7.
ConsoleWrite(UBound($aPart2D, 1) & " row x " & UBound($aPart2D, 2) & " col" & @CRLF)
_ArrayDisplay($aPart2D, "R 1-2; Col 4-7")

$aElement = f("$aArray[1][4]") ; Eleement in row 1 and  column 4.
ConsoleWrite(UBound($aElement, 1) & " row x " & UBound($aElement, 2) & " col  $aElement[0] = " & $aElement[0] & @CRLF)
_ArrayDisplay($aElement, "R 1-2; Col 4-7")


; f("ArrayName[StartRowIndex:EndRowIndex][StartColumnIndex:EndColumnIndex]")
; [Blank] - Will return all the rows or columns.
; [one index number] - Will return only the entire row or column with that index.
;
Func f($s)
    $a = StringRegExpReplace($s, "\[.+$", "") ; Get Array Name
    If IsArray(Execute($a)) Then
        $aRow = StringRegExp($s, "\[(\d*):?(\d*)\]\[", 3) ; Get Row parameters
        $aRow[0] = ($aRow[0] = "" ? -1 : $aRow[0])
        $aRow[1] = ($aRow[1] = "" ? ($aRow[0] = "" ? -1 : $aRow[0]) : $aRow[1])
        ;ConsoleWrite("Row  " & $aRow[0] & "  " & $aRow[1] & @CRLF)
        $aCol = StringRegExp($s, "\[(\d*):?(\d*)\]$", 3) ; Get Column parameters
        $aCol[0] = ($aCol[0] = "" ? -1 : $aCol[0])
        $aCol[1] = ($aCol[1] = "" ? ($aCol[0] = "" ? -1 : $aCol[0]) : $aCol[1])
        ;ConsoleWrite("Col  " & $aCol[0] & "  " & $aCol[1] & @CRLF)
        Return _ArrayExtract(Execute($a), $aRow[0], $aRow[1], $aCol[0], $aCol[1])
    EndIf
EndFunc   ;==>f

 

1 person likes this

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