I want to parse and filter a file that looks like this:
@@1 Row one.
@@2 Row two.
I have been able to do the filtering of the rows with the following code:
(defn parse-text-cms [sel-row]
(let [f_data (st/split #"@@" (slurp "cms/tb_cms.txt"))]
;(prn (map #(take 1 %) f_data))))
(filter #(= (first (take 1 %)) sel-row) f_data)))
However, this codes gives me (if sel-row=1):
1 Row one.
I would like to chop off that 1 and the space after, so to have:
Row one.
I think there is some sequence magic to do this. I just can’t come up with an elegant solution.
I would define the function the following way:
The combination of
line-seqandreadergives me a sequence of lines from the input file.with-openensures that the file is properly closed when I’m done. I apply a regex to each line that looks for@@followed by a number and some spaces.re-findreturns a vector with three items:I bind these to
numberandlineusing destructuring in aforstatement (I’m not interested in the whole matched line, so I ignore that). I filter for the selectedsel-rowusing:whenand yield only the (rest of the)line.Since I only expect one match in the file, I return just the first item from the sequence built by
for. Because of the laziness offor,mapandline-seq, this also stops reading of the file after the item is found.If you do a lot of lookups for rows, I would suggest loading the whole file into memory instead of reading it every time, though.