Re: Print key-value pairs from both plist and alist

Jean Louis <[email protected]>
Newsgroups gmane.emacs.help
Organization GNU Support
Message-ID <[email protected]>
On 2026-04-10 17:43, Heime wrote:
> I want to print key-value pairs using a function to which I can
> pass either a plist or an alist as argument.
> 
> Have seen different constructs to do so, but is there a clause
> that will apply for both a plist and an alist?

(defun print-key-value-pairs (data)
   "Print key-value pairs from alist or plist."
   (if (and (listp data) (consp (car data)))
       ;; Alist
       (progn
         (princ "\nAlist:\n")
         (dolist (pair data)
           (princ (format "  %S: %S\n" (car pair) (cdr pair)))))
     ;; Plist
     (progn
       (princ "\nPlist:\n")
       (let ((rest data))
         (while rest
           (princ (format "  %S: %S\n" (car rest) (cadr rest)))
           (setq rest (cddr rest)))))))

(print-key-value-pairs '(:name "John" :age 30 :city "Boston"))

Plist:
   :name: "John"
   :age: 30
   :city: "Boston"
nil

(print-key-value-pairs '((:name . "John") (:age . 30) (:city . 
"Boston")))

Alist:
   :name: "John"
   :age: 30
   :city: "Boston"


-- 
Jean Louis
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.