Jump to content

My proposal for maps


philpw99
 Share

Recommended Posts

@jpm

Maps variable type has been be in beta for years, and only recently it became officially supported.

I like the idea of object-oriented maps, and I like the features and simplicity it offers.

However, in practice it's too easy to confuse with arrays and other object type, like dictionary. When run some tests, I also encounter the same errors listed here:

After some thinking, I think we really do need to distinguish a map from an array. We cannot just use the same "[]" to reference different types of variables.

So my propose is like this:

=====

To declare a map, you need to declare like this:

 

local $map:[]

; or even simplier:

local $map:

":" indicate that this is a map, not an array, and the difference is enough to be seen right away. You can define the number of elements inside.

local $map:5

; Not: local $map:[5] which means create a map with 5 as the first key

; Or
$number = 5;

local $map:$number

means there are 5 empty elements already in the map.

=====

To set a map element, you just follow the same rule:

$map:0 = "item 1"  ; Set the first map element to be "item 1", with or without a key.

$map:["key 1"] = "item with key"  ; Set a map element with key "key 1" to "item with key", if this key doesn't exist, add a new item in the map.

$map:"key 1" = "item with key" ; It's the same as above.

$map:[0] = "itme with 0 as key" ; You can use 0 as the key.

=====

To reference a map element, you can either use a zero-based index, or a string as the key:

$item = $map:0   ; It will return the first item in the map, with or without a key

$item = $map:[0] ; It will return a item with key 0

$item = $map:["key 1"]    ; It will return the item with key "key 1"

$item = $map:"key 1"    ; Same as above.

===== more about the number referencing:
$number = 5
$item6 = $map:$number  ; It will return the 6th item in the map (zero based)
$item = $map:[$number]      ; It will return the item with the key "5"

=======================

The reason we need to add ":" before the "[",  is to distinguish what we want to get. Do we want to get the map, or an array?

Suppose there is a scenario like this:

local $map[] ; declare a map

local $array[5] ; declare an array

$map[0] = $array

$map[0][0] = 123  ; Error here.

The above statement "$map[0][0]" created an confusion that's difficult to process. it can mean either:

"Set $array[0] to 123 " or "Set $map[0] with key '0' with a new value 123"

It will make the program hard to understand, and error prone.

So if we adopt the new syntax, the purpose will become easy:

$map:0:0 = 123  ; Set the first element $map, which is a map, to first element with value 123
$map:[0]:[0] = 123  ; Set the map element with key 0, which is a map, to element with key 0 to value 123

$map:0[0] = 123  ; Set the first element $map, which is an array, to first data be value 123
$map:[0][0] = 123  ; Set the element in $map with key 0, which is an array, to first data be value 123

$map[0][0] = 123 ; Set the 2D array $map's first data to be 123

$map[0]:0 = 123 ; Set the 1D array $map's first data, which is a map, its first element to be value 123, with or without a key.
$map[0]:[0] = 123 ; Set the 1D array $map's first data, which is a map, its element with key 0 to be value 123.

To reference map's elements by keys are also easy:

$map:"Key 1":"Key 2" = 123 ; Referencing $map["key 1"]["key 2"]

$map.key1:"Key 2" = 123; Reference a dictionary object $map's item("key 1"), to have a map with "key 2" value set to 123

$map:key1.key2 = 123 ; Referencing a map element with a key string, which value is key1.key2, and set this element to 123.

$map:["key1"].key2 = 123 ; Referencing a map elment with key "key1", which is an object, and set its key2 property to 123.

$map.key1.key2 = 123 ; Referencing an object map's property key1.key2, and set it to 123

$map:0:0    ; Referencing the first element in map, which is also a map, the first element

 

 

I think when doing like this, it will be much easier for Jon to program the maps variable, and we will have a much more precise way to reference and set map and array values.

Please leave your comments for this proposal. Thank you !

 

 

 

 

 

 

 

 

Edited by philpw99
Link to comment
Share on other sites

In AutoIt it is suggested to use the hungarian notation for variables.
Arrays use "a" and Maps use "m" as the the first character.
This way it is quite easy to distinguish them.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

It's not about the name conversion, but how to define and referencing maps.

The way maps works now is not good, quite confusing, and very easy to have program error.

for example, if I give you something like this:

$var["key"]

How do you know the $var is referencing to a dict object, or a map?

$var[0]

How do you know $var is a 1D array, or just map with 0 as the key?

It gets much more complicated once the referencing 2D to 3D  mixture of array, dict and maps variables.

like $var[0][0][0]

which one is array, which one is map with 0 as key, which one is dict object with 0 as key ?

Edited by philpw99
Link to comment
Share on other sites

If I had to answer it would be simple: know your data and have the lead over the datatypes you deal with. You're the programmer after all, aren't you?

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

9 hours ago, philpw99 said:

How do you know $var is a 1D array, or just map with 0 as the key?

Lets assume you need to process some employee data.
Using the hungarian notation I would name a map $mEmployee and an array $aEmployee. Problem solved ;)

If you really need to know wether it is a map or an array use functions IsMap or IsArray. Function VarGetType helps as well.
 

6 hours ago, jchd said:

You're the programmer after all, aren't you?

I second that.

 

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

6 hours ago, jchd said:

If I had to answer it would be simple: know your data and have the lead over the datatypes you deal with. You're the programmer after all, aren't you?

I tend to agree with your statement @jchd in general, as long as the project isn't shared with several developers (collaboration project). Then it should be definitely clear what's going on in the code without long onboarding etc. But this brings me to @waters statement that I agree with, too:

27 minutes ago, water said:

[...] Using the hungarian notation I would name a map $mEmployee and an array $aEmployee [...] If you really need to know wether it is a map or an array use functions IsMap or IsArray. Function VarGetType helps as well [...]

 

As addition I can say that modern IDEs like VSCode supports you by the variable hover tooltip which show up the data type information.
Unfortunately not yet for AutoIt in VSCode, but loganch and other working on it 😀 .

Best regards
Sven

Edited by SOLVE-SMART

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

Guys, you completely misunderstood the purpose of my proposal.

This is not about how to know your own program's variables. This is about making AutoIt program itself better and less error prone.

Please, take a look at the post again:

 

Please , read that post and understand the problem. It's NOT about how to let programmer know which variable is which type, it's about how to let AutoIt know which variable is which type.

The AutoIt program generate errors at some places, like $map1.bar["baz"] will give you an error, while $map1.bar.baz works just fine. The problem is: sometimes is very hard for AutoIt to know what the programmer intents, therefore there are errors in some strange ways. And this is why maps has been in beta for years, and it's still not stable.

The proposal here is to let Maps become more specific, more precise and more stable.

Maybe I post this one in the wrong place. This is not belong to general technical discussion?

Link to comment
Share on other sites

29 minutes ago, philpw99 said:

The AutoIt program generate errors at some places, like $map1.bar["baz"] will give you an error, while $map1.bar.baz works just fine.

Sounds like a bug in the parser/interpreter to me, this is definitely fixable without any syntax change and it should be fixed. Tagging @jpm so that we can get his opinion on the issue as an AutoIt developer :)

Edited by TheDcoder

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

This is just a common misunderstanding of the priorities between dot and [].

In $map1.bar["baz"] the first part evaluated is bar["baz"], which obviously raises an error.

There are many possible valid syntaxes:

Local $m[], $p[]

$m["foot"] = 123
$p["bare"] = $m

ConsoleWrite($p.bare.foot & @LF)
ConsoleWrite(($p.bare)["foot"] & @LF)
ConsoleWrite(($p["bare"]).foot & @LF)
ConsoleWrite($p["bare"].foot & @LF)
ConsoleWrite(($p["bare"])["foot"] & @LF)
ConsoleWrite($p["bare"]["foot"] & @LF)

What makes you think that the Map datatype isn't "stable"?

Edited by jchd

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

2 hours ago, philpw99 said:

it's about how to let AutoIt know which variable is which type.

The interpreter knows that very well!

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

$map1.bar["baz"] has two meanings:

* Referencing $map1's element "bar" 's element "baz"

* Referencing $map1's element by using bar["baz"] as the key.

We programmer obviously know bar["baz"] doesn't exist, so it must be the first meaning, but how do AutoIt know?

Jchd: try this, maybe you can fix it?

Local $m[], $p[]

$m["foot"] = 123
$p["bare"] = $m

$p.bare.foot = 345          ; OK
$p.bare["foot"] = 345       ; Error here
$p["bare"].foot = 345       ; OK
$p["bare"]["foot"] = 345    ; OK

 

Edited by philpw99
Link to comment
Share on other sites

Quote
$p.["bare"].foot = 345          ; Error here
$p.["bare"]["foot"] = 345       ; Error here

Use the dot format xor the square braket format. Using both is an error IFAIK.

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

Sorry, I see that now. Correct it. Still has errors.

Remember this is a very simple example. When the program grows big, when maps grow like a big tree , this will become very problematic.

$p.bare["foot"] = 345       ; Error here
Edited by philpw99
Link to comment
Share on other sites

3 hours ago, TheDcoder said:

Sounds like a bug in the parser/interpreter to me, this is definitely fixable without any syntax change and it should be fixed. Tagging @jpm so that we can get his opinion on the issue as an AutoIt developer :)

Thank you for pointing me to a great direction ! Just edited my post and tagged Jon.

In my opinion, this create too much of confusion.

* Array use "[]", dictionary and others objects use "." , map use both "[]" and "."  

* All of them can be expressed as $v[0]

* Both Dictionary and Maps can be expressed as $v["key"]

Even the program can run fine, it will be quite difficult to troubleshoot problem related to dict, array and maps using together.

As @SOLVE-SMART, @jchd and @water previously stated, we can use Hungarian notation to reduce the confusion. Yet sometimes we humans think one way, and the machine interpret it another way. If we use the notation wrong, or machine thinks differently, it will create even more difficulties for troubleshooting.

Edited by philpw99
Link to comment
Share on other sites

Also, my proposal solves the referencing maps with number index problem.

Right now, you cannot access maps by just the first element, second element... You have to access it by a key. It reduce maps flexibility a lot.

We cannot write a simple $map[0] to indicate we want to use the first element. We have to use MapKeys() then get the first one in the array. It's not efficient and need extra coding if all we want is to get the first few elements.

In my proposal, $map:0 means the first element, $map:[0] means a element with key "0". Both has important uses in programming, and we need to distinguish them and enable both of them in a concise and precise way.

Edited by philpw99
Link to comment
Share on other sites

A Map is a dictionary, and map elements don't have an index: they're accessed thru their key.

What you're dreaming of is a combination of an [indexed] array and a dictionary (aka an associative array). Also note that an AutoIt Map key can be a string or an integer. In the latter case, this isn't an index.

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

On 1/13/2023 at 1:21 PM, jchd said:

What you're dreaming of is a combination of an [indexed] array and a dictionary (aka an associative array).

Why this must be a dream ? In computer everything is indexed, every object is indexed, all you can see on this screen, all the text, buttons, tags, inputs are indexed.

Then why maps cannot be indexed ? Why maps has to follow the rules of dictionary ?

If maps cannot be indexed, then the advantage of using maps is reduced to:

1. We can use $map.key instead of $map("key") .

2. We don't need dict object creation.

I don't know how maps are implemented. If AutoIt really uses dictionary object, then I got nothing to say, but if it's implemented using its own methods, index should be a good thing to have.

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

×
×
  • Create New...