Slurping structs from file fails with whitespace string attributes

6 views
Skip to first unread message

Perttu

unread,
May 28, 2009, 4:23:15 PM5/28/09
to Clojure
Hi.

I'm a newbie so bare with me. I have just started playing with Clojure
and the first thing I thought I'd try is storing and retrieving a seq
of structs, like in Suart Halloway's example here:

http://github.com/stuarthalloway/practical-cl-clojure/blob/1c2f138bda5e73ebbbafbc2cea7e5cd14ec335c4/pcl/chap_03/chap_03.clj

My spit/slurp of a hash of structs works fine with, if I use struct
instances without spaces in the attribute strings like the following:

(struct customer "Apple" "InfiniteLoop")

But if I use this:

(struct customer "Apple" "Infinite Loop 1")

I get an error:

Exception in thread "main" clojure.lang.LispReader$ReaderException:
java.lang.ArrayIndexOutOfBoundsException: 7 (test-storing.clj:19)
at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:2719)
at clojure.lang.Compiler$DefExpr.eval(Compiler.java:298)
at clojure.lang.Compiler.eval(Compiler.java:4537)
at clojure.lang.Compiler.load(Compiler.java:4857)
at clojure.lang.Compiler.loadFile(Compiler.java:4824)
at clojure.main$load_script__5833.invoke(main.clj:206)
at clojure.main$init_opt__5836.invoke(main.clj:211)
at clojure.main$initialize__5846.invoke(main.clj:239)
at clojure.main$null_opt__5868.invoke(main.clj:264)
at clojure.main$legacy_script__5883.invoke(main.clj:295)
at clojure.lang.Var.invoke(Var.java:346)
at clojure.main.legacy_script(main.java:34)
at clojure.lang.Script.main(Script.java:20)
Caused by: clojure.lang.LispReader$ReaderException:
java.lang.ArrayIndexOutOfBoundsException: 7
at clojure.lang.LispReader.read(LispReader.java:180)
at clojure.core$read__4168.invoke(core.clj:2083)
at clojure.core$read__4168.invoke(core.clj:2081)
at clojure.core$read__4168.invoke(core.clj:2079)
at clojure.core$read__4168.invoke(core.clj:2077)
at chap_03$load_db__54.invoke(chap_03.clj:71)
at clojure.lang.AFn.applyToHelper(AFn.java:173)
at clojure.lang.AFn.applyTo(AFn.java:164)
at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:2714)
... 12 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: 7
at clojure.lang.PersistentArrayMap$Seq.first(PersistentArrayMap.java:
216)
at clojure.lang.APersistentMap.hashCode(APersistentMap.java:101)
at clojure.lang.Util.hash(Util.java:55)
at clojure.lang.PersistentHashMap.entryAt(PersistentHashMap.java:134)
at clojure.lang.PersistentHashMap.containsKey(PersistentHashMap.java:
130)
at clojure.lang.APersistentSet.contains(APersistentSet.java:33)
at clojure.lang.PersistentHashSet.cons(PersistentHashSet.java:59)
at clojure.lang.PersistentHashSet.create(PersistentHashSet.java:34)
at clojure.lang.LispReader$SetReader.invoke(LispReader.java:974)
at clojure.lang.LispReader$DispatchReader.invoke(LispReader.java:540)
at clojure.lang.LispReader.read(LispReader.java:145)
... 20 more

I use a store-function like this:

(defn store-customer-db [customer-db filename]
(spit filename (with-out-str (print customer-db))))

And a read-function like this:

(defn load-db [filename]
(with-in-str (slurp filename)(read)))

From the output file of spit I can see that the print doesn't give
double quotes to the strings which seems to be a problem for slurp.
What would be the correct solution for this?

My Clojure version is 1.0, and the contrib is a few weeks old
snapshot.

Thanks,
Perttu


J. McConnell

unread,
May 29, 2009, 7:56:51 AM5/29/09
to clo...@googlegroups.com
On Thu, May 28, 2009 at 4:23 PM, Perttu <perttu...@gmail.com> wrote:

I use a store-function like this:

(defn store-customer-db [customer-db filename]
 (spit filename (with-out-str (print customer-db))))

I believe this is your problem. The print/println functions print in a format suitable for human consumption. That's why you don't see quotes around the strings. These are the functions you would normally use for, for example, echoing instructions to a console user.

I believe what you are looking for are the pr/prn functions (pr in particular). These are designed to print objects for reader consumption.

HTH,

- J.

Perttu

unread,
Jun 7, 2009, 5:34:31 AM6/7/09
to Clojure
Thanks!

Sorry about double-posting, the message took over 24 hours to appear
here.

-Perttu

On May 29, 2:56 pm, "J. McConnell" <jdo...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages