MapInMap

Started by spl, July 09, 2024, 12:11:21 PM

Previous topic - Next topic

spl

Upgraded to 2024A + Compiler. First thought was to play with Maps and Json. Maps can be considered hashtables and hashtables can output as Json. So, how about nesting a Map. Below is dumb code, but interesting
Enums = $"Phone=123-256-7890
email=john.doe@example.com$"
Enums = StrReplace(Enums, @cr, "") ; Remove editor dependence
SubMap = MapCreate(Enums, "=", @lf)

Enums1 = $"Name=John Doe
Company=Tesla
Occupation=Safety Inspector
Veteren=true
Contact=%SubMap%$"
Enums1 = StrReplace(Enums1, @cr, "") ; Remove editor dependence
MainMap = MapCreate(Enums1, "=", @lf)

Message(MainMap["Name"],"Contact Info:" : MainMap["Contact"])
Exit

Will run and display Contact as an array. If I try adding
info = ""
Foreach c in MainMap["Contact"]
   info += c.Phone : @LF : c.email
Next

;then
Message(MainMap["Name"],"Contact Info:" : info)

I will get an error 1690 Unable to Perform enumeration of specified Object. Probably should set up Contact as an array in advance. Something to play with.. with eventual outcome to be
{
    "Name":  "John Doe",
    "Company":  "Tesla",
    "Occupation":  "Safety Inspector",
    "Veteren":  true,
    "Contact":  {
                    "Email":  "john.doe@example.com",
                    "Phone":  "123-456-7890"
                }
}

Stan - formerly stanl [ex-Pundit]

td

The line Contact=%SubMap%$" simply creates a map key's value containing the variable's datatype. In other words the string "<array>". That is how substitution works in this case and another reason to use it with extreme care.

Also, maps are implemented in WIL as arrays and not COM objects so you cannot use dot notation to access names and values. You need to use array notation.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

If you really want to use dot notation with hash tables, you could use the venerable "Script.Dictionary" COM Automation object. But then you already knew that...
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

Quote from: td on July 09, 2024, 10:24:38 PMIf you really want to use dot notation with hash tables, you could use the venerable "Script.Dictionary" COM Automation object. But then you already knew that...

Yep. I posted something with Script.Dictionary some weeks back. I was thinking that since Maps or HashTables have unique keys I would use the Map as a 'record' and dictionary as the recordset. Was trolling my archives and came across posts for MsShape persisted recordsets [ circa 2002-2004 ] and took the leap of faith that a Map might behave accordingly. Jim has also reminded me that generic .Net collections are possible in WB with in-memory C# compiled code. But since WB already has a robust Json Extender, that would be fantasy for writing nested Json. 
Stan - formerly stanl [ex-Pundit]

spl

Under the beating the dead horse category, I forgot all about this: https://forum.winbatch.com/index.php?topic=3054.0

Possibly can create an ArrayList and output to Nested Json. Played around iterating installed PS modules with version info, but should work equally with other file system or SQL iterations. [attached in zip].
Stan - formerly stanl [ex-Pundit]

spl

Here is the tricky part. The attached used the same process but this time to nest the WB 64-bit dll's from latest update as placed in Program files. You will note that while Json syntax in intact both the last write time and file size will require further processing when reading the Json. Can these be made more easily read [of course they can].....
Stan - formerly stanl [ex-Pundit]

spl

Well, time to abandon or ignore this thread. Although there is a jsConNew() function, the jsValueAdd() for arrays says
@JsonArr (16) An array.  Ignored. Set to an empty string ("")
 
Stan - formerly stanl [ex-Pundit]

td

The jsValueAdd creates an empty array so the value parameter is ignored. This is because JSON arrays are containers and not simply name/value pairs. If you want to add values to the array use jsValueAdd to add the elements.

For example:
Json = jsConNew(@JsonObj)
jsValueAdd(Json, 'somearray', '', @JsonArr)
jsValueAdd(Json, 'somearray[0]', 'SomeValue', @JsonStr)

"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

Quote from: td on July 19, 2024, 09:21:04 AMThe jsValueAdd creates an empty array so the value parameter is ignored. This is because JSON arrays are containers and not simply name/value pairs. If you want to add values to the array use jsValueAdd to add elements using the jsValueAdd function.


If you see how convoluted the response was, you can see.... any confusion.
Stan - formerly stanl [ex-Pundit]

td

Convoluted? Perhaps for some. I did add an extra cause to the end of the third sentence of the response which I have since removed. 
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

spl

Stan - formerly stanl [ex-Pundit]