1

Below example dump it as a list, but I wish to dump it in property value pairs:

#+BEGIN_SRC elisp :results output
(defun dump-plist(obj level)
  (if (listp obj)
      (dolist (item obj)
        (dump-plist item (1+ level)))
    (progn
      (while (> level 1)
        (princ "  ")
        (setq level (1- level)))
      (princ obj)
      (princ "\n"))))

(defun test()
  (let* ((my-plist (list :a 1 :b 2 :c 3 :more (list 4 5 6))))
    (print my-plist)
    (dump-plist my-plist 0)
    ))

(test)
#+END_SRC

#+RESULTS:
#+begin_example

(:a 1 :b 2 :c 3 :more (4 5 6))
:a
1
:b
2
:c
3
:more
  4
  5
  6
#+end_example

expected output is:

:a 1
:b 2
:c 3
:more (4 5 6)
1
  • Either iterate or recurse over the list a pair of entries at a time, not a single entry at a time.
    – Drew
    Commented Aug 10, 2017 at 20:25

1 Answer 1

-1

Replace your code block with this:

#+BEGIN_SRC elisp :results output
(defun dump-plist(obj)
    (unless (null obj)
        (princ (format "%s %s\n" (car obj)  (cadr obj)))
        (dump-plist (cddr obj))))

(defun test()
    (let* ((my-plist (list :a 1 :b 2 :c 3 :more (list 4 5 6))))
        (print my-plist)
        (dump-plist my-plist)))

(test)
#+END_SRC

It goes recursively through your plist and prints two entries at once until the list is empty

This works because plist means paired list. This implicates that this list has always a equal number of entries.

Replacing princ with format saves some lines of code.
The second parameter level is not needed anymore and therefore omitted.

1
  • Officially plist means "property list", not "paired list". But yeah, it should always have an even number of entries regardless of what you call it.
    – npostavs
    Commented Aug 11, 2017 at 6:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.