tags:

views:

545

answers:

3

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  A: 
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"}
Brian Carper
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?
Hamza Yerlikaya
`_` 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.
Brian Carper
+2  A: 

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).

Jim Downing
+1  A: 

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.

Timothy Pratley