Comma separated String values from vector

208 views
Skip to first unread message

sunil...@gmail.com

unread,
Jul 16, 2013, 9:53:34 AM7/16/13
to clo...@googlegroups.com
Hi All,

I'm new to Clojure - 

I'm defining a vector containing string values. The requirement for me is to retrieve the String values separated by comma from the input vector.

Example: 

=> (def my-strings ["one" "two" "three"])

;; My expected output should be ;; "one", "two", "three" 

I tried interpose and join as below

=> (apply str (interpose "," my-strings))

=> (clojure.string/join "," my-strings)

both returning as "one,two,three" but I need each string surrounded by double quotes "" like in my example example.

/Sunil.



 

Cedric Greevey

unread,
Jul 16, 2013, 10:17:37 AM7/16/13
to clo...@googlegroups.com
(apply str "\"" (interpose "\", \"" my-strings) "\"") might work...


--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Maximilian Karasz

unread,
Jul 16, 2013, 10:21:53 AM7/16/13
to clo...@googlegroups.com
hi,

i might be wrong but it seems you're looking for something like https://github.com/clojure/data.csv

cheers,

-Max
--

Jay Fields

unread,
Jul 16, 2013, 10:39:14 AM7/16/13
to clo...@googlegroups.com
this seems to do what you want: (clojure.string/join ", " (map pr-str my-strings))

Jonathan Fischer Friberg

unread,
Jul 16, 2013, 10:40:17 AM7/16/13
to clo...@googlegroups.com
Jay beat me to it. :)

I'll add the documentation for pr-str:

Jonathan

Softaddicts

unread,
Jul 16, 2013, 10:40:36 AM7/16/13
to clo...@googlegroups.com
(apply str (interpose "," (map #(str "\"" % "\"") [...])))

Not the most efficient way but short.

Luc P.


> Hi All,
>
> I'm new to Clojure -
>
> I'm defining a vector containing string values. The requirement for me is
> to retrieve the String values separated by comma from the input vector.
>
> Example:
>
> => (def my-strings ["one" "two" "three"])
>
> ;; My expected output should be ;; *"one", "two", "three"*
>
> I tried interpose and join as below
>
> => (apply str (interpose "," my-strings))
>
> => (clojure.string/join "," my-strings)
>
> both returning as "one,two,three" but I need each string surrounded by
> double quotes "" like in my example example.
>
> /Sunil.
>
>
>
>
>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>
--
Softaddicts<lprefo...@softaddicts.ca> sent by ibisMail from my ipad!

Softaddicts

unread,
Jul 16, 2013, 10:49:31 AM7/16/13
to clo...@googlegroups.com
Pr-str is very slow compared to string concats....
Luc P.


> Jay beat me to it. :)
>
> I'll add the documentation for pr-str:
> http://clojuredocs.org/clojure_core/clojure.core/pr-str
>
> Jonathan
>
>
> On Tue, Jul 16, 2013 at 4:39 PM, Jay Fields <j...@jayfields.com> wrote:
>
> > this seems to do what you want: (clojure.string/join ", " (map pr-str
> > my-strings))
> >
> >
> > On Tue, Jul 16, 2013 at 10:17 AM, Cedric Greevey <cgre...@gmail.com>wrote:
> >
> >> (apply str "\"" (interpose "\", \"" my-strings) "\"") might work...
> >>
> >>
> >> On Tue, Jul 16, 2013 at 9:53 AM, <sunil...@gmail.com> wrote:
> >>
> >>> Hi All,
> >>>
> >>> I'm new to Clojure -
> >>>
> >>> I'm defining a vector containing string values. The requirement for me
> >>> is to retrieve the String values separated by comma from the input vector.
> >>>
> >>> Example:
> >>>
> >>> => (def my-strings ["one" "two" "three"])
> >>>
> >>> ;; My expected output should be ;; *"one", "two", "three"*

Softaddicts

unread,
Jul 16, 2013, 10:52:03 AM7/16/13
to clo...@googlegroups.com
I assume here that the strings are already escaped. Which might not be true at all.
pr-sr is safer in this regard but 6 times slower.

Luc P.

sunil...@gmail.com

unread,
Jul 16, 2013, 11:31:00 AM7/16/13
to clo...@googlegroups.com
Thanks everyone for contributing.

=> (clojure.string/join "," (map #(str \" % \") my-strings)) works perfectly for my case and that will do for now. 

/Sunil.
Reply all
Reply to author
Forward
0 new messages