Jump to content

MapKeys() on empty map


Recommended Posts

I was surprised to find that, if a map has no elements, MapKeys() returns an array with no dimensions:

Local $map[]
Local $mapKeys = MapKeys($map)
MsgBox(0,'',UBound($mapKeys,0)&','&UBound($mapKeys,1))

shows 0,0

It would be more logical to me for MapKeys() to return a 1-d array with zero elements.

It would then work with

For $key in MapKeys($map)
...
Next

as does a map with elements.

Also, to my knowledge this is the only case where a 0-d array can exist in AutoIt. (Correct me if I am wrong.)

I also note that the value of @error is 1 for both the argument being an empty map, and for it being a variable or constant other than a map.

I do not see why MapKeys() returns @error=1 for a map with no elements, because to me getting the keys in a map with no elements is not necessarily an error.

Further, the Help says that on failure MapKeys() returns "a zero based array". As I see it, a 0-d array is not based.

Thoughts?

I do have a work-around:

Local $map[]
Local $mapKeys = cMapKeys($map)

Func cMapKeys($map)
    Local $mapKeys = MapKeys($map)
    If @error Then
        If IsMap($map) Then
            Local $mapKeys[0]
        Else
             MsgBox(0,'Error','Argument to cMapKeys() is a '&VarGetType($map))
            Exit
        EndIf
    EndIf
    Return $mapKeys
EndFunc

 

Edited by c.haslam
Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Link to comment
Share on other sites

7 hours ago, c.haslam said:

Also, to my knowledge this is the only case where a 0-d array can exist in AutoIt. (Correct me if I am wrong.)

This isn't a 0D array (that could qualify a flat variable), but a different unicorn: it's a 1D array with zero row.

AutoIt allows array dimension(s) to be 0: Local $a[0] is perfectly valid, just like $a[0][3] (a 2D array of zero row of three columns each, or $a[0][2][5] (a 3D array of zero planes of 2 rows by 5 columns).

All of this is perfectly sensible, as functions returning an array should return a suitable dimensionned array with zero rows when there is no data to return. That would save extra code in applications and verbiage in help. Unfortunately this possibility is relatively recent and older UDFs do return anything like an empty string or an error in such case, which is plain wrong IMVHO.

If you're the only child and someone asks for the number of brothers/sisters you have, you say "zero" or "none" but you never reply "error" or "" (an empty string).

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

what if you are a string and someone asks about the boundaries of your dimensions?

$mapKeys = "this is a thing that is not an array"
MsgBox(0,'',UBound($mapKeys,0)&','&UBound($mapKeys,1))

I'm not saying zero is an incorrect response, but still debate worthy IM-even humbler-O.

Edited by iamtheky
wordiness

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

jchd,

I think that you are mistaking which is the dimension (number of subscripts) value, so below I have an example  using AutoIt $ constants:

#include <AutoItConstants.au3>

Local $ar2d[5][8]
MsgBox(0,'',UBound($ar2d,$UBOUND_DIMENSIONS)&':'&UBound($ar2d,$UBOUND_ROWS)&','&UBound($ar2d,$UBOUND_COLUMNS)); shows 2:5,8  so 5 rows and 8 columns

Local $ar1d[3]
MsgBox(0,'',UBound($ar1d,$UBOUND_DIMENSIONS)&':'&UBound($ar1d,$UBOUND_ROWS))    ; shows 1:3 so 3 rows

Local $map2[]
$map2.one = 1
$map2.two = 2
$map2.three = 3
$map2.four = 4
Local $keyAr = MapKeys($map2)
MsgBox(0,'',UBound($keyAr,$UBOUND_DIMENSIONS)&':'&UBound($keyAr,$UBOUND_ROWS))  ; shows 1:4 so 4 elements

Local $map1[]
$map1.one = 1
Local $keyAr = MapKeys($map1)
MsgBox(0,'',UBound($keyAr,$UBOUND_DIMENSIONS)&':'&UBound($keyAr,$UBOUND_ROWS))  ; shows 1:1 so 1 element


Local $map0[]
$keyAr = MapKeys($map0)
MsgBox(0,'',UBound($keyAr,$UBOUND_DIMENSIONS))  ; shows 0 so neither rows nor columns

I would expect the last example to be:

Local $map0[]
$keyAr = MapKeys($map0)
MsgBox(0,'',UBound($keyAr,$UBOUND_DIMENSIONS)&':'&UBound($keyAr,$UBOUND_ROWS))  ; should show 1:0 so 0 elements

 

Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Link to comment
Share on other sites

I ain't mistaking anything: I never pretended that MapKeys was correct!

ConsoleWrite(@LF & "! Array returned by MapKeys" & @LF)
Local $map0[]
Local $keyAr = MapKeys($map0)
ConsoleWrite("MapKeys of empty Map = bug!  " & @TAB & VarGetType($keyAr) & @TAB & Ubound($keyAr, 0) & '-D : (' & Ubound($keyAr, 1) & ')' & @LF) ; demonstrates bug of MapKeys: array is ill-formed
ReDim $keyAr[3]
ConsoleWrite("ReDim [3] of above           " & @TAB & VarGetType($keyAr) & @TAB & Ubound($keyAr, 0) & '-D : (' & Ubound($keyAr, 1) & ')' & @LF) ; yet array can de ReDim-ed
$map0[17] = "Hello World!"
$keyAr = MapKeys($map0)
ConsoleWrite("MapKeys of non-empty Map     " & @TAB & VarGetType($keyAr) & @TAB & Ubound($keyAr, 0) & '-D : (' & Ubound($keyAr, 1) & ')' & @LF) ; nos bug here: array is correct

; this has to do mith MapKeys only
ConsoleWrite(@LF & "+ Declared and ReDim-ed arrays" & @LF)
Local $a[0]
ConsoleWrite("Local $a[0]                  " & @TAB & VarGetType($a) & @TAB & Ubound($a, 0) & '-D : (' & Ubound($a, 1) & ')' & @LF)
ReDim $a[3]
ConsoleWrite("ReDim $a[3]                  " & @TAB & VarGetType($a) & @TAB & Ubound($a, 0) & '-D : (' & Ubound($a, 1) & ')' & @LF)
ReDim $a[0][3]
ConsoleWrite("ReDim $a[0][3]               " & @TAB & VarGetType($a) & @TAB & Ubound($a, 0) & '-D : (' & Ubound($a, 1) & ', ' & Ubound($a, 2) & ')' & @LF)  ; no more bug
ReDim $a[2][3]
ConsoleWrite("ReDim $a[2][3]               " & @TAB & VarGetType($a) & @TAB & Ubound($a, 0) & '-D : (' & Ubound($a, 1) & ', ' & Ubound($a, 2) & ')' & @LF)
Local $a[2][3]
ConsoleWrite("Local $a[2][3]               " & @TAB & VarGetType($a) & @TAB & Ubound($a, 0) & '-D : (' & Ubound($a, 1) & ', ' & Ubound($a, 2) & ')' & @LF)
Local $a[0][2][0][3][1][5][0]
ConsoleWrite("Local $a[0][2][0][3][1][5][0]" & @TAB & VarGetType($a) & @TAB & Ubound($a, 0) & '-D : (' & Ubound($a, 1))
For $i = 2 To UBound($a, 0)
    ConsoleWrite(', ' & UBound($a, $i))
Next
ConsoleWrite(')' & @LF & @LF)

 

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

We now understand each other: there is a bug in MapKeys.

I did not find it in Trac, so should I create a ticket?

 

 

Edited by c.haslam
Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Link to comment
Share on other sites

  • Moderators

c.haslam,

As it says in the Beta Help file for all the Map* functions:

Warning: This feature is experimental. It may not work, may contain bugs or may be changed or removed without notice.

DO NOT REPORT BUGS OR REQUEST NEW FEATURES FOR THIS FEATURE.

So please do not. And as I understand it the whole Map datatype is broken and so may be removed a t any time - so do not get too attached to it.

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

M23,

OK, I won't create a ticket.

I don't see the warning in 3.3.15.0 Help. Perhaps you would tell me where I can find the warning. I looked in Function Reference > Map Management, in the page for each of the Map*() functions, and in Language Reference > Variables.

I am only using maps to report their values in my cDebug . I can easily remove this feature.

Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Link to comment
Share on other sites

  • Moderators

c.haslam,

Sorry, I am running a pre-release v3.3.15.1 where the warning is in a large red-bordered box at the top of each Map* function page. I have just checked the v3.3.15.0 Help file and you are quite correct - it does not appear there, which I find quite strange as I was sure we had added it. Anyway, it will be in the next Beta release - and my comments above still stand.

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

AFAIK there are deeper and more serious issues with the current implementation of Maps, e.g. vanishing entries and such.

I'd find it terribly unfortunate if Jon decided to drop Maps as they are a real improvement.

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

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