Initializing help system before first use

Working with records


Type: Programming
Rating: 3
Description:
  • defining records and arrays of records
  • accessing record fields
  • assigning values to record fields
  • initialization with data from file
  • defining records of records (recorddef2.mos)
  • selecting record fields in initializations (recorddef2.mos)
File(s): recorddef.mos, recorddef2.mos
Data file(s): recorddef.dat, recorddef2.dat


recorddef.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file recorddef.mos 
   `````````````````` 
   Defining and initializing records.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Nov. 2006, rev. Dec. 2017
*******************************************************!)

model "Defining records"

 declarations 
  R = 1..10
  D: record
   name: string
   values: array(R) of real
  end-record
 end-declarations

 declarations 
  mydata = record                   ! Define a named record
   name: string
   values: array(R) of real
  end-record
  M: mydata
  A: dynamic array(T:range) of mydata
 end-declarations
 
 D.name:= "D"
 forall(i in R) D.values(i):= i
 writeln("Values of ", D.name, ": ", D.values)

 initializations from "recorddef.dat"
  A
 end-initializations

 writeln("An entry of A: ", A(1))
 writeln("'name' of an entry of A: ", A(4).name)
 writeln("'values' of an entry of A: ", A(3).values)
 writeln("First entry of 'values' of 'A4': ", A(4).values(1))

 forall(i in T | exists(A(i))) writeln(A(i).name)

end-model 

recorddef2.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file recorddef2.mos 
   ``````````````````` 
   Defining and initializing records.
   - Records within records,
     selection of fields in initializations -
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Sep. 2007
*******************************************************!)

model "Defining records"

 declarations 
  R = 1..10

  mydata = record                      ! Define a named record
   name: string
   values: array(R) of real
  end-record

  B: dynamic array(T:range) of record  ! Define an unnamed record
   val: real
   rd: mydata                          ! Record field containing a record
  end-record
 end-declarations
 
 initializations from "recorddef2.dat"
  B(rd) as "B"
  B(val) as "C"
  B(rd(name)) as "D"
  B(rd(values)) as "E"
  B(rd(values)) as "F"
 end-initializations

 writeln(B)

end-model