Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure:从文件中slurping结构失败,字符串属性包含空格_Clojure - Fatal编程技术网

Clojure:从文件中slurping结构失败,字符串属性包含空格

Clojure:从文件中slurping结构失败,字符串属性包含空格,clojure,Clojure,我刚刚开始玩Clojure,我想我要做的第一件事是存储和检索结构列表,就像Suart Halloway的例子一样 如果我使用属性字符串中没有空格的结构实例(如以下所示),那么我对结构哈希的吐出/吐出效果很好: (struct customer "Apple" "InfiniteLoop") 但如果我用这个: (struct customer "Apple" "Infinite Loop 1") 我得到一个错误: Exception in thread "main" clojure.lang.

我刚刚开始玩Clojure,我想我要做的第一件事是存储和检索结构列表,就像Suart Halloway的例子一样

如果我使用属性字符串中没有空格的结构实例(如以下所示),那么我对结构哈希的吐出/吐出效果很好:

(struct customer "Apple" "InfiniteLoop")
但如果我用这个:

(struct customer "Apple" "Infinite Loop 1")
我得到一个错误:

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
根据结构中字段的数量,我也可能只获取字符串的一部分作为属性名,而不是错误。例如:循环1

我使用的存储函数如下所示:

(defn store-customer-db [customer-db filename]
  (spit filename (with-out-str (print customer-db))))
(defn load-db [filename]
  (with-in-str (slurp filename)(read)))
以及如下的读取函数:

(defn store-customer-db [customer-db filename]
  (spit filename (with-out-str (print customer-db))))
(defn load-db [filename]
  (with-in-str (slurp filename)(read)))
从spit的输出文件中,我可以看到打印没有给字符串加双引号,这似乎是slurp的一个问题。正确的解决方案是什么


我的Clojure版本是1.0,contrib是几周前的快照。

print
println
是用于人类可读的输出。如果您想打印以后要再次读取的内容,请使用
pr
prn

user> (read-string (with-out-str (prn {"Apple" "Infinite Loop"})))
{"Apple" "Infinite Loop"}
鉴于:

user> (read-string (with-out-str (print {"Apple" "Infinite Loop"})))
java.lang.ArrayIndexOutOfBoundsException: 3 (NO_SOURCE_FILE:0)
它正在尝试运行以下代码:

(read-string "{Apple Infinite Loop}")
具有奇数个键/值的。请注意,各个散列键/值周围缺少引号。即使这种读取有效(即,如果您同时提供偶数个参数),它读取的也不是一个满是字符串的哈希映射,而是符号。因此,您将获得除输出内容之外的其他内容

user> (map class (keys (read-string (with-out-str (print {"foo bar" "baz quux"})))))
(clojure.lang.Symbol clojure.lang.Symbol)
例如:

(def hashed-hobbits {:bilbo "Takes after his Mother's family" :frodo "ring bearer"})
您只需要:

(spit "hobbitses.txt" hashed-hobbits)
要重新阅读:

(def there-and-back-again (read-string (slurp "hobbitses.txt")))

spit/slurp将其全部封装在一个字符串中,但在slurp上使用read string将该字符串解释回clojure代码/数据。也适用于巨魔数据结构

你很可能会在#clojure(irc)中得到权威而直接的答案。