View Full Version : access information from a list??
clidalbianco
2009-07-14, 03:42 AM
Hello.
How do I access the information coming from a list?. And this list is composed of the properties of an object. Ie have a list that resulted from the command entget and must access all points in which the number 10 and order these items at lower y. Where to access the first item on the list use assoc 10, but must save the rest of the points 10 ...
Is it possible to do this? help me ...
irneb
2009-07-14, 07:57 AM
Look into using the nth function ... you'll have to step through the list and "recreate it". E.g. using the nth step through from position 0. If you've found a 10 code changed it. For each item just cons it into a new list. Then after stepping though totally, use reverse to turn it back in its original order and use the entmod as usual on the new list.
You could probably do a more efficient way, but that shows the LISP list operations. Unfortunately AutoLISP does not have an ARRAY structure where you can replace one item out of a set. The subst actually does this same form of list processing, only changes ALL found to match with car. Search this forum, I think I remember someone making a function which can do this for you automatically.
If you really want to get efficient with modifications, I'd advise looking into vla/vlax functions and operating on the ActiveX objects.
devitg.89838
2009-07-18, 11:49 PM
Hello.
How do I access the information coming from a list?. And this list is composed of the properties of an object. Ie have a list that resulted from the command entget and must access all points in which the number 10 and order these items at lower y. Where to access the first item on the list use assoc 10, but must save the rest of the points 10 ...
Is it possible to do this? help me ...
It is the much known, and never well ponderate massoc routine .
It is not mine , I salute the author
(defun massoc (key alist / x nlist)
(foreach
x alist
(if (eq key (car x))
(setq nlist (cons (cdr x) nlist))
)
)
(reverse nlist)
)
;;Usage
(setq code-10-list (massoc 10 (entget (car (entsel)))))
The sorting part can be made in Vl function
(vl-load-com)
(defun sort-<-y (lst)
(vl-sort lst '(lambda (x y) (< (cadr x) (cadr y))))
)
irneb
2009-08-10, 03:13 PM
Here's my version;; Return a list of associated dotted pairs
(defun mmassoc (key lst / res item)
(foreach item lst ;Step through entire list
(if (or (equal key (car item)) ;Check if item matches association key
(and (= (type key) 'LIST)
(member (car item) key) ;Or one of the list of association keys
) ;_ end of and
) ;_ end of or
(setq res (cons item res)) ;Add to the result list
) ;_ end of if
) ;_ end of foreach
(reverse res) ;Return the result list in original order
) ;_ end of defunBasically does the same as massoc, only you could use multiple association keys as well. E.g. (mmassoc '(10 11 12) lst) would result in a list containing all 10s, 11s & 12s in the original order.
Also to get around the problem of subst substituting all items matching the old parameter. I've made one which only subs the 1st found after a startpos. Not always needed as usually a Pline's points don't lay on top of each other. But could come in usefull when wanting to change a specific vertex's width from the norm instead of all of them at once.;; Substitute only 1st found after position N
(defun substn (new old lst n / res i item changed)
(if (> n (length lst)) ;If startpos is larger than list length
(setq res lst) ;Just return the list
(progn ;Else
(setq i 0) ;Set counter to 0
(while (< i n) ;Step through list until reach startpos
(setq res (cons (car lst) res) ;Add 1st item from list to result
lst (cdr lst) ;Remove 1st item from list
i (1+ i) ;Increment counter
) ;_ end of setq
) ;_ end of while
(while (and lst ;While still items in list
(setq item (car lst)) ;Get 1st item
) ;_ end of and
(if (and (not changed) ;Check if not yet chaged
(equal old item) ;And if item equals old
) ;_ end of and
(setq res (cons new res) ;Add new into result
changed t ;Set changed flag
) ;_ end of setq
(setq res (cons item res)) ;Else add item into result
) ;_ end of if
(setq lst (cdr lst)) ;Remove 1st item from list
) ;_ end of while
(setq res (reverse res)) ;Change result list's order to original
) ;_ end of progn
) ;_ end of if
res ;Return the result list
) ;_ end of defun
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.