1. Saving a Map with a Map Inside
- Posted by euphoric (admin) Sep 11, 2011
- 1137 views
I basically have this:
map:map mainMap = map:new() map:map mainMapVars = map:new() -- populate mainMap with some data -- populate mainMapVars with some variables map:put( mainMap, "VARS", mainMapVars ) map:save_map( mainMap, myMapFileName )
which is fine during program execution. HOWEVER, when I go to save it, I need to save the actual map contents, not the pointer to the map.
Then, when I load it back up, I need to put those VARS back into a map of their own.
How do I put the actual map itself, and not the pointer, into mainMap for the save? Or do I need to use another file to save the vars map (yuck!)?
2. Re: Saving a Map with a Map Inside
- Posted by DerekParnell (admin) Sep 11, 2011
- 1103 views
I basically have this:
map:map mainMap = map:new() map:map mainMapVars = map:new() -- populate mainMap with some data -- populate mainMapVars with some variables map:put( mainMap, "VARS", mainMapVars ) map:save_map( mainMap, myMapFileName )
Why does your application need to put VARS inside the mainmap?
3. Re: Saving a Map with a Map Inside
- Posted by euphoric (admin) Sep 11, 2011
- 1072 views
Why does your application need to put VARS inside the mainmap?
It doesn't HAVE to; I'm just doing it to keep the top-level keys uncluttered. I don't really have to do it that way, though. I guess. :)
But I'm sure at some point, somebody is going to want to store maps inside of maps out of necessity, and then the question will come up again. So why not solve it now?
4. Re: Saving a Map with a Map Inside
- Posted by DerekParnell (admin) Sep 11, 2011
- 1114 views
But I'm sure at some point, somebody is going to want to store maps inside of maps out of necessity, and then the question will come up again. So why not solve it now?
Solving it NOW takes time so I was wondering how urgent was your specific need.
Another approach would be to store the 'keys,values' pairs of the VARS map in mainMap instead of the VARS map itself.
map:put(mainMap, "VARS", map:pairs(varMap))
And you can then get them back again ...
varMap = new_from_pairs( map:get(mainMap, "VARS") )
5. Re: Saving a Map with a Map Inside
- Posted by euphoric (admin) Sep 11, 2011
- 1094 views
Thanks, Derek! That should work just fine. I didn't realize there was a new_from_kvpairs(), but that's basically a serializer/deserializer for a map. That should work just fine. A few extra steps, but sufficient for now. :)