Processing List Elements

AutoCAD AutoLISP & Visual LISP

 
Processing List Elements
 
 
 

Because the value of a list_box tile can contain leading spaces (especially if you are retrieving multiple items), do not test the value as a string comparison. Convert list_box value to an integer first with the atoi function, before processing the list box. You can also use the read function, which converts a token to an integer automatically. For example, for a list named justone that accepts only a single selection, the following code fragment checks to see if the third item in the list was selected:

(setq index ( get_tile "justone"))
(cond
 ((/= index "")               ;See if string is empty.
     (= 2 (atoi index))
                              ; Process the third entry.
     ...
     )
)

It is necessary to first check if the string is empty, because the atoi functions return 0 for an empty string as well as the string "0".

The value of a pop-up list never has a leading space, so you don't have to convert the value. Pop-up lists do not allow for multiple selection.

If the list box supports multiple selection, your program must do the conversion and step through the multiple values in the value string. The following definition of MK_LIST returns a list containing only items the user has selected from the original displist. (In this example, the display list displist is maintained as a global variable.) The MK_LIST function expects to be called with the current $value of the list box:

(defun MK_LIST (readlist / count item retlist)
  (setq count 1)
  (while (setq item (read readlist))
    (setq retlist (cons (nth item displist) retlist))
    (while (and (/= " " (substr readlist count 1))
      (/= "" (substr readlist count 1)))
      (setq count (1+ count))
    )
    (setq readlist (substr readlist count))
  )
  (reverse retlist)
)

Both preceding examples also work for the case of a single selection.