AfraLisp Blog

The AutoLisp/Visual Lisp/VBA Resource Website

AfraLisp Blog

Home Newsletter Utter Rubbish Coding Tips AutoCAD Tips Contact Downloads WAUN

Page I. Home. Page III.

List Manipulation - Page II.

We've now managed to extract elements from a list, but what do you do
if you want to create a new list. Let's say you have two elements :

	(setq a 200.0)
	(setq b 400.0)

You want to combine them to create a new list. To do this you would use the "List" function. For example :

	(setq c (list a b))

Would return :

	(200.0 400.0)

You could also write the function like this :

	(setq c '(a b))

Here's an example of List Manipulation. We are going to use the (car), (cadr) and (list) function to draw a rectangle.

(defun c:rec ( / pt1 pt2 pt3 pt4)

	(setq pt1 (getpoint "\nSelect First Corner: "))
	;get the first point

	(setq pt3 (getcorner pt1 "\nSelect Second Corner: "))
	;get the third point

	(setq pt2 (list (car pt1) (cadr pt3)))
	;construct the second point

	(setq pt4 (list (car pt3) (cadr pt1)))
	;construct the fourth point

	(command "Line" pt1 pt2 pt3 pt4 "c")
	;draw the rectangle



Let's look closer at the line :

	(setq pt2 (list (car pt1) (cadr pt3)))

This function retrieves the first element (x coordinate) from the list pt1, the second element (y coordinate) from the list pt3, creates a list from these elements and stores the list in the variable pt2.
The following diagram should help you to better understand this.



AutoLisp provides other functions to manipulate lists. Let's have a look at some of them.


This takes any number of lists and runs them together as one list :

	(append '(12.0 15.5) '("M20" "M30))

Would return :
	(12.0 15.5 "M20" "M30")


Will return the last element of a list :

	(last '("M20" "M24" "M30"))

Would return :



This returns an integer indicating the number of elements in a list :

	(length '("M20" "M24" "M30"))

Should return :



This function searches a list for a specific element. If found it returns the element plus the remainder of the list :

	(member 'c '(a b c d e f))

would return :

	(c d e f)


Returns a list with it's elements reversed :

	(reverse '(a b c d e f))

Will Return :

	(f e d c b a)


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

	Syntax : (subst newitem olditem lst)

	(setq lst '(a b c d e c)))

	(subst 'f 'c lst)

Would return

	(a b f d e f)

On the next page we will have a look at a more advanced List Manipulation Example.

Page I. Home. Page III.
The AutoLisp/Visual Lisp/VBA Resource Website

Copyright 1999-Perpetuity by AfraLisp

All rights reserved.
Information in this document is subject to change without notice.
Site created and maintained by Kenny Ramage

The AutoLisp/Visual Lisp/VBA Resource Website