foreach (AutoLISP)

Evaluates expressions for all members of a list

Supported Platforms: Windows and Mac OS

Signature

(foreach name list [expr...])
name

Type: Symbol

Variable that each element in the list will be assigned to.

list

Type: List

List to be stepped through and evaluated.

expr

Type: List

Expression to be evaluated for each element in list.

Return Values

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

The result of the last expr evaluated. If no expr is specified, foreach returns nil.

Remarks

The foreach function steps through a list, assigning each element in the list to a variable, and evaluates each expression for every element in the list. Any number of expressions can be specified.

Examples

Print each element in a list:

(foreach n '(a b c) (print n))
A
B
C C

foreach prints each element in the list and returns C, the last element. This command is equivalent to the following sequence of commands, except that foreach returns the result of only the last expression evaluated:

(print a) 
(print b) 
(print c)