Skip to content

Instantly share code, notes, and snippets.

@joebo
Created June 17, 2015 19:28
Show Gist options
  • Save joebo/c0c56f41c610eb04370b to your computer and use it in GitHub Desktop.
Save joebo/c0c56f41c610eb04370b to your computer and use it in GitHub Desktop.
picat reading a file
% exmaple reading a file
import util.
main =>
writeTest,
readTest.
randBetween(Lower,Upper)=Num => Num=floor(frand()*(Upper-Lower+1)+Lower).
writeTest =>
Lines=1..5,
OutputFile = open("output.txt",write),
foreach(Line in Lines)
Text=[chr(randBetween(65,90)) : I in 1..randBetween(5,20)],
writef(OutputFile, "%d\t%s\n", Line,Text)
end,
close(OutputFile).
readTest =>
Lines = read_file_lines("output.txt"),
Arr = new_array(Lines.length,2),
foreach(I in 1..Lines.length)
Parts = Lines[I].split("\t"),
Arr[I,1] = parse_term(Parts[1]),
Arr[I,2] = Parts[2]
end,
write(Arr).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment