Initializing help system before first use

Read two tables and a set from a text data file


Type: Programming
Rating: 2 (easy-medium)
Description: Read two tables and a set from a free format data file (text file).
  • opening and closing a file
  • skipping comments and empty lines
  • using 'read' and 'readln'
  • using loops: forall, repeat-until, while
File(s): readfile.mos
Data file(s): datafile.dat


readfile.mos
(!*******************************************************
  * Mosel Example Problems                              *
  * ======================                              *
  *                                                     *
  * file readfile.mos                                   *
  * `````````````````                                   *
  * Example for the use of the Mosel language           *
  * (Read a data file)                                  *
  *                                                     *
  * (c) 2008 Fair Isaac Corporation                     *
  *     author: Y. Colombani, 2001                      *
  *******************************************************!)

model readfile

parameters
 F="Data/datafile.dat"
end-parameters

declarations
 t1,t2:integer
end-declarations

fopen(F,F_INPUT)            ! Open the file F for reading
write("Reading file `",F,"'...")

fskipline("#\n")            ! Skip lines starting with a '#' and empty lines
readln(t1,t2)               ! Read the size of the table on a line

declarations
 TF:array(1..t1,1..t2) of integer
 TD:array(string,string) of real
 S: set of string
 s1,s2:string
end-declarations

fskipline("#\n")            ! Skip lines starting with a '#' and empty lines
forall (i in 1..t1)
do
 forall (j in 1..t2)
  read(TF(i,j))             ! Read t2 values
 readln                     ! on each line
end-do

fskipline("#\n")            ! Skip lines starting with a '#' and empty lines
repeat
 readln(s1,'|',s2,'|',TD(s1,s2))    ! Read the indices and the value on a line
until getparam("nbread")<5  ! Until a line is not properly constructed

fskipline("#\n")            ! Skip lines starting with a '#' and empty lines
read(s1)                    ! Read a string
while (s1<>'.' and not iseof)
do                          ! While the string is not '.' and the end of file
                            ! is not reached
 S+={s1}                    ! Add the string to the set
 read(s1)                   ! Read another string
end-do

fclose(F_INPUT)
writeln(" Done.\n")

writeln("TF=",TF)
writeln("TD=",TD)
writeln("S =",S)
end-model


© 2001-2020 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.