4

I have the following string

layout: default
title: Envy Labs

What i am trying to do is create map from it

layout->default 
title->"envy labs"

Is this possible to do using sequence functions or do i have to loop through each line?

Trying to get a regex to work with and failing using.


(apply hash-map (re-split #": " meta-info))

3 Answers 3

4
user> (let [x "layout: default\ntitle: Envy Labs"]
        (reduce (fn [h [_ k v]] (assoc h k v))
                {}
                (re-seq #"([^:]+): (.+)(\n|$)" x)))
{"title" "Envy Labs", "layout" "default"}
Sign up to request clarification or add additional context in comments.

2 Comments

thx that worked, but i have one questions, (fn [h [_ k v]] what does _ mean do you skip everything but the last two params? or is it just a unused var name?
_ is a normal variable name. It's just a convention to say to human readers that it's a placeholder and you don't plan to use that variable.
3

The _ is a variable name used to indicate that you don't care about the value of the variable (in this case, the whole matched string).

Comments

1

I'd recommend using clojure-contrib/duck-streams/read-lines to process the lines then split the fields from there. I find this method is usually more robust to errors in the file.

Comments

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.