1. Saving a Map with a Map Inside

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!)?

new topic     » topic index » view message » categorize

2. Re: Saving a Map with a Map Inside

euphoric said...

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?

new topic     » goto parent     » topic index » view message » categorize

3. Re: Saving a Map with a Map Inside

DerekParnell said...

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?

new topic     » goto parent     » topic index » view message » categorize

4. Re: Saving a Map with a Map Inside

euphoric said...

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") ) 
new topic     » goto parent     » topic index » view message » categorize

5. Re: Saving a Map with a Map Inside

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. :)

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu