Getting a matching set of symbols

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

Getting a matching set of symbols

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

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

Post 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

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

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

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

Post 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)

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

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

Locked