Jump to content

Unexpected Ubound result


Go to solution Solved by dmob,

Recommended Posts

I query the number of columns in array but getting unexpected result.
In both cases in the code below, the array is created and displays properly, j

but Ubound(array, 2) returns 0. I was expecting 1.

Can anyone else confirm this on ver 3.3.16 or my expectation is wrong (again) :ermm:?

#include <Array.au3>

Local $aArray[4][4]
For $i = 0 To 3
    For $j = 0 To 3
        $aArray[$i][$j] = $i & $j
    Next
Next
;_ArrayDisplay($aArray, "Original")
Local $aExtract = _ArrayExtract($aArray, -1, -1, 0, 0)
_ArrayDisplay($aExtract, "$aExtract")
ConsoleWrite("Colums1: " & UBound($aExtract, 2) & @CRLF)

Local $aNew[4]
For $i = 0 To 3
    $aNew[$i] = $aArray[$i][0]
Next
_ArrayDisplay($aNew, "$aNew")
ConsoleWrite("Colums2: " & UBound($aNew, 2) & @CRLF)

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" /ErrorStdOut "C:\Users\DMOB\AppData\Local\Temp\Untitled 1.au3"    
Colums1: 0
Colums2: 0
>Exit code: 0    Time: 3.804

Link to comment
Share on other sites

  • Moderators

dmob,

Both of the arrays are 1D (look at the values displayed at the bottom of the ArrayDisplay dialog) so there are no "columns"

And if you check @error after the UBound call, you will see it is non-zero, as the Help file tells you it will be for a failure.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I see.

I have a function B that accepts a 1D or 2D array as parameter. This particular 1D array in the sample code is extracted from a 2D array from another function A.
How would I check for the number of columns in function B?

Edited by dmob
Or would I need to check number of rows in array if Ubound returns error 2 to verify the input array in func B?
Link to comment
Share on other sites

  • Moderators

dmob,

Just use UBound with the $UBOUND_COLUMNS parameter. A 2D array will give you a non-0 return - if the return is 0 then check @error and if it too is set then you have a 1D array.

I have no idea about the "rows" question. Do you mean that you want to use 1D elements as if they were the col 0 elements of each column? If so, then _ArrayTranspose might be the function you need.

M23

 

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Solution
51 minutes ago, Melba23 said:

if the return is 0 then check @error and if it too is set then you have a 1D array

I figured that was the only option. Kinda hard to wrap my head around, I thought a 1D array as in array[4] has 1 column, now I gotta learn it has 0 columns :blink:

Link to comment
Share on other sites

3 hours ago, dmob said:

Ubound(array, 2) returns 0. I was expecting 1

Dmob, I understand you because it always frustrated me too.

We see 1 column in both following cases, when creating an array with :

Local $aArray[10]
; or
Local $aArray[10][1]

But in 1st case (the 1D case) number of cols = 0
And in 2nd case (the 2D case) number of cols = 1

@jpm simplified it when scripting the new ArrayDisplay, with this line :

$_g_ArrayDisplay_nCols = ($_g_ArrayDisplay_iDims = 2) ? UBound($_g_ArrayDisplay_aArray, $UBOUND_COLUMNS) : 1 ; Jpm's 1D => 1 (new !)

(comment at the end of line comes from my reworked version of ArrayDisplay)

I think he did great, because it brought fresh air and simplified the whole function.

Link to comment
Share on other sites

dmob : could you please have a look at the function Sortall() found in the last post of this link, just below the line :

2) Include file "SortAll.au3"

It doesn't check on @error but it could help you with the order of the tests to do in your B function, the 1st step should always be :

If Not IsArray($aArray) Then
    ...

Hope it helps.

Edit: yes, the sentence "always 0 for 1D array" will be found there, in a comment, since the 1st day when the function was posted. Always good to write things easily forgettable :D

Edited by pixelsearch
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

  • Recently Browsing   0 members

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