1

If I have

(def a "((a,"a1",0.533,0.122,0.608,0.258) 
         (c,"c1",0.863,0.031,0.998,0.667) 
         (b,"b1",0.53,0.117,0.609,0.256))")

I would like to have

(def b '((a,"a1",0.533,0.122,0.608,0.258) 
         (c,"c1",0.863,0.031,0.998,0.667) 
         (b,"b1",0.53,0.117,0.609,0.256)))

where I can access each element in b with

(first b) ; which should return (a,"a1",0.533,0.122,0.608,0.258) 

and

(type (first (first b)) ; returns clojure.lang.Symbol
(type (second (first b)) ; returns java.lang.String
(type (last (first b)) ; returns java.lang.Double

I tried just putting (symbol a) but that seems to turn the entire string into a single symbol.

1 Answer 1

3

what you need is clojure.edn/read-string:

user> (clojure.edn/read-string
       "((a,\"a1\",0.533,0.122,0.608,0.258) (c,\"c1\",0.863,0.031,0.998,0.667)(b,\"b1\",0.53,0.117,0.609,0.256))")

;;=> ((a "a1" 0.533 0.122 0.608 0.258)
;;    (c "c1" 0.863 0.031 0.998 0.667)
;;    (b "b1" 0.53 0.117 0.609 0.256))

user> (def b (clojure.edn/read-string
               "((a,\"a1\",0.533,0.122,0.608,0.258) (c,\"c1\",0.863,0.031,0.998,0.667) (b,\"b1\",0.53,0.117,0.609,0.256))"))
#'user/b

user> (first b)
;;=> (a "a1" 0.533 0.122 0.608 0.258)

user> (type (first (first b)))
;;=> clojure.lang.Symbol

user> (type (last (first b)))
;;=> java.lang.Double

user> (type (second (first b)))
;;=> java.lang.String
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's exactly what I needed!

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.