Hi Greg, here's a sample but realistic pattern of the sort of thing
you're doing;
(import '(
java.io BufferedReader FileReader File IOException)
'(bqutils BQUtil))
(defn samp-loop
"Read a csv file containing user records."
[#^String fpath]
(with-open [r (BufferedReader. (FileReader. (File. fpath)))]
(let [; read line 1 for field names
line (.readLine r)
flds (seq (BQUtil/parseCsv line))
nflds (count flds)]
(try
(loop [line (.readLine r) i 0 ]
(when line
(let [ri (seq (BQUtil/parseCsv line))
_ (when (not (= (count ri) nflds))
(throw (IOException. (str "Bad line:"i))))
[uid lname inits] ri]
(println :lname lname :inits inits :uid uid)
(if (= "" uid)
(println "Breaking at line:" i)
(recur (.readLine r) (inc i) )))))
(catch Exception x (prn (.toString x)))
(finally (println "Done:" flds))))))
A few notes;
- BQUtil is home-grown java fn for parsing csv to ArrayLists.
Note the wrapping in seq and then destructuring to get the
field values for each line.
- The outer let (flds) is in scope in the (finally
- The (with-open has its own (finally
- You can throw within the loop - note the throw within
the let using a dummy _ placeholder
- Note the line counter
- Note the technique to "break" when uid = ""
(assuming that's what you wanted to do)
Hth, Adrian.
> --
> 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