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
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
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
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
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
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
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
>
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
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
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:
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
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
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
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
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
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,
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
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
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
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}
>
>
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
21 matches
Mail list logo