subst (AutoLISP)

Searches a list for an old item and returns a copy of the list with a new item substituted in place of every occurrence of the old item

Supported Platforms: Windows and Mac OS

Signature

(subst newitem olditem lst)
newitem

Type: Integer, Real, String, List, Symbol, ads_name, T, or nil

An atom or list.

olditem

Type: Integer, Real, String, List, Symbol, ads_name, T, or nil

An atom or list.

lst

Type: List

A list.

Return Values

A list, with newitem replacing all occurrences of olditem. If olditem is not found in lst, subst returns lst unchanged.

Examples

(setq sample '(a b (c d) b))
(A B (C D) B)

(subst 'qq 'b sample)
(A QQ (C D) QQ)

(subst 'qq 'z sample)
(A B (C D) B)

(subst 'qq '(c d) sample)
(A B QQ B)

(subst '(qq rr) '(c d) sample)
(A B (QQ RR) B)

(subst '(qq rr) 'z sample)
(A B (C D) B)

When used in conjunction with assoc, subst provides a convenient means of replacing the value associated with one key in an association list, as demonstrated by the following function calls.

Set variable who to an association list:

(setq who '((first john) (mid q) (last public)))
((FIRST JOHN) (MID Q) (LAST PUBLIC))

The following sets old to (FIRST JOHN) and new to (FIRST J):

(setq old (assoc 'first who) new '(first j))
(FIRST J)

Finally, replace the value of the first item in the association list:

(subst new old who)
((FIRST J) (MID Q) (LAST PUBLIC))