Re: "dumping" a map
- Posted by euphoric (admin) Feb 17, 2023
- 1970 views
So, in my tests I want to be able to dump a text representation of a complex map, which includes nested maps, and none of the map functions in section 8.32 of the manual allow me to do this. I therefore would like to write a function that reads all key/value pairs in order to display them on screen, and calls itself recursively if a value is a nested map, but I don't know how to check if a value is a map. Using the map() function returns 1 for integers too, so doesn't help me.
Can anyone point me in the right direction, either to a function that already does what I want, or to tell me how to check if a value is a map or not ?
You can't. Since, as you discovered, "maps" are simply references to in-memory constructs, you cannot "store a map inside a map" on disk. At least, not easily.
The best you can do is convert maps into JSON, which can give you "nested maps." However, even then, it is near impossible to reform a map because there is no way to know what kind of data you stored (not without explicit fields telling you what it is, which I guess is possible, actually). When I had questions about this (or a similar) need, Greg Haberek, current Lord of Euphoria, said this is difficult if not impossible in Euphoria's current incarnation.
You could also serialize the map and store it that way, but, again, you can't have a map of maps and just "save it to disk" without some indication of what your data was before you serialized it. There's gonna be a lot of processing required.
I guess the point is, maps have a specific use-case in Euphoria right now. You might want to consider another data store... Maybe an SQLite database would be better for your use-case.
P.S. I can't remember, but Pete's Phix might make map handling (called dictionaries in Phix) easier. What say you, Pete?