Re: Creating map from string

2012-03-19 Thread Ben Smith-Mannschott
On Sun, Mar 18, 2012 at 21:04, Jimmy wrote: > Hi, > > I would like to generate a hashmap from a string. The key portions of > the string  will have some a prefix such as @ to define that they are > a key. So the following string > > "@key1  this is a value  @another-key  and another value @test1 a

Re: Creating map from string

2012-03-19 Thread Jimmy
Ok, those sugggestions work great. Thanks for the help. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first

Re: Creating map from string

2012-03-18 Thread gaz jones
argh i come back to paste in my answer and you beat me to it :( i was gonna say: (let [s ""@key1 this is a value @another-key and another value @test1 and other value""] (reduce (fn [m [_ k v]] (assoc m k (string/trim v))) {} (re-seq #"(@[\w-]+)([^@]*)" s))) much the same... On Sun, Mar 18

Re: Creating map from string

2012-03-18 Thread Moritz Ulrich
On Sun, Mar 18, 2012 at 21:14, David Powell wrote: > (into {} >   (map (fn [[_ x y]] [(keyword x) (clojure.string/trim y)]) >     (re-seq #"(@[^ ]*) *([^@]*)" s))) I think `for' is cleaner than map + anonymous function: (into {} (for [[_ k v] (re-seq #"(@[^ ]*) *([^@]*)" s)] [(keyw

Re: Creating map from string

2012-03-18 Thread David Powell
You could use a regexp to pick out the key and the value - something like this: (into {} (map (fn [[_ x y]] [(keyword x) (clojure.string/trim y)]) (re-seq #"(@[^ ]*) *([^@]*)" s))) -- Dave -- You received this message because you are subscribed to the Google Groups "Clojure" group. To po

Creating map from string

2012-03-18 Thread Jimmy
Hi, I would like to generate a hashmap from a string. The key portions of the string will have some a prefix such as @ to define that they are a key. So the following string "@key1 this is a value @another-key and another value @test1 and other value" would get converted to. { :@key1 "this

Re: into map with vectors versus lists (Re: Creating map from string)

2010-12-24 Thread Emeka
Did you try this (apply hash-map (partition 2 (split (slurp "data") #","))) On Sat, Dec 4, 2010 at 11:12 AM, Remco van 't Veer wrote: > I expected this to work: > > (into {} (partition 2 (split (slurp "data") #","))) > > But unfortunately, `into' doesn't seem to allow pushing lists of pairs >

Re: Creating map from string

2010-12-07 Thread Anclj
Thanks a lot for all the answers :) I've been busy trying to understand all the scripts that you posted. The code works but I also wanted to know why. I'm new to Clojure and it's hard for me to understand "advanced code", but looking through the docs and the api I'm learning a lot. Cheers! On 4

Re: Creating map from string

2010-12-04 Thread Tyler Perkins
Or, better yet, (into {} (->> (str "{" (slurp "data") "}") read-string (map (fn [[k v]] [(keyword (str k)) v] -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send emai

Re: Creating map from string

2010-12-04 Thread Tyler Perkins
On Dec 4, 12:21 pm, Tim Robinson wrote: > Using 'apply hash-map' doesn't handle the transformations. > Note the original post requested keywords for keys and integers for > vals in the output. I'm not the only one who overlooked that! :o) Might just as well use strings as keys. But OK, try this:

Re: Creating map from string

2010-12-04 Thread Tim Robinson
Using 'apply hash-map' doesn't handle the transformations. Note the original post requested keywords for keys and integers for vals in the output. On Dec 4, 11:49 am, Tyler Perkins wrote: > How about just > > (apply hash-map (split (slurp "data") #",")) -- You received this message because you

Re: Creating map from string

2010-12-04 Thread Tyler Perkins
How about just (apply hash-map (split (slurp "data") #",")) -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your fi

into map with vectors versus lists (Re: Creating map from string)

2010-12-04 Thread Remco van 't Veer
I expected this to work: (into {} (partition 2 (split (slurp "data") #","))) But unfortunately, `into' doesn't seem to allow pushing lists of pairs into a map. But vectors are allowed: (into {} (map vec (partition 2 (split (slurp "data") #"," Can somebody explain why vectors are allowe

Re: Creating map from string

2010-12-03 Thread Laurent PETIT
2010/12/3 Ken Wesson > Object creation wasn't my concern -- modern JVMs are very efficient in > that regard -- just traversals. > Ken, I've done my "homework", and of course you were right, and me wrong. At some point in my reasoning I've conflated number of object allocations with number of tr

Re: Creating map from string

2010-12-03 Thread Ken Wesson
Object creation wasn't my concern -- modern JVMs are very efficient in that regard -- just traversals. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are m

Re: Creating map from string

2010-12-03 Thread Laurent PETIT
2010/12/3 Ken Wesson > On Fri, Dec 3, 2010 at 10:08 AM, Laurent PETIT > wrote: > > 2010/12/3 Laurent PETIT > >> 2010/12/3 Anclj > >>> I've got: > >>> > >>> user> (split (slurp "data") #",") > >>> ["0" "2" "1" "5" "2" "8" "3" "15" "4" "9"] > >>> > >>> And I would like: > >>> {:0 2, :1 5, :2 8,

Re: Creating map from string

2010-12-03 Thread Ken Wesson
On Fri, Dec 3, 2010 at 10:08 AM, Laurent PETIT wrote: > 2010/12/3 Laurent PETIT >> 2010/12/3 Anclj >>> I've got: >>> >>> user> (split (slurp "data") #",") >>> ["0" "2" "1" "5" "2" "8" "3" "15" "4" "9"] >>> >>> And I would like: >>> {:0 2, :1 5, :2 8, :3 15, :4 9} >>> >>> Any idea? >> >> >> (let

Re: Creating map from string

2010-12-03 Thread Laurent PETIT
2010/12/3 Laurent PETIT > Hi, > > 2010/12/3 Anclj > > Hi, >> >> I have a string of data and I would like to get a map {:key >> value, :key value, …} >> >> How could I do that? >> >> I've got: >> >> user> (split (slurp "data") #",") >> ["0" "2" "1" "5" "2" "8" "3" "15" "4" "9"] >> >> And I would

Re: Creating map from string

2010-12-03 Thread Sunil S Nandihalli
Here is what I came up with ... (let [d (split (slurp "data") #",")] (->> d (apply hash-map) (clojure.walk/keywordize-keys) (clojure.contrib.generic.functor/fmap read-string))) Sunil. On Fri, Dec 3, 2010 at 7:52 PM, Anclj wrote: > Hi, > > I have a string of

Re: Creating map from string

2010-12-03 Thread Laurent PETIT
Hi, 2010/12/3 Anclj > Hi, > > I have a string of data and I would like to get a map {:key > value, :key value, …} > > How could I do that? > > I've got: > > user> (split (slurp "data") #",") > ["0" "2" "1" "5" "2" "8" "3" "15" "4" "9"] > > And I would like: > {:0 2, :1 5, :2 8, :3 15, :4 9} > >

Creating map from string

2010-12-03 Thread Anclj
Hi, I have a string of data and I would like to get a map {:key value, :key value, …} How could I do that? I've got: user> (split (slurp "data") #",") ["0" "2" "1" "5" "2" "8" "3" "15" "4" "9"] And I would like: {:0 2, :1 5, :2 8, :3 15, :4 9} Any idea? Thanks. -- You received this message