Author Topic: How can I identify an associative list?  (Read 263 times)

0 Members and 1 Guest are viewing this topic.

mkweaver

  • Bull Frog
  • Posts: 344
How can I identify an associative list?
« on: June 04, 2019, 05:23:42 PM »
I have the following routine:
Code - Auto/Visual Lisp: [Select]
  1. ;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. Routine:        ToListOfEnts
  3. Purpose:        Reduce the supplied argument to a list of enames
  4. Arguments:      ent, an entity name, an entity list, a vla-object, a selection set or a
  5.                   list/assortment of these.
  6. Returns:        a list of enames
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;
  8. (defun ToListOfEnts (ent /)
  9.   (cond ((= 'ENAME (type ent))                              ;an ename
  10.          (list ent)
  11.         )
  12.         ((= 'VLA-OBJECT (type ent))                         ;a vla-object
  13.          (list (vlax-vla-object->ename ent))
  14.         )
  15.         ((= 'PICKSET (type ent)) (tolist ent))              ;a selection set
  16.         ((and (vl-consp ent)                                ;an elist
  17.               (assoc -1 ent)
  18.               (= 'ENAME (type (cdr (assoc -1 ent))))
  19.          )
  20.          (list (cdr (assoc -1 ent)))
  21.         )
  22.         ((= 'listp (type ent))                              ;a list of any of the objects valid for ent
  23.          (mapcar (function (lambda (e) (ToListOfEnts e))) e)
  24.         )
  25.   )
  26. )

The ToList routine called in the last condition simply takes a selection set and returns a list of the enames therein.

So, the problem I'm having is that the condition starting at line 16 (an entity list as returned by entget) chokes if the list is not an assoc list.  How can I test a variable to see if it's a valid assoc list?

Thanks,
Mike W.



kdub

  • Mesozoic relic
  • SuperMod
  • Swamp Rat
  • Posts: 1364
  • class keyThumper<T>:ILazy<T>
Re: How can I identify an associative list?
« Reply #1 on: June 04, 2019, 07:39:22 PM »
Hi Mike,
A couple things

I changed your mapcar function (  the last argument )
Added a check for a dottted pair as the first argument to the elist test otherwise the (assoc -1 .. ) fails
Changed last conditional (= 'listp (type ent) because it would always return nil
Changed the parameter passed in to 'Argument in place of ent because I was getting confused ...
Note My (kdub:ss->entlist Argument) returns a list of enames not a list of lists as yours may do.

Output :

(ToListOfEnts (setq a (entlast)) )

(ToListOfEnts (setq b (entget (entlast) )) )

(ToListOfEnts (setq c (ssget "X")) )

(ToListOfEnts (setq d (list a b c)))


_$

KDUB:SS->ENTLIST
KDUB:SS->OBJLIST
KDUB:DOTTED-PAIR-P
KDUB:NORMAL-LIST-P
TOLISTOFENTS
_$

(<Entity name: 159d8395620>)
(<Entity name: 159d8395620>)
(<Entity name: 159d8395620> <Entity name: 159d8395610> <Entity name: 159d8395600>)
((<Entity name: 159d8395620>) (<Entity name: 159d8395620>) (<Entity name: 159d8395620> <Entity name: 159d8395610> <Entity name: 159d8395600>))
_$




Revised Code :
Code - Auto/Visual Lisp: [Select]
  1. ;|;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. Routine:        ToListOfEnts
  3. Purpose:        Reduce the supplied argument to a list of enames
  4. Arguments:      ent, an entity name, an entity list, a vla-object, a selection set or a
  5.                   list/assortment of these.
  6. Returns:        a list of enames
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|;
  8. (defun ToListOfEnts (Argument /)
  9.   (cond ((= 'ENAME (type Argument))        ;an ename
  10.          (list Argument)
  11.         )
  12.         ((= 'VLA-OBJECT (type Argument))   ;a vla-object
  13.          (list (vlax-vla-object->ename Argument))
  14.         )
  15.         ((= 'PICKSET (type Argument)) (kdub:ss->entlist Argument))
  16.                                         ;a selection set
  17.         ((and (vl-consp Argument)         ;an elist
  18.               (kdub:dotted-pair-p (car Argument))
  19.               (assoc -1 Argument)
  20.               (= 'ENAME (type (cdr (assoc -1 Argument))))
  21.          )
  22.          (list (cdr (assoc -1 Argument)))
  23.         )
  24.         ((= 'LIST (type Argument))        ;a list of any of the objects valid for Argument
  25.          (mapcar (function (lambda (e) (ToListOfEnts e))) Argument)
  26.         )
  27.   )
  28. )
  29.  



Library stuff :
Code - Auto/Visual Lisp: [Select]
  1. ;;;------------------------------------------------------------------
  2. ;;;------------------------------------------------------------------
  3. ;;;
  4. (defun kdub:ss->entlist (ss / n lst)
  5.   (if ss
  6.     (repeat (setq n (sslength ss))
  7.       (setq lst (cons (ssname ss (setq n (1- n))) lst))
  8.     )
  9.   )
  10. )
  11. ;;;------------------------------------------------------------------
  12. ;;;------------------------------------------------------------------
  13. ;;;
  14. (defun kdub:ss->objlist (ss / n lst)
  15.   (if ss
  16.     (repeat (setq n (sslength ss))
  17.       (setq lst (cons (vlax-ename->vla-object (ssname ss (setq n (1- n)))) lst))
  18.     )
  19.   )
  20. )
  21. ;;;------------------------------------------------------------------
  22. ;;;------------------------------------------------------------------
  23. ;;;
  24.  
  25.  
  26.  ;|--------------------------------------------------------
  27. Function: (KDUB:Dotted-Pair-p arg )
  28. RETURN : T if arg is a dotted-pair list
  29. nil otherwise
  30. Notes:
  31. Anything that is not a list is considered an atom. NIL is an atom.
  32. ----------------------------------------------------------|;
  33.  
  34. (defun kdub:dotted-pair-p (arg) (and (not (atom arg)) (not (listp (cdr arg)))))
  35.  
  36. ;;;------------------------------------------------------------------
  37. ;;;------------------------------------------------------------------
  38. ;;;
  39.  
  40.  ;|---------------------------------------------------------
  41. Function: (KDUB:normal-list-p   arg )
  42. RETURN : T if arg is a list
  43. nil if arg is atom or nil or dotted-pair list
  44. EXAMPLE:
  45. (KDUB:normal-list-p nil)       ==>> nil
  46. (KDUB:normal-list-p "abc")    ==>> nil
  47. (KDUB:normal-list-p '(1 . 3 )) ==>> nil
  48. (KDUB:normal-list-p '((1 2 3)(4 5 6)) ) ==>> T
  49. (KDUB:normal-list-p '((1 2 3)(4 . 5)(a b c)) ) =>> T
  50. (KDUB:normal-list-p '((1 . "first")(2 . "second")) ) ==>> T
  51. (KDUB:normal-list-p (car '((1 . "first")(2 . "second")) )) ==>> nil
  52. (KDUB:normal-list-p (car '(("a" "first")(2 . "second")) )) ==>> T
  53. ----------------------------------------------------------|;
  54. (defun kdub:normal-list-p (arg / return)
  55.   (setq return (if (not (atom arg))
  56.                  (vl-list-length arg)
  57.                  nil
  58.                )
  59.   )
  60.   (if (or (not return) (zerop return))
  61.     nil
  62.     t
  63.   )
  64. )
  65. ;;;------------------------------------------------------------------
  66. ;;;------------------------------------------------------------------
  67. ;;;
  68.  
« Last Edit: June 04, 2019, 07:44:36 PM by kdub »
called Kerry in my other life

Sometimes the question is more important than the answer.
#ridesober

mkweaver

  • Bull Frog
  • Posts: 344
Re: How can I identify an associative list?
« Reply #2 on: June 05, 2019, 10:17:00 AM »
Kerry,
Nice!

I was hoping there was an out of the box function like your kdub:dotted-pair-p.  Apparently not.  Thanks for posting that.

Your comment about my ToList routine returning nested lists sent me into an in depth review of ToList.  I have never identified a case where tolist returned nested lists, but it certainly looks like that is a possibility.  Because of that, I changed ToList to use ssname instead of ssnamex.  I had originally used ssnamex with the thought that it may return sub-entities, but I'm not seeing it now.  It's strange.  SSGET gives one the ability to select sub-entities, but I see no way to get to those.  I thought ssnamex would do that but I don't see how.

Thanks for the help