Page 1 of 1

Getting a matching set of symbols

Posted: Mon Dec 11, 2006 4:44 pm
by cormullion
What's the best way to do this? I want to define a set of symbols, eg t1, t2, t3, t4, and so on throughout a script. Then later i want to run a function over all the symbols that got defined - eg

Code: Select all

(dolist (s (list-of-t-symbols)
 ...(eval s) (name s)...
How do I extract all the symbols starting with t-and-a-digit from (symbols) using a regex pattern?

Posted: Mon Dec 11, 2006 5:59 pm
by Lutz

Code: Select all

> (set 'slist '(one two three four five))
(one two three four five)

> (dolist (s slist) (if (starts-with (name s) "o|f" 0) (println "->" s)))
->one
->four
->five
five
> 
Lutz

Posted: Mon Dec 11, 2006 7:03 pm
by cormullion

Code: Select all

 > (set 'slist '(one two three four five)) 
(one two three four five) 
Hi Lutz! Indeed, that's the bit I want to be able to do another way. Imagine that the symbols have been created on the fly, and that later I want to build a list of them. Eg someone else has done this first without me knowing...:

Code: Select all

(for (i 0 20)
     (set (sym (string "t" i)) (time-of-day))
     (sleep (rand 200))
Now I want to create a list with all the variables starting with t-digit. I don't know how many there are, though...

I couldn't find an easy way to process (symbols) to find and extract matching symbols. Is the only way just to filter a (dolist (s symbols) loop?

Posted: Mon Dec 11, 2006 7:38 pm
by Lutz

Code: Select all

> (set 'slist '(one two three four five)) 
(one two three four five)
> (filter (fn (s) (starts-with (name s) "o|f" 0)) slist)
(one four five)
> 
Lutz

instead of: slist it coulde be: (symbols)

Posted: Mon Dec 11, 2006 9:36 pm
by cormullion
yes - I think that's the one. I don't think it can be done with any of the existing find- functions 'in one go'...

Thanks Lutz - you saved the day again!