1

Working in AutoLISP. I am looking for a more elegant way to extract the second string in this list of lists...

(("FullName") ("Larry Paige"))

Currently using the following to extract desired data.

(setq _BAMOFO (nth 0 (nth 1 Result))) 

Thank you.

3 Answers 3

1

The most readable approach to me would be to be able to write:

(setq _BAMOFO (fullname result))

And you need to define a fullname accessor function. Then you do not really care about how it is implemented; nth is quite readable in my opinion.

(defun fullname (list)
  (nth 0 (nth 1 list)))

Or maybe you can find a more robust way to extract the name given the shape of your data, for example finding out the list that follows the list that contains "FullName", that's not entirely clear to me if that makes sense in your context.

Sign up to request clarification or add additional context in comments.

Comments

1

if all the sublists always contain just one string each, then it is also a good way to unwrap the list(Result) first, if you use it several times after.

(setq Result '(("FullName") ("Larry Paige"))) ; => (("FullName") ("Larry Paige"))
(setq unwrapped_list (mapcar 'car Result)) ; => ("FullName" "Larry Paige")
(setq _BAMOFO (nth 1 unwrapped_list)) ; => "Larry Paige"

Comments

0

Fully low-level way:

(car (car (cdr Result)))

Mixed way that is likely more readable, as it is closer to human intuition of what we want to get:

(car (nth 1 Result))

3 Comments

Does autolisp have the classic car/cdr combinations like caadr?
Answering my own question, I only saw cadr and caddr in the documentation. So (car (cadr Result)) could be another variation.
@Shawn yes it does, the usual combos up to four letters long. cadr would be even more idiomatic than nth 1, I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.