Jump to content

Change variables from numrical code to corresponding phrase


Recommended Posts

Posted

When retrieving information from a drawing into lisp I am given one of five didgets each represents a material.

 

1 for lead
2 = zinc
3 = ceramic
4 = concrete
5 = synthetic

 

 

The values are stored in a variable, which is written to excel.

 

Rather then have an excel file work things out how can I reference/ index the numbers with there corresponding material using lisp?

 

So rather then have an excel with:

 

1
1
2
1
3
1
4
2

 

I have an excel file with:

 

Lead
Lead
Zinc
Lead
Ceramic
Lead
Concrete
Zinc

Posted

Associative lists are really good for this type of data

(setq matdata '(
("LEAD" . 1)
("ZINC" . 2)
("CERAMIC" . 3)
("CONCRETE" . 4)
("SYNTHETIC" . 5)
))

and then:

(cdr (assoc value matdata))

 

They are case sensitive however. Or it could be reversed depending on your needs. -David

Posted

Use a cond statement prior to writing the value to the excel file.

 

Example:

 

;; Get variable value
(setq var 1)

;; Interpret value prior to write
(cond
 ((= 1 var)
  (setq var "Lead"))
 ((= 2 var)
  (setq var "Zinc"))
 ((= 3 var)
  (setq var "Ceramic"))
 ((= 4 var)
  (setq var "Concrete"))
 ;; ...etc.
 )

;; Write variable to excel

Posted
Associative lists are really good for this type of data

(setq matdata '(
("LEAD" . 1)
("ZINC" . 2)
("CERAMIC" . 3)
("CONCRETE" . 4)
("SYNTHETIC" . 5)
))

and then:

(cdr (assoc value matdata))

 

They are case sensitive however. Or it could be reversed depending on your needs. -David

 

 

Perhaps this format would be preferable?

 

(setq matdata '((1 . "LEAD")
               (2 . "ZINC")
               (3 . "CERAMIC")
               (4 . "CONCRETE")
               (5 . "SYNTHETIC")
               ))

Posted
Perhaps this format would be preferable?

 

 

I couldn't tell from the OP which way they needed the data.....

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...