TheRealSiV

Lista_LISP

Apr 19th, 2020
858
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;; Personal data registry handling functions.
  2. ;;;
  3. ;;; The registry is a list containing as entries associative lists
  4. ;;; with the following structure:
  5. ;;; ((FN . <first name>) (LN . <last name>) (AGE . <age>))
  6. ;;; The registry is held in a global variable named PD_DATA.
  7. ;; Requests data for a new entry from the user and appends
  8. ;; the entry to the registry.
  9. (defun C:PD_ADDENTRY ( / fname lname age entry)
  10. ; get the data from the user
  11.  (initget 1)
  12.  (setq fname (getstring "\nEnter person's first name: "))
  13.  (initget 1)
  14.  (setq lname (getstring "\nEnter person's last name: "))
  15.  (initget 7)
  16.  (setq age (getint "\nEnter person's age: "))
  17.  ; create and append the entry
  18.  
  19.  ; note the usage of the (cons) function with two atom arguments
  20.  ; and that the new entry must be wrapped in a list with the (list)
  21.  ; function prior to being appended to the PD_DATA
  22.  (setq entry (list (cons 'FN fname) (cons 'LN lname)
  23.  (cons 'AGE age))
  24.  PD_DATA (append PD_DATA (list entry))
  25.  )
  26.  
  27.  ; supress return
  28.  (princ)
  29.  )
  30.  
  31. ;; Lists the contents of the personal data registry in
  32. ;; human-readable form.
  33. (defun C:PD_LIST ( / current entry counter)
  34.  
  35.  ; we don't want to destroy the pointer to the actual registry,
  36.  ; so we use an internal var to navigate over the entries
  37.  (setq current PD_DATA
  38.  counter 1) ; this is just for the looks of it
  39.  ; since the navigation pointer will eventually reach the NIL
  40.  ; at the end of the registry list, it's a perfectly good value
  41.  ; to use for the loop test
  42.  (while current
  43.  (setq entry (car current)) ; get the data
  44.  ; the actual data is in the CDRs of data items on the
  45.  ; entry's associative list, the CARs are the keys
  46.  (princ (strcat "\nRecord #" (itoa counter)
  47.  ": " (cdr (assoc 'LN entry))
  48.  ", " (cdr (assoc 'FN entry))
  49.  ". Age " (itoa (cdr (assoc 'AGE entry)))
  50.  )
  51.  )
  52.  (setq current (cdr current) ; next entry
  53.  counter (1+ counter)
  54.  )
  55.  )
  56.  ; suppress return
  57.  (princ)
  58.  )
RAW Paste Data