saving a list of symbols...

Notices and updates
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

saving a list of symbols...

Post by 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:
(save str-file sym-1 [sym-2 ... ])
I tried this:

Code: Select all

(save file (map quote tables))
but that's still a list and it doesn't work. What's the solution?! :)

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Code: Select all

> (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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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

Code: Select all

> (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:

Code: Select all

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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:

Code: Select all

> (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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

... finally here the best one:

Code: Select all

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

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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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...!

Locked