0

I'm new with clojure, and i want extracting the data (only the integer numbers) in txt file for a differents processing.

in the first time i want to extract the integer data which are on the txt file : ({score 1} {:score 2} {:score 3} {:score 4} {:score 5} {:score 6} {:score 7} {:score 8} {:score 9} {:score 10} {:score 11} {:score 12} {:score 13} {:score 14} {:score 11} {:score 12} {:score 13} {:score 14} {:score 15} {:score 16} {:score 17} {:score 18} {:score 19} {:score 20} {:score 21} {:score 22} {:score 23} {:score 24} {:score 11} {:score 2} {:score 3} {:score 5} {:score 8} {:score 4} {:score 5} {:score 2} {:score 2} {:score 8} {:score 4} {:score 7} {:score 5} {:score 2} {:score 5} {:score 5} {:score 2} {:score 6} {:score 2} {:score 5} {:score 2} {:score 3} {:score 5} {:score 2} {:score 3} {:score 5} {:score 23} {:score 5} {:score 7} {:score 7} {:score 7} {:score 7} {:score 7} {:score 7} {:score 7} {:score 7} {:score 7} {:score 7} {:score 7} {:score 7} {:score 7} {:score 7} {:score 0} {:score 0} {:score 0} {:score 0} {:score 0} {:score 0} {:score 0} {:score 0}

after this i want to extract a data on another file txt for apply the the differents opération between all of data .

the second txt file is like :

   `  <  name 1>: <45>   <name 2 >: <78>     <  name 3>: <100>  <name 4 >: <8> `
ELM
  • 3
  • 2

2 Answers2

1

if I am correct you need data like :

(1 2 3 4 5 6 7 8 9 10 11 12 13 14 11 12 13 14 15 16 17 18 19 20 21 22 23 24 11)

if so then your solution is right here

(map :score (read-string(slurp "file-location")))
piyushmandovra
  • 4,219
  • 3
  • 19
  • 30
  • thank you for your response , exactly it's what i nedd .. but when i try to apply it in another format i have an error , the format is like : `< name > : < 25 > < name > : < 30 > < name > : < 5 > < name > : < 15 >` i'm using this syntax : (map :< (read-string(slurp "file-location"))) can you tell me where is a probleme in my function ? – ELM Mar 25 '15 at 10:05
  • please provide me exact data so i can help you..? – piyushmandovra Mar 25 '15 at 10:10
  • that is mean i can't read a string on another format , like my second file txt ? – ELM Mar 25 '15 at 10:32
  • and if i'm right , can'i covert my txt file to a edn file ? i'm sorry for these a lots of questions by i need to resolve a probleme today – ELM Mar 25 '15 at 11:39
  • dude check here http://conj.io/ all clojure higher order function here. hope it will help, if i will come up with soln will update. – piyushmandovra Mar 25 '15 at 11:48
0

Here's a solution for your second problem (which is really unrelated to your first which was nicely formated as EDN values).

For this sample (foo.txt):

<foo 1>: <45> <bar 2>: <78> <baz 3>: <100> <qux 4>: <8>

The following creates a map of the names to values:

(defn data-to-map [f]
  (reduce (fn [acc i] 
            (assoc acc (nth i 1) (nth i 2)))
          {} 
          (re-seq #"<([^>]+)>: <([^>]+)>" (slurp f))))

It uses a regex to break up your pattern into <some name>: <some value> and puts them in a map using the name as key.

Note, in your question you place the colon right next to the close bracket, if there are spaces (as in your comments to @piyushmandovra) then you'll have to adjust the regex to suit with additional \s whitespace values.

Example repl output:

user> (data-to-map "foo.txt")
{"qux 4" "8", "baz 3" "100", "bar 2" "78", "foo 1" "45"}

If you only want values, and not names from your second sample (not sure why, but that's what you originally asked for)

(defn data-vals-to-array [f] 
  (reduce (fn [acc i] 
            (conj acc (nth i 2)))
          []
          (re-seq #"<([^>]+)>: <([^>]+)>" (slurp f))))

with example

user> (data-vals-to-array "foo.txt")
["45" "78" "100" "8"]

In this case, it discards the name and just conjs the data into the vector.

You could replace the reducing function to simply #(conj %1 (last %2)) but I kept it similar to the map example for clarity.

I've kept everything as strings for simplicity. If your data is good enough, parse the integers using this SO answer

Community
  • 1
  • 1
Mark Fisher
  • 9,838
  • 3
  • 32
  • 38
  • Thank you very much for your ansewers , these is all what i'm checked !!! Tahnks – ELM Mar 26 '15 at 01:34