Author Topic: MapCreate error?  (Read 916 times)

bottomleypotts

  • Newbie
  • *
  • Posts: 42
MapCreate error?
« on: December 09, 2020, 04:15:53 pm »
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.

Code: [Select]
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

  • Tech Support
  • *****
  • Posts: 4059
    • WinBatch
Re: MapCreate error?
« Reply #1 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
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

  • Tech Support
  • *****
  • Posts: 4059
    • WinBatch
Re: MapCreate error?
« Reply #2 on: December 09, 2020, 07:10:05 pm »
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

  • Newbie
  • *
  • Posts: 42
Re: MapCreate error?
« Reply #3 on: December 09, 2020, 09:08:19 pm »
My use-case is reading from a database and converting straight into a map.

bottomleypotts

  • Newbie
  • *
  • Posts: 42
Re: MapCreate error?
« Reply #4 on: December 09, 2020, 09:12:24 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
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

  • Tech Support
  • *****
  • Posts: 4059
    • WinBatch
Re: MapCreate error?
« Reply #5 on: December 09, 2020, 10:49:53 pm »
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
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

  • Tech Support
  • *****
  • Posts: 4059
    • WinBatch
Re: MapCreate error?
« Reply #6 on: December 10, 2020, 06:51:52 am »
And empty strings work if the syntax rules are followed:

Code: Winbatch
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

  • Newbie
  • *
  • Posts: 42
Re: MapCreate error?
« Reply #7 on: December 10, 2020, 06:04:42 pm »
Ok. My problem. Maps are not appropriate to my use case.

ChuckC

  • Sr. Member
  • ****
  • Posts: 389
Re: MapCreate error?
« Reply #8 on: December 11, 2020, 06:48:42 am »
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

  • Newbie
  • *
  • Posts: 42
Re: MapCreate error?
« Reply #9 on: December 11, 2020, 05:18:44 pm »
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

  • Pundit
  • *****
  • Posts: 1808
    • Data & Stuff Inc.
Re: MapCreate error?
« Reply #10 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

stanl

  • Pundit
  • *****
  • Posts: 1632
Re: MapCreate error?
« Reply #11 on: December 12, 2020, 02:40:59 am »
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

  • Sr. Member
  • ****
  • Posts: 389
Re: MapCreate error?
« Reply #12 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.

bottomleypotts

  • Newbie
  • *
  • Posts: 42
Re: MapCreate error?
« Reply #13 on: December 12, 2020, 05:40:53 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

  • Newbie
  • *
  • Posts: 42
Re: MapCreate error?
« Reply #14 on: December 12, 2020, 05:45:31 am »
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

  • Pundit
  • *****
  • Posts: 1808
    • Data & Stuff Inc.
Re: MapCreate error?
« Reply #15 on: December 12, 2020, 08:18:54 am »
...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

  • Tech Support
  • *****
  • Posts: 4059
    • WinBatch
Re: MapCreate error?
« Reply #16 on: December 12, 2020, 09:24:04 am »
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

  • Pundit
  • *****
  • Posts: 1632
Re: MapCreate error?
« Reply #17 on: December 12, 2020, 12:11:41 pm »
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.