saving a list of symbols...

Started by cormullion, December 29, 2008, 10:51:16 AM

Previous topic - Next topic

cormullion

My head has been befuddled by Xmas, and I'm puzzled by my inability to solve this problem...



I have a symbol called tables that contains a list of symbols. I want just the contents of those symbols (and the symbol 'tables' itself) to be saved by save (I don't want to save the whole context since there's lots of code there).



But save doesn't like a list:


Quote(save str-file sym-1 [sym-2 ... ])


I tried this:


(save file (map quote tables))

but that's still a list and it doesn't work. What's the solution?! :)

Lutz

#1
> (set 'x 1 'y 2 'z 3)
3
> (set 'table '(x y z))
(x y z)
> (eval (append '(save "mysims.lsp") (map quote table)))
true
>


'append' fabricates the s-expression and 'eval' evaluates it.

cormullion

#2
Thanks Lutz - those look like they should work... But



> (set 'table1 (sequence 0 10))
(0 1 2 3 4 5 6 7 8 9 10)
> (set 'table2 (sequence 0 10))
(0 1 2 3 4 5 6 7 8 9 10)
> (set 'table3 (sequence 0 10))
(0 1 2 3 4 5 6 7 8 9 10)
> (set 't '(table1 table2 table3))
(table1 table2 table3)
> (apply (curry save "fred") t (length t))
true


but fred is only:


(set 'table1 '(0 1 2 3 4 5 6 7 8 9 10))

Lutz

#3
yes, that didn't work (shouldn't because curry will take only one of the remaining). I deleted that post. But the first one does:


> (set 'table1 (sequence 0 10))
(0 1 2 3 4 5 6 7 8 9 10)
> (set 'table2 (sequence 0 10))
(0 1 2 3 4 5 6 7 8 9 10)
> (set 'table3 (sequence 0 10))
(0 1 2 3 4 5 6 7 8 9 10)
> (set 't '(table1 table2 table3))
(table1 table2 table3)
> (eval (append '(save "mysims.lsp") (map quote t)))
true
> !cat mysims.lsp
(set 'table1 '(0 1 2 3 4 5 6 7 8 9 10))

(set 'table2 '(0 1 2 3 4 5 6 7 8 9 10))

(set 'table3 '(0 1 2 3 4 5 6 7 8 9 10))

>

Lutz

#4
... finally here the best one:


(set 'x 1 'y 2 'z 3)
(set 'table '(x y z))

(apply save (cons "mysims.lsp" table))

cormullion

#5
Thanks! That one definitely works...



I'm pleased that you didn't find this question too easy... :)



By the way - is there an ideal (ie not implemented in newLISP yet) solution in other similar languages... ?

Lutz

#6
There are several other functions in newLISP which can take both, multiple single args or args in a list, e.g. 'format' or 'select'. Similar thing could be done with 'save', and I wrote it down as a potential enhancement.



The reason that it hasn't been done already is, that this was the first time, I saw somebody wanting to do it this way. Normally you would group multiple symbols in a name space, not in a table, and when using a name space symbol in 'save' then the whole context space gets saved automatically.



I use this technique frequently when saving configuration sets of variables, e.g. in newlisp-edit.lsp.

cormullion

#7
Ah, yes. And turning a list into an unlist isn't something I need to do very often... :)



The reason I'm not saving the context in its entirety is due to the particular way I've written this application. The context is a simple pure-newlisp database (based on one written by kinghajj many months ago). I've ended up with both database functions and data inside the context. The context can be loaded first, with empty data tables, then the data tables can be loaded into the context later. It's a bit odd, now that I write it down like that... :)



Perhaps I'll post it some day so you can see what strange things your users get up to...!