Getting a matching set of symbols

Started by cormullion, December 11, 2006, 08:44:05 AM

Previous topic - Next topic

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


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

Lutz

#1
> (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

cormullion

#2
> (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...:


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

Lutz

#3
> (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)

cormullion

#4
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!