Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Please help with assoc list.

43 REPLIES 43
SOLVED
Reply
Message 1 of 44
mid-awe
1063 Views, 43 Replies

Please help with assoc list.

Hi all,

 

I am working on a function that accepts a light type and prompts for a color-of-fixture and them should return the model number from an associated list, but I cannot seem to understand what I'm doing wrong. Below is my current code:

(DEFUN C:LIGHT-MODEL (LIGHTTYPE / BBQ CATEGORY FLOOD HANGING LEDGE LGTCOL MASONRY PATH SPOT STEP UNDERWATER WELL)
  (SETQ	PATH	   '((GRANITE PR-4704-GT) (RUST PR-4704-R) (BLACK PR-4704-B) (COPPER PR-2165-CSN))
	SPOT	   '((GRANITE GR-2216-GT) (RUST GR-2216-R) (BLACK GR-2216-B) (COPPER GR-2116-CSN))
	STEP	   '((GRANITE SL-4246-GT) (RUST SL-4246-R) (BLACK SL-4246-B) (COPPER SL-2146-CSN))
	WELL	   '((BLACK GW-5240-B) (LED GW-5272-LED))
	BBQ	   '((STEEL FL-4017-N))
	FLOOD	   '((GRANITE GR-7218-GT) (RUST GR-7218-R) (BLACK GR-7218-B) (COPPER GR-2104-CSN))
	MASONRY	   '((GRANITE SL-4250-GT) (RUST SL-4250-R) (BLACK SL-4250-B))
	HANGING	   '((GRANITE DS-2237-GT) (RUST DS-2237-R) (BLACK DS-2237-B))
	LEDGE	   '((GRANITE SL-4260-GT) (RUST SL-4260-R) (BLACK SL-4260-B) (COPPER SL-4260-CSN))
	UNDERWATER '((BLACK UW-5217-B))
  )
  (COND	((VL-POSITION LIGHTTYPE '("PATH" "SPOT" "STEP" "FLOOD" "LEDGE")) (SETQ PROMPT$ "GRANITE/RUST/BLACK/COPPER"))
	((VL-POSITION LIGHTTYPE '("MASONRY" "HANGING")) (SETQ PROMPT$ "GRANITE/RUST/BLACK"))
	((VL-POSITION LIGHTTYPE '("WELL")) (SETQ PROMPT$ "BLACK/LED"))
	((VL-POSITION LIGHTTYPE '("BBQ")) (SETQ LGTcol "STEEL"))
	((VL-POSITION LIGHTTYPE '("UNDERWATER")) (SETQ LGTcol "BLACK"))
  )
  (IF (VL-POSITION LIGHTTYPE '("PATH" "SPOT" "STEP" "FLOOD" "MASONRY" "HANGING" "LEDGE"))
    (PROGN (INITGET 1 "GRANITE RUST BLACK COPPER")
	   (SETQ LGTcol (GETKWORD (STRCAT "\n\t =>> " LIGHTTYPE " Light Color [" PROMPT$ "]: ")))
    )
  )
  (IF (SETQ LIGHTS '(PATH SPOT STEP WELL BBQ FLOOD MASONRY HANGING LEDGE UNDERWATER))
    (FOREACH LIGHT LIGHTS
      (SETQ CATEGORY (VL-SYMBOL-NAME LIGHT))
      (IF (= CATEGORY LIGHTTYPE)
	(SETQ LGT (ASSOC LGTcol LIGHT))
      )
    )
  )
  (PRINC)
)

 Any help is greatly appreciated. Thank you in advance.

43 REPLIES 43
Message 2 of 44
BlackBox_
in reply to: mid-awe

Couple of things to get you started....

 

Firstly, a Defun which includes the "C:" prefix cannot accept arguments. The "C:" indicates a command, and not a sub-function (which does accept arguments).

 

As for the association list, consider between your data list, and this quick example:

(defun _GetModel (material / itemFound)
  ;; Example: (_GetModel "BLACK")
  (if (setq itemFound (assoc material
                             '(("GRANITE" . "PR-4704-GT")
                               ("RUST" . "PR-4704-R")
                               ("BLACK" . "PR-4704-B")
                               ("COPPER" . "PR-2165-CSN")
                              )
                      )
      )
    ;; return model #
    (cdr itemFound)
  )
)

 

 



"How we think determines what we do, and what we do determines what we get."

Message 3 of 44
BlackBox_
in reply to: BlackBox_


@BlackBox_ wrote:

 

As for the association list, consider between your data list, and this quick example:

(defun _GetModel (material / itemFound)
  ;; Example: (_GetModel "BLACK")
  (if (setq itemFound (assoc material
                             '(("GRANITE" . "PR-4704-GT")
                               ("RUST" . "PR-4704-R")
                               ("BLACK" . "PR-4704-B")
                               ("COPPER" . "PR-2165-CSN")
                              )
                      )
      )
    ;; return model #
    (cdr itemFound)
  )
)

 

 


Also, I just wanted to point out that the above code was written specifically to demonstrate the difference between argument (parameter) and local variable.

 

One could reduce the code to simply:

(defun _GetModel (material / ;|no local variables|;)
  (cdr (assoc material
              '(("GRANITE" . "PR-4704-GT")
                ("RUST" . "PR-4704-R")
                ("BLACK" . "PR-4704-B")
                ("COPPER" . "PR-2165-CSN")
               )
       )
  )
)

 



"How we think determines what we do, and what we do determines what we get."

Message 4 of 44
alanjt_
in reply to: mid-awe


@mid-awe wrote:

Hi all,

 

I am working on a function that accepts a light type and prompts for a color-of-fixture and them should return the model number from an associated list, but I cannot seem to understand what I'm doing wrong. Below is my current code:

(DEFUN C:LIGHT-MODEL (LIGHTTYPE / BBQ CATEGORY FLOOD HANGING LEDGE LGTCOL MASONRY PATH SPOT STEP UNDERWATER WELL)
  (SETQ	PATH	   '((GRANITE PR-4704-GT) (RUST PR-4704-R) (BLACK PR-4704-B) (COPPER PR-2165-CSN))
	SPOT	   '((GRANITE GR-2216-GT) (RUST GR-2216-R) (BLACK GR-2216-B) (COPPER GR-2116-CSN))
	STEP	   '((GRANITE SL-4246-GT) (RUST SL-4246-R) (BLACK SL-4246-B) (COPPER SL-2146-CSN))
	WELL	   '((BLACK GW-5240-B) (LED GW-5272-LED))
	BBQ	   '((STEEL FL-4017-N))
	FLOOD	   '((GRANITE GR-7218-GT) (RUST GR-7218-R) (BLACK GR-7218-B) (COPPER GR-2104-CSN))
	MASONRY	   '((GRANITE SL-4250-GT) (RUST SL-4250-R) (BLACK SL-4250-B))
	HANGING	   '((GRANITE DS-2237-GT) (RUST DS-2237-R) (BLACK DS-2237-B))
	LEDGE	   '((GRANITE SL-4260-GT) (RUST SL-4260-R) (BLACK SL-4260-B) (COPPER SL-4260-CSN))
	UNDERWATER '((BLACK UW-5217-B))
  )
  (COND	((VL-POSITION LIGHTTYPE '("PATH" "SPOT" "STEP" "FLOOD" "LEDGE")) (SETQ PROMPT$ "GRANITE/RUST/BLACK/COPPER"))
	((VL-POSITION LIGHTTYPE '("MASONRY" "HANGING")) (SETQ PROMPT$ "GRANITE/RUST/BLACK"))
	((VL-POSITION LIGHTTYPE '("WELL")) (SETQ PROMPT$ "BLACK/LED"))
	((VL-POSITION LIGHTTYPE '("BBQ")) (SETQ LGTcol "STEEL"))
	((VL-POSITION LIGHTTYPE '("UNDERWATER")) (SETQ LGTcol "BLACK"))
  )
  (IF (VL-POSITION LIGHTTYPE '("PATH" "SPOT" "STEP" "FLOOD" "MASONRY" "HANGING" "LEDGE"))
    (PROGN (INITGET 1 "GRANITE RUST BLACK COPPER")
	   (SETQ LGTcol (GETKWORD (STRCAT "\n\t =>> " LIGHTTYPE " Light Color [" PROMPT$ "]: ")))
    )
  )
  (IF (SETQ LIGHTS '(PATH SPOT STEP WELL BBQ FLOOD MASONRY HANGING LEDGE UNDERWATER))
    (FOREACH LIGHT LIGHTS
      (SETQ CATEGORY (VL-SYMBOL-NAME LIGHT))
      (IF (= CATEGORY LIGHTTYPE)
	(SETQ LGT (ASSOC LGTcol LIGHT))
      )
    )
  )
  (PRINC)
)

 Any help is greatly appreciated. Thank you in advance.


FYI, there is no reason to check if assoc gives a non-nil return, before feeding it to cdr.

 

eg. (cdr nil) => nil

 

slight revision: 

(defun _getModel (material)
  (cdr (assoc material
              '(("GRANITE" . "PR-4704-GT")
                ("RUST" . "PR-4704-R")
                ("BLACK" . "PR-4704-B")
                ("COPPER" . "PR-2165-CSN")
               )
       )
  )
)

 

Message 5 of 44
mid-awe
in reply to: BlackBox_

Completely off topic, but (C:LIGHT-MODEL "SPOT") is perfectly acceptable input to my function. I don't build functions with a C: prefix usually; this one is just testing for now until it is working then I'll drop the C: from the name.
Message 6 of 44
mid-awe
in reply to: alanjt_

I'm not sure how to follow the advice given, since I need a double check. First the argument to the function which is the TYPE of light fixture; and then, the material for the light fixture. But, I need the Light TYPE, Light Material, and the Model Number all returned.
Message 7 of 44
alanjt_
in reply to: mid-awe

Never mind, I see you responded to youself. Stupid no delete option.

Message 8 of 44
Kent1Cooper
in reply to: mid-awe

I'm not positive without digging a little deeper, and this may not be the whole answer, but I believe you need to change this line:

 

(IF (SETQ LIGHTS '(PATH SPOT STEP WELL BBQ FLOOD MASONRY HANGING LEDGE UNDERWATER))

 

to this:

 

(IF (SETQ LIGHTS (list PATH SPOT STEP WELL BBQ FLOOD MASONRY HANGING LEDGE UNDERWATER))

 

The "quoted list" approach, with the apostrophe at the start, takes everything inside the list literally, and not as something to be evaluated or read as a variable.

 

[I haven't figured out why that's inside an (if) function -- will it ever return nil?]

Kent Cooper, AIA
Message 9 of 44
BlackBox_
in reply to: mid-awe


@mid-awe wrote:
Completely off topic, but (C:LIGHT-MODEL "SPOT") is perfectly acceptable input to my function. I don't build functions with a C: prefix usually; this one is just testing for now until it is working then I'll drop the C: from the name.

No, it's not (off topic)... While one can call the command within parens supplying the necessary argument, once cannot properly call it by name via command line, menu macro, etc., hence the comment made (which was specific to the latter).

 

Working code is working code; I'm not trying to come down on you, it's just that the way you've coded it is, I'll call it 'uncommon' coding practice, and can easily mislead others... Others who are trying to help you... So take from this what you like.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 10 of 44
BlackBox_
in reply to: mid-awe


@mid-awe wrote:
I'm not sure how to follow the advice given, since I need a double check. First the argument to the function which is the TYPE of light fixture; and then, the material for the light fixture. But, I need the Light TYPE, Light Material, and the Model Number all returned.

I'm not sure what part is confusing to you, you'll have to be more specific.

 

Your original code supplies a symbol as first element, and another symbol as the second element to each list item... Not an association list (see the CONS function).

 

 



"How we think determines what we do, and what we do determines what we get."

Message 11 of 44
BlackBox_
in reply to: alanjt_


@alanjt_ wrote:

Never mind, I see you responded to youself. Stupid no delete option.


This forum is greatly lacking, as compared to others that you and I participate in.



"How we think determines what we do, and what we do determines what we get."

Message 12 of 44
BlackBox_
in reply to: Kent1Cooper


@Kent1Cooper wrote:

 

[I haven't figured out why that's inside an (if) function -- will it ever return nil?]


To the best o fmy knowledge... No, that test expression will never return Nil.



"How we think determines what we do, and what we do determines what we get."

Message 13 of 44
martti.halminen
in reply to: mid-awe

Some style points:

 

- Lisp is case-insensitive, but usually lowercase is easier to read. I tend to write program text in lowercase and data in uppercase.

- Using VL-POSITION to check for membership is rather confusing for the reader, unless he happens to know about the speed difference to MEMBER. In a program with a human interface and without deep loops, saving a few milliseconds is not worth the confusion.

- Using a global variable to return results is clumsy, a more lispy style would be returning the result as the value of the function call.

 

I'd do this somewhat differently, using nested association lists:

 

 

(defun light-model (lighttype / ltype types data)
  ;; Returns the light model code as its value, no side effects
  (setq ltype (read lighttype)) ;; string -> symbol
  (setq types
	'((PATH (GRANITE PR-4704-GT) (RUST PR-4704-R) (BLACK PR-4704-B) (COPPER PR-2165-CSN))
          (SPOT (GRANITE GR-2216-GT) (RUST GR-2216-R) (BLACK GR-2216-B) (COPPER GR-2116-CSN))
          (STEP (GRANITE SL-4246-GT) (RUST SL-4246-R) (BLACK SL-4246-B) (COPPER SL-2146-CSN))
          (WELL (BLACK GW-5240-B) (LED GW-5272-LED))
          (BBQ (STEEL FL-4017-N))
          (FLOOD (GRANITE GR-7218-GT) (RUST GR-7218-R) (BLACK GR-7218-B) (COPPER GR-2104-CSN))
          (MASONRY (GRANITE SL-4250-GT) (RUST SL-4250-R) (BLACK SL-4250-B))
          (HANGING (GRANITE DS-2237-GT) (RUST DS-2237-R) (BLACK DS-2237-B))
          (LEDGE (GRANITE SL-4260-GT) (RUST SL-4260-R) (BLACK SL-4260-B) (COPPER SL-4260-CSN))
          (UNDERWATER (BLACK UW-5217-B))))
  (setq data (cdr (assoc ltype types)))
  (cadr (assoc (ask-or-get-color data lighttype)
               data)))

(defun ask-or-get-color (data lighttype)
  ;; data is an association list. If only one choice, returns its key, otherwise prompts the user.
  (if (= (length data) 1)
      (caar data)
      (ask-for-lightcolor lighttype
                          (mapcar (function
                                   (lambda (x)(vl-symbol-name (car x))))
                                  data))))

(defun ask-for-lightcolor (lighttype colors / key)
  (initget 1 (make-prompt colors " "))
  (setq key  (GETKWORD (STRCAT "\n\t =>> " LIGHTTYPE " Light Color [" (make-prompt colors "/") "]: ")))
  (if key (read key))) ; string -> symbol
;; What to do with nil if user cancels?
      


(defun make-prompt (strings separator / res)
  (strcat (car strings)
          (if (null (cdr strings))
              ""
              (strcat separator (make-prompt (cdr strings)
                                             separator)))))
    

 --

Message 14 of 44
Kent1Cooper
in reply to: mid-awe

My take on this....

 

Any time I see a lot of repeated elements [in this case, the same colors spelled out over and over in different lists], I figure there ought to be a way to reduce the repetition.  Here's how I would do it:

 

(defun Light-Model
  (appl / colors path spot step well bbq flood masonry hanging ledge underwater str color)
  (setq
    colors '("Granite" "Rust" "Black" "Copper" "Led" "Steel")
    path '("PR-4704-GT" "PR-4704-R" "PR-4704-B" "PR-2165-CSN" nil nil)
    spot '("GR-2216-GT" "GR-2216-R" "GR-2216-B" "GR-2116-CSN" nil nil)
    step '("SL-4246-GT" "SL-4246-R" "SL-4246-B" "SL-2146-CSN" nil nil)
    well '(nil nil "GW-5240-B" nil "GW-5272-LED" nil)
    bbq '(nil nil nil nil nil "FL-4017-N")
    flood '("GR-7218-GT" "GR-7218-R" "GR-7218-B" "GR-2104-CSN" nil nil)
    masonry '("SL-4250-GT" "SL-4250-R" "SL-4250-B" nil nil nil)
    hanging '("DS-2237-GT" "DS-2237-R" "DS-2237-B" nil nil nil)
    ledge '("SL-4260-GT" "SL-4260-R" "SL-4260-B" "SL-4260-CSN" nil nil)
    underwater '(nil nil "UW-5217-B" nil nil nil)
    models (eval (read appl)); list of model numbers for this application
    str "" ; start with empty STRing for (initget) & colors prompt
  ); setq
  (foreach x models (if x (setq str (strcat str (nth (vl-position x models) colors) " "))))
  (initget 1 str)
  (setq color (getkword (strcat "\nColor [" (vl-string-trim " " str) "]: ")))
  (prompt (strcat (strcase appl) " " color " " (nth (vl-position color colors) models)))
  (princ)
); defun

The (foreach) builds a string, specific to the options for the particular application, that is used for both the (initget) restriction of choices and for the prompt for color selection.  Colors not available for a particular application are represented by nil in the application's list, so they are omitted from the string.  The (getkword) allows selection from among only those available colors.  It then finds the position of that color in the colors list, and goes to the corresponding position in the models list for the application, to get the model number.

 

Using initial-capital color names allows the User to select a color by typing only the initial letter.  I haven't loaded and tried other routines, but some at least look as though the User would need to spell out the entire color word, since it looks like they're left all in capitals.

 

Usage:

Command: (light-model "flood")

Color [Granite Rust Black Copper]: c
FLOOD Copper GR-2104-CSN

 

[EDIT:  It could easily enough be made to automatically report the only color and model number for those applications with only one.  As currently written, it does require the User to designate that color choice, even though it's the only one.]

Kent Cooper, AIA
Message 15 of 44
BlackBox_
in reply to: mid-awe

(vl-load-com)

(defun _Light-Model (lightType / _GetOptionString data types option lightColor)

  (defun _GetOptionString (types / color option)
    (foreach x types
      (foreach y (cdr (assoc x data))
        (if (not (vl-position (setq color (car y)) option))
          (setq option (cons color option))
        )
      )
    )
    (vl-string-right-trim
      "/"
      (apply
        'strcat
        (mapcar (function (lambda (x) (strcat x "/"))) option)
      )
    )
  )

  (setq data
         '(("BBQ" ("STEEL" . "FL-4017-N"))
           ("FLOOD"
            ("BLACK" . "GR-7218-B")
            ("COPPER" . "GR-2104-CSN")
            ("GRANITE" . "GR-7218-GT")
            ("RUST" . "GR-7218-R")
           )
           ("HANGING"
            ("BLACK" . "DS-2237-B")
            ("GRANITE" . "DS-2237-GT")
            ("RUST" . "DS-2237-R")
           )
           ("LEDGE"
            ("BLACK" . "SL-4260-B")
            ("COPPER" . "SL-4260-CSN")
            ("GRANITE" . "SL-4260-GT")
            ("RUST" . "SL-4260-R")
           )
           ("MASONRY"
            ("BLACK" . "SL-4250-B")
            ("GRANITE" . "SL-4250-GT")
            ("RUST" . "SL-4250-R")
           )
           ("PATH"
            ("BLACK" . "PR-4704-B")
            ("COPPER" . "PR-2165-CSN")
            ("GRANITE" . "PR-4704-GT")
            ("RUST" . "PR-4704-R")
           )
           ("SPOT"
            ("BLACK" . "GR-2216-B")
            ("COPPER" . "GR-2116-CSN")
            ("GRANITE" . "GR-2216-GT")
            ("RUST" . "GR-2216-R")
           )
           ("STEP"
            ("BLACK" . "SL-4246-B")
            ("COPPER" . "SL-2146-CSN")
            ("GRANITE" . "SL-4246-GT")
            ("RUST" . "SL-4246-R")
           )
           ("UNDERWATER" ("BLACK" . "UW-5217-B"))
           ("WELL" ("BLACK" . "GW-5240-B") ("LED" . "GW-5272-LED"))
          )
  )

  (cond ((vl-position
           (setq lightType (strcase lightType))
           (setq types '("FLOOD" "LEDGE" "PATH" "SPOT" "STEP"))
         )
         (setq option (_GetOptionString types))
        )
        ((vl-position lightType (setq types '("HANGING" "MASONRY")))
         (setq option (_GetOptionString types))
        )
        ((vl-position lightType (setq types '("WELL")))
         (setq option (_GetOptionString types))
        )
        ((= lightType "BBQ") (setq lightColor "STEEL"))
        ((= lightType "UNDERWATER") (setq lightColor "BLACK"))
  )
  (if
    (and (not lightColor)
         (vl-position
           lightType
           '("FLOOD" "LEDGE" "MASONRY" "HANGING" "PATH" "SPOT" "STEP")
         )
    )
     (progn
       (initget 1 "BLACK COPPER GRANITE RUST")
       (setq lightColor
              (getkword (strcat "\nEnter " lightType " light color ["
                                option "]: "
                               )
              )
       )
     )
  )
  (if lightColor
    (cons lightType (assoc lightColor (cdr (assoc lightType data))))
  )
)

 [Edit] - Sorted nested association lists, for alphabetical user prompt.



"How we think determines what we do, and what we do determines what we get."

Message 16 of 44
BlackBox_
in reply to: Kent1Cooper


@Kent1Cooper wrote:

 

Any time I see a lot of repeated elements [in this case, the same colors spelled out over and over in different lists], I figure there ought to be a way to reduce the repetition. 


So I take it you're not a fan of Autoloader's PackageContents.xml, or utilizing XML for data storage generally? 

 


@Kent1Cooper wrote:

 

 

;; <snip>
(setq colors '("Granite" "Rust" "Black" "Copper" "Led" "Steel") path '("PR-4704-GT" "PR-4704-R" "PR-4704-B" "PR-2165-CSN" nil nil) spot '("GR-2216-GT" "GR-2216-R" "GR-2216-B" "GR-2116-CSN" nil nil) step '("SL-4246-GT" "SL-4246-R" "SL-4246-B" "SL-2146-CSN" nil nil) well '(nil nil "GW-5240-B" nil "GW-5272-LED" nil) bbq '(nil nil nil nil nil "FL-4017-N") flood '("GR-7218-GT" "GR-7218-R" "GR-7218-B" "GR-2104-CSN" nil nil) masonry '("SL-4250-GT" "SL-4250-R" "SL-4250-B" nil nil nil) hanging '("DS-2237-GT" "DS-2237-R" "DS-2237-B" nil nil nil) ledge '("SL-4260-GT" "SL-4260-R" "SL-4260-B" "SL-4260-CSN" nil nil) underwater '(nil nil "UW-5217-B" nil nil nil) models (eval (read appl)); list of model numbers for this application str "" ; start with empty STRing for (initget) & colors prompt ); setq
;;<snip>

The OP is having trouble with (nested?) assiciation list(s), and you suggest splitting each element of a given association into a separate list cooresponding only by element position...?

 

 



"How we think determines what we do, and what we do determines what we get."

Message 17 of 44
BlackBox_
in reply to: BlackBox_

Bollocks... Ran out of time to edit my post. *kicks dirt*

 

A few things were missing in the original code found in OP, and just now observed in testing:

 

  • "LED" was missing from the Initget call, prior to user prompt.
  • "WELL" was missing from the lightColor user prompt.
  • Also, added a call to Reverse for prompt options construction.

 

 

So, here's a repost of the entire, working code for a few minor code edits for completeness (apologies😞

 

(vl-load-com)

(defun _Light-Model (lightType / _GetOptionString data types option lightColor)

  (defun _GetOptionString (types / color option)
    (foreach x types
      (foreach y (cdr (assoc x data))
        (if (not (vl-position (setq color (car y)) option))
          (setq option (cons color option))
        )
      )
    )
    (vl-string-right-trim
      "/"
      (apply
        'strcat
        (mapcar (function (lambda (x) (strcat x "/"))) (reverse option))
      )
    )
  )

  (setq data
         '(("BBQ" ("STEEL" . "FL-4017-N"))
           ("FLOOD"
            ("BLACK" . "GR-7218-B")
            ("COPPER" . "GR-2104-CSN")
            ("GRANITE" . "GR-7218-GT")
            ("RUST" . "GR-7218-R")
           )
           ("HANGING"
            ("BLACK" . "DS-2237-B")
            ("GRANITE" . "DS-2237-GT")
            ("RUST" . "DS-2237-R")
           )
           ("LEDGE"
            ("BLACK" . "SL-4260-B")
            ("COPPER" . "SL-4260-CSN")
            ("GRANITE" . "SL-4260-GT")
            ("RUST" . "SL-4260-R")
           )
           ("MASONRY"
            ("BLACK" . "SL-4250-B")
            ("GRANITE" . "SL-4250-GT")
            ("RUST" . "SL-4250-R")
           )
           ("PATH"
            ("BLACK" . "PR-4704-B")
            ("COPPER" . "PR-2165-CSN")
            ("GRANITE" . "PR-4704-GT")
            ("RUST" . "PR-4704-R")
           )
           ("SPOT"
            ("BLACK" . "GR-2216-B")
            ("COPPER" . "GR-2116-CSN")
            ("GRANITE" . "GR-2216-GT")
            ("RUST" . "GR-2216-R")
           )
           ("STEP"
            ("BLACK" . "SL-4246-B")
            ("COPPER" . "SL-2146-CSN")
            ("GRANITE" . "SL-4246-GT")
            ("RUST" . "SL-4246-R")
           )
           ("UNDERWATER" ("BLACK" . "UW-5217-B"))
           ("WELL" ("BLACK" . "GW-5240-B") ("LED" . "GW-5272-LED"))
          )
  )

  (cond ((vl-position
           (setq lightType (strcase lightType))
           (setq types '("FLOOD" "LEDGE" "PATH" "SPOT" "STEP"))
         )
         (setq option (_GetOptionString types))
        )
        ((vl-position lightType (setq types '("HANGING" "MASONRY")))
         (setq option (_GetOptionString types))
        )
        ((vl-position lightType (setq types '("WELL")))
         (setq option (_GetOptionString types))
        )
        ((= lightType "BBQ") (setq lightColor "STEEL"))
        ((= lightType "UNDERWATER") (setq lightColor "BLACK"))
  )
  (if
    (and (not lightColor)
         (vl-position
           lightType
           '("FLOOD" "HANGING" "LEDGE" "MASONRY" "PATH" "SPOT" "STEP" "WELL")
         )
    )
     (progn
       (initget 1 "BLACK COPPER GRANITE LED RUST")
       (setq lightColor
              (getkword (strcat "\nEnter " lightType " light color ["
                                option "]: "
                               )
              )
       )
     )
  )
  (if lightColor
    (cons lightType (assoc lightColor (cdr (assoc lightType data))))
  )
)


"How we think determines what we do, and what we do determines what we get."

Message 18 of 44
alanjt_
in reply to: BlackBox_


@BlackBox_ wrote:

@Kent1Cooper wrote:

 

Any time I see a lot of repeated elements [in this case, the same colors spelled out over and over in different lists], I figure there ought to be a way to reduce the repetition. 


So I take it you're not a fan of Autoloader's PackageContents.xml, or utilizing XML for data storage generally? 

 


@Kent1Cooper wrote:

 

 

;; <snip>
(setq colors '("Granite" "Rust" "Black" "Copper" "Led" "Steel") path '("PR-4704-GT" "PR-4704-R" "PR-4704-B" "PR-2165-CSN" nil nil) spot '("GR-2216-GT" "GR-2216-R" "GR-2216-B" "GR-2116-CSN" nil nil) step '("SL-4246-GT" "SL-4246-R" "SL-4246-B" "SL-2146-CSN" nil nil) well '(nil nil "GW-5240-B" nil "GW-5272-LED" nil) bbq '(nil nil nil nil nil "FL-4017-N") flood '("GR-7218-GT" "GR-7218-R" "GR-7218-B" "GR-2104-CSN" nil nil) masonry '("SL-4250-GT" "SL-4250-R" "SL-4250-B" nil nil nil) hanging '("DS-2237-GT" "DS-2237-R" "DS-2237-B" nil nil nil) ledge '("SL-4260-GT" "SL-4260-R" "SL-4260-B" "SL-4260-CSN" nil nil) underwater '(nil nil "UW-5217-B" nil nil nil) models (eval (read appl)); list of model numbers for this application str "" ; start with empty STRing for (initget) & colors prompt ); setq
;;<snip>

The OP is having trouble with (nested?) assiciation list(s), and you suggest splitting each element of a given association into a separate list cooresponding only by element position...?

 

 


I finally had a reason to use the kudos button.

Message 19 of 44
mid-awe
in reply to: Kent1Cooper

I gave it a try. Changing from the quoted list did not help. Whatever I did incorrectly was not so simple.

Thank you, I really had hoped to discover what exactly cause my original code to fail (better learning that way)

Thanks again.
Message 20 of 44
mid-awe
in reply to: BlackBox_

I don't wish to sound ungrateful, because I am very thankful for all of the help here. By off topic I intended to convey that I found the whole "'C:' prefix cannot accept arguments" thing to have nothing to do with the code failing. But, it never hurts to refresh your memory. I seriously only use the C: on a function as I did when I'm testing, I only overlooked it before uploading. It was unintentional that anyone other than me would ever see that.

Thank you for being an educator. 🙂

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost