[Oz] college timetable
Kent Hoxsey
khoxsey at ix.netcom.com
Thu May 6 17:08:20 CEST 1999
Gary R Keen wrote:
...
> Please suggest how to use Mozart to convert from a text file into an Oz
> record term.
>
> Some sample code would be nice.
> Are their specific Oz functions that can used?
> Or does one need to write one's own functions or procedures to do this?
Well, a little of both. There are some very useful Oz functions,
particularly String.tokens, but at some point you must write your own
code to parse your input format.
Here's an example of how I handled a file of data, one record per line,
each value delimited by a single space. In my program, I populate S by
reading from a file:
local S Toks PairLs OutRec in
S = "2 4 6"
Toks = {String.tokens S & }
% Yields ["2" "4" "6"]
PairLs = {List.mapInd Toks fun {$ I A} I#{String.toInt A} end}
% Yields [1#2 2#4 3#6]
% You can change or remove the conversion function, too:
% PairLs = {List.mapInd Toks fun {$ I A} I#A end}
% Yields [1#"2" 2#"4" 3#"6"]
OutRec = {List.toRecord i PairLs}
% Yields i(2 4 6)
end
In my case, the input data were all integers, so I ran the String.toInt
on them. If you were parsing mixed data, you would probably want to
delay the type conversion. The main thrust of my parsing code is that it
puts the elements of a string into a record with numbered features, so
you can extract the features you're interested in from the record on
demand:
X = OutRec.2 % X == 4
You should read up on the 'Records, Tuples, and Lists' and 'Text'
chapters of the Oz Base Environment manual. They'll give you a lot of
ideas for ways to represent and manipulate your input data. If you have
more complicated (or less regular) data input formats, you might
need/want to use the regex contrib package. I don't have any experience
with it (haven't needed it) but it looks quite powerful.
Kent Hoxsey
-
Please send submissions to users at mozart-oz.org
and administriva mail to users-request at mozart-oz.org.
The Mozart Oz web site is at http://www.mozart-oz.org/.
More information about the mozart-users
mailing list