MapCreate error?

Started by bottomleypotts, December 09, 2020, 04:15:53 PM

Previous topic - Next topic

bottomleypotts

I am just wondering if there is an issue with MapCreate? I know maps can have a key of "", but it seems when you initialise a map that has a key of "", it fails with an error.

i="a":@TAB:"alpha":@CR:"b":@TAB:"beta"
m=MapCreate(i,@TAB,@CR) ; works

m[""]="blank" ; works also

i="":@TAB:"blank":@CR:"a":@TAB:"alpha":@CR:"b":@TAB:"beta"
m=MapCreate(i,@TAB,@CR) ; fails

td

Empty string keys are not supported by the MapCreate function because leading spaces are removed from key names by MapCreate for obvious reasons. However, something like this works:

Code (winbatch) Select
i="' '":@TAB:"blank":@CR:"a":@TAB:"alpha":@CR:"b":@TAB:"beta"
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

Or you could just use MapCreate minus the space entry and set that space/value pair by direct assignment.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

bottomleypotts

My use-case is reading from a database and converting straight into a map.

bottomleypotts

Quote from: td on December 09, 2020, 04:54:20 PM
Empty string keys are not supported by the MapCreate function because leading spaces are removed from key names by MapCreate for obvious reasons. However, something like this works:

Code (winbatch) Select
i="' '":@TAB:"blank":@CR:"a":@TAB:"alpha":@CR:"b":@TAB:"beta"

If a key of "" is a valid, then the "obvious reason" for MapCreate throwing an error does not make sense (at least to me).

td

The function is dealing with a string of words from any source. It cannot assume the significance of leading spaces for strings of unknown origin because that would lead to a lot of confusing problems with lookups. Also, I had the quotes reversed. The string should be

Code (winbatch) Select
i='" "':@TAB:"blank":@CR:"a":@TAB:"alpha":@CR:"b":@TAB:"beta"

sorry about that... The string handling is modeled after WinBatch's CSV file processing for consistency.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

And empty strings work if the syntax rules are followed:

Code (winbatch) Select
i='""':@TAB:"empty":@CR:"a":@TAB:"alpha":@CR:"b":@TAB:"beta"

Note that something like m[""]=whatever doesn't require the WIL interpreter to guess about the meaning because the string does not need to be parsed.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

bottomleypotts

Ok. My problem. Maps are not appropriate to my use case.

ChuckC

Before calling MapCreate(), you do have the possibility of performing some pre-processing of the map data string that was dynamically obtained from some "other" source to perform some "scrubbing" of the data.

bottomleypotts

I would like something like Maps, but where the 'implementation' respected the key.

Having to pre-process would then require post-processing, because obviously they keys would never match.

I have no idea in what context anyone would use Maps. Why would I ever want my key modified?

JTaylor

What are you needing to accomplish?  Sorry if I missed it but didn't see anything that made it clear.

Jim

stanl

Quote from: JTaylor on December 11, 2020, 06:45:11 PM
What are you needing to accomplish?  Sorry if I missed it but didn't see anything that made it clear.

Jim


My question as well: if "reading from a database", assumes a recordset, so why need a Map?

ChuckC

If the keys need to be preserved, then you will have to pass on using native WIL maps and delve down into the .NET Framework and make use of a Dictionary or SortedDictionary, both of which are generic types where you can specify the data type for the "key" and for the "value".  Using a string as the "key" type will permit you to have keys with leading spaces.

bottomleypotts

Quote from: ChuckC on December 12, 2020, 04:18:51 AM
If the keys need to be preserved, then you will have to pass on using native WIL maps and delve down into the .NET Framework and make use of a Dictionary or SortedDictionary, both of which are generic types where you can specify the data type for the "key" and for the "value".  Using a string as the "key" type will permit you to have keys with leading spaces.

If you had a snippet of code to share that would be appreciated.

bottomleypotts

Quote from: stanl on December 12, 2020, 02:40:59 AM
Quote from: JTaylor on December 11, 2020, 06:45:11 PM
What are you needing to accomplish?  Sorry if I missed it but didn't see anything that made it clear.

Jim


My question as well: if "reading from a database", assumes a recordset, so why need a Map?

I'm importing about 10000 pieces of information into a MSSQL database, and the process goes a lot quicker (by a factor of 10) when I do the processing locally, so I read a table ID and a key and parse those into the SQL statements that upload the data.

Unfortunately for this project I no longer have access to the server in order to run the app locally.

JTaylor

...and the values of one or both of these fields can be blank and/or repeatable?   Sorry if I am being dense.   If you could show a very small bit of [similar] data that you start with and the steps to the end, I am sure someone here could help.   The data of which you speak sounds like something that would not be blank and, at least the key, would be unique but I must be missing something.


Jim

td

Quoting empty values is fairly trivial provided there is a consistent delimiter and something approaching a reasonable file format. Of course, repeating "key" values is a deal killer assuming all data is to be preserved because by definition a map's keys must be unique.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

stanl

I'm again, still sorry and confused. I had to work with MSSQL for years without direct server access, but was able to quickly parse incoming data [CDL records] into fabricated recordsets - ergo typed data - for later inserting into a table. So, while not arguing against the use of Maps I still have to question why that is the primary choice.