Question

I am making a website and I want to know if there is any way to return a string of integers from a vector, each in their own line. When I rig my code to use apply str to the output I can get an something in the browser that goes from this -> [1 2 3 4] to this -> 1 2 3 4. But I want it to look like this:

1
2
3
4 
.
.
.

When I try to get each element on its own line using println, pprint or format, I get blank under the results header in the browser. I assume this is because all of those return nil. Is there some way to get the formatting I need, so that the results of my output can be easily copied and pasted into an excel file without the user having to format it by hand?

Was it helpful?

Solution 2

I figured it out! Since I am using hiccup it is possible to do this:

[:p (interpose [:br]
      (process-grades (clojure.edn/read-string weights) (clojure.edn/read-string grades)))]

and have each element from the output vector rendered on its own line in the browser.

OTHER TIPS

If you're explicitly looking to output csv-data that can be read by Excel, I would recommend looking at one of the CSV libraries like data.csv. These libraries will not only handle formatting data as strings but also the actually quite tricky rules in CSV around quoting.

Example that obtains a string based on some csv data:

user=> (def data [[1 2] [2 3] [3 4] [4 5]])
#'user/data
user=> (with-out-str (write-csv *out* data))
"1,2\n2,3\n3,4\n4,5\n"

If you want to constrain narrowly to the problem you list, one way (of many ways) to do this would be to interleave an infinite sequence of newlines between each piece of data:

user=> (def data [1 2 3 4])
#'user/data
user=> (apply str (interleave data (repeat \newline)))
"1\n2\n3\n4\n"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top