Jump to content

number of elements in 2d array

Recommended Posts

Reread help about UBound().

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)

Link to comment
Share on other sites

UBound accepts an optional Dimension parameter.



Variable An array or map variable
Dimension [optional] For an array - Which dimension size to return:
    $UBOUND_DIMENSIONS (0) = Number of subscripts in the array
    $UBOUND_ROWS (1) = Number of rows in the array (default)
    $UBOUND_COLUMNS (2) = Number of columns in the array
For arrays with more than 2 dimensions, just use the corresponding integer
For a map - this parameter is ignored and the number of keys is returned

Constants are defined in AutoItConstants.au3.

You can use $UBOUND_COLUMNS to return the number of columns.


#include <Array.au3>

Local $aArray1 = [[3,39],[2,36,37],[0,38],[1,40]]

$iRows = UBound($aArray1)
$iColumns = UBound($aArray1, $UBOUND_COLUMNS)

ConsoleWrite("Rows: " & $iRows & ", Columns: " & $iColumns & @CRLF)


Link to comment
Share on other sites

thank you but that would only work if all rows had three columns?

but here three rows have two columns and the searched row has three columns.

this would also be only an example it could be that all columns have completely different lengths,

then I cannot work with the maximum value.

Link to comment
Share on other sites

52 minutes ago, Socrathes said:

that would only work if all rows had three columns?

All rows do have 3 columns.  All rows may not have values for all 3 columns, but 3 columns exist for each and every row.

52 minutes ago, Socrathes said:

this would also be only an example it could be that all columns have completely different lengths

All rows may have a different number of values, but not different dimension sizes.  Your sample array is $a[4][3] because it had to handle the maximum number of elements per dimension, which was 3 for the second dimension.  Just because you didn't specify values in your initialization string, that doesn't meat that the column doesn't exist.



The simple script below shows what your array looks like:

#include <Array.au3>

Global $a = [[3,39],[2,36,37],[0,38],[1,40]]


Edited by TheXman

CryptoNG UDF: Cryptography API: Next Gen
jq UDF: Powerful and Flexible JSON Processor | jqPlayground: An Interactive JSON Processor
Xml2Json UDF: Transform XML to JSON | HttpApi UDF: HTTP Server API | Roku Remote: Example Script

About Me

"Any fool can know.  The point is to understand."  -Albert Einstein
The more you know, the more you understand how little you know.  That's why ignorant people think they know so much.

Link to comment
Share on other sites

20 minutes ago, Socrathes said:

thanks for that of course that makes sense. now the only question would be whether there is a possibility with which one can query the exact number of entries, or must a function be written for that?

You could use a 1D array instead - each entry containing a delimited string? Like so:

Global $aArray = [ "3,39", "2,36,37", "0,38", "1,40" ]

Global $iRows = UBound($aArray)

For $i = 0 To ($iRows - 1) Step 1
    $iColumns = UBound(StringSplit($aArray[$i], ","))
    ConsoleWrite("Row " & $i & " has " & ($iColumns - 1) & " columns" & @CRLF)



Row 0 has 2 columns
Row 1 has 3 columns
Row 2 has 2 columns
Row 3 has 2 columns


Edited by Luke94
Link to comment
Share on other sites

@Socrathes :

You can also stick with a 2D array, since this is what is returned by many functions. The number of elements that are not empty is reported as a 1D array.

Example :

#include <Array.au3>
Global $aArr = [[10, 11, 12], [20, 21], [30, '', 32], [], [50]]
Global $aRowEntries[UBound($aArr)]
_ArrayDisplay($aArr, "2-D Array - Example", Default, Default, Default, "Column 1|Column 2|Column 3")

_ArrayDisplay($aRowEntries, "RowEntries")

Func _SetRowEntries(ByRef $aArr)
    Local $iEntryCounter
    For $i = 0 To UBound($aArr) - 1
        $iEntryCounter = 0
        For $k = 0 To UBound($aArr, $UBOUND_COLUMNS) -1
            If $aArr[$i][$k] <> '' Then $iEntryCounter += 1
        $aRowEntries[$i] = $iEntryCounter



"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...