6

I am writing a piece of code that needs to read in a text file that has data. The text file is in the format:

name 1 4
name 2 4 5
name 3 1 9

I am trying to create a vector of a map in the form [:name Sarah :weight 1 cost :4].

When I try reading the file in with the line-seq reader, it reads each line as an item so the partition is not correct. See repl below:

(let [file-text (line-seq (reader "C://Drugs/myproject/src/myproject/data.txt"))
                       new-test-items (vec (map #(apply struct item %) (partition 3 file-text)))]
     (println file-text)   
     (println new-test-items))


(sarah 1 1 jason 4 5 nila 3 2  jonas 5 6 judy 8 15 denny 9 14 lis 2 2  )
[{:name sarah 1 1, :weight jason 4 5, :value nila 3 2 } {:name jonas 5 6, :weight judy 8 15, :value denny 9 14}]

I then tried to just take 1 partition, but still the structure is not right.

=> (let [file-text (line-seq (reader "C://Drugs/myproject/src/myproject/data.txt"))
                                new-test-items (vec (map #(apply struct item %) (partition 1 file-text)))]
              (println file-text)   
              (println new-test-items))
(sarah 1 1 jason 4 5 nila 3 2  jonas 5 6 judy 8 15 denny 9 14 lis 2 2  )
[{:name sarah 1 1, :weight nil, :value nil} {:name jason 4 5, :weight nil, :value nil} {:name nila 3 2 , :weight nil, :value nil} {:name jonas 5 6, :weight nil, :value nil} {:name judy 8 15, :weight nil, :value nil} {:name denny 9 14, :weight nil, :value nil} {:name lis 2 2, :weight nil, :value nil} {:name  , :weight nil, :value nil}]
nil

Next I tried to slurp the file, but that is worse:

=> (let [slurp-input (slurp "C://Drugs/myproject/src/myproject/data.txt")
            part-items (partition 3 slurp-input)
            mapping (vec (map #(apply struct item %) part-items))]
            (println slurp-input)
            (println part-items)
            (println mapping))
sarah 1 1
jason 4 5
nila 3 2 
jonas 5 6
judy 8 15
denny 9 14
lis 2 2

((s a r) (a h  ) (1   1) (

Please help! This seems like such an easy thing to do in Java, but is killing me in Clojure.

2 Answers 2

7

split it into a sequence of lines:

(line-seq (reader "/tmp/data"))

split each of them into a sequence of words

(map #(split % #" ") data)

make a function that takes a vector of one data and turns it into a map with the correct keys

(fn [[name weight cost]] 
   (hash-map :name name 
             :weight (Integer/parseInt weight) 
             :cost (Integer/parseInt cost))) 

then nest them back together

(map (fn [[name weight cost]] 
       (hash-map :name name 
                 :weight (Integer/parseInt weight) 
                 :cost (Integer/parseInt cost))) 
     (map #(split % #" ") (line-seq (reader "/tmp/data"))))

({:weight 1, :name "name", :cost 4} 
 {:weight 2, :name "name", :cost 4} 
 {:weight 3, :name "name", :cost 1})

you can also make this more compact by using zip-map

5
  • 1
    because this is lazy, it's up to you to remember to close the file when you are not going to read any more data from it. (I often find my self wrapping sequences in a close-when-empty lazy reader. Jul 15, 2012 at 19:24
  • split comes from clojure.string and reader comes from clojure.java.io Jul 15, 2012 at 19:25
  • Arthur thank you so much! I was lost on this. I tried your solution, but I get the following error.=> (map (fn [[name weight cost]] (hash-map :name name :weight (Integer/parseInt weight) :cost (Integer/parseInt cost))) (map #(split % #" ") (line-seq (reader "C://Drugs/myproject/src/myproject/data.txt")))) CompilerException java.lang.RuntimeException: Unable to resolve symbol: split in this context, compiling:(NO_SOURCE_PATH:5) Jul 15, 2012 at 19:25
  • see the comment above ^. add a (:use [clojure.string :only (split)]) to your namespace definition Jul 15, 2012 at 19:26
  • Awesome! I am just learning Clojure this weekend, so it all seems so complicated right now. I really appreciate the help. Jul 15, 2012 at 19:41
3

You are trying to do everything in one place without testing intermediate results. Instead Clojure recommends to decompose task into a number of subtasks - this makes code much more flexible and testable. Here's the code for your task (I assume records in file describe people):

(defn read-lines [filename]
  (with-open [rdr (clojure.java.io/reader filename)]
    (doall (line-seq rdr))))

(defn make-person [s]
  (reduce conj (map hash-map [:name :weight :value] (.split s " "))))

(map make-person (read-lines "/path/to/file"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.