I am trying to input data from a .txt file into a scheme structure. Each element is separated by a tab in the data file and each structure set is on a new line. I want to be able to read in the data from one line into a structure and make a list of each structure set in the file. Any suggestions?
6
votes
2 Answers
6
votes
Not really sure what structures you had in mind, but say you had a text file like the following:
--> cat blah.txt foo bar baz 1 2 3 4 5 aa bb cc dd ee
You could convert it directly into a list of lists in scheme using sed:
--> echo "(define mylist '("`sed -e 's/\(.*\)/(\1)/' blah.txt`"))" > foo.txt
which then produces the following file:
--> cat foo.txt (define mylist '((foo bar baz) (1 2 3 4 5) (aa bb cc dd ee)))
And now all you have to do is load the file into scheme:
(load "foo.txt")
And you can access the structure via the `mylist' variable.
3
votes
Sounds like a CSV file with tabs instead of commas. If you're using PLT Scheme (DrScheme/mzscheme) neil's csv library is probably what you want.
Here is how to load it remotely:
(require (planet neil/csv:1:2/csv))
At least, that's what the instructions say. On my slightly oodate DrScheme, this is what worked:
(require (planet "csv.ss" ("neil" "csv.plt" 1 (= 1))))