How to get a property from a plist

emotionull

I am a newbie in Lisp. I want to access a particular property from a property list with a string variable like this

(setf sym (list :p1 1))
(setf x "p1")
(getf sym :x)
Joshua Taylor

About cl:getf

Let Petit Prince's answer is right that getf is probably the function you want to use here, but note that it can be used for more than just keyword symbols. You can use it for any objects. A property list is just a list of alternating indicators and values, and any object can be an indicator:

(let ((plist (list 'a 'b 'c 'd)))
  (getf plist 'c))
;=> D

You can even use strings as indicators:

(let* ((name "p1")
       (plist (list name 1)))
  (getf plist name))
;=> 1

However, that's probably not great practice, since getf compares indicators with eq. That means that using strings as indicators might not be reliable, depending on your use case:

(let ((plist (list "p1" 1)))
  (getf plist "p1"))
;=> NIL

For your example

In your case, you're trying to take a string and find the object for a symbol with a name that's string-equal (i.e., with the same characters, but disregarding case). It probably makes more sense to loop over the list and compare indicators with string-equal.

(let ((plist '(:p1 1 :p2 2)))
  (loop 
     for (indicator value) on plist by #'cddr
     when (string-equal indicator "p1")
     return value))
;=> 1

And of course, you can wrap that up in a function for abstraction:

(defun getf-string-equal (plist indicator)
  (loop
     for (i v) on plist by #'cddr
     when (string-equal i indicator)
     return v))

(getf-string-equal '(:p1 1 :p2 2) "p1")
;=> 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get a property from a plist

From Dev

How to get a property from another property in JS?

From Dev

How to get property from ofono

From Dev

How to parse NSData from Login Items property file (com.apple.loginitems.plist)

From Dev

how to delete from uitableview with plist?

From Dev

How to get translate property from a group?

From Dev

How to get property info from Powershell object?

From Dev

how to get to parent overridden property from child?

From Dev

How to get property name from expression

From Dev

How to get property from component when testing?

From Dev

How to get property from an array of object ?

From Dev

How to get value from property in BeanShell (jmeter)

From Dev

How to get Property value from a Javascript object

From Dev

How the get "Manager" property from ActiveDirectory with NodeJS?

From Dev

How to get annotations of a Kotlin property from Java?

From Dev

how to get to parent overridden property from child?

From Dev

How to get property name from expression

From Dev

How to get property information from a class

From Dev

How get response from resolve property in routeProvider?

From Dev

How to get property from an array of object ?

From Dev

How to get object with largest property from array?

From Dev

How to get property info from Powershell object?

From Dev

How to get a property from a List Android

From Dev

How to get enum value from property

From Dev

Get only certain Strings from plist iOS

From Dev

How to get an object property->property from a stdClass with a string representing it

From Dev

How to get property value from property-getter quotation

From Dev

how to get specific data from plist when an annotation is tap and show it to label

From Java

How do I get a plist as a Dictionary in Swift?

Related Related

  1. 1

    How to get a property from a plist

  2. 2

    How to get a property from another property in JS?

  3. 3

    How to get property from ofono

  4. 4

    How to parse NSData from Login Items property file (com.apple.loginitems.plist)

  5. 5

    how to delete from uitableview with plist?

  6. 6

    How to get translate property from a group?

  7. 7

    How to get property info from Powershell object?

  8. 8

    how to get to parent overridden property from child?

  9. 9

    How to get property name from expression

  10. 10

    How to get property from component when testing?

  11. 11

    How to get property from an array of object ?

  12. 12

    How to get value from property in BeanShell (jmeter)

  13. 13

    How to get Property value from a Javascript object

  14. 14

    How the get "Manager" property from ActiveDirectory with NodeJS?

  15. 15

    How to get annotations of a Kotlin property from Java?

  16. 16

    how to get to parent overridden property from child?

  17. 17

    How to get property name from expression

  18. 18

    How to get property information from a class

  19. 19

    How get response from resolve property in routeProvider?

  20. 20

    How to get property from an array of object ?

  21. 21

    How to get object with largest property from array?

  22. 22

    How to get property info from Powershell object?

  23. 23

    How to get a property from a List Android

  24. 24

    How to get enum value from property

  25. 25

    Get only certain Strings from plist iOS

  26. 26

    How to get an object property->property from a stdClass with a string representing it

  27. 27

    How to get property value from property-getter quotation

  28. 28

    how to get specific data from plist when an annotation is tap and show it to label

  29. 29

    How do I get a plist as a Dictionary in Swift?

HotTag

Archive