Curl Global Community

Full Version: Page 8: Extra Practice
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Extra Practice

Practice Problem 1: Basics

Let’s write a program that writes the data displayed by RecordGrid into a file. The program must satisfy the following requirements:
  • The location to which the file is saved is to be specified by the user, using the Save as dialog box.
  • The data displayed using RecordGrid will have three fields: name, age, and score.
  • The overwriting of data in an existing file shall be prohibited.


Hint: Use commas to delimit each of the fields of the data in each Record, and then write each Record to one line of the file.

Solution

Solution Program: c:\Curl\Try5\04_exercise1\start.curl

Code:
{value
    let out:#TextOutputStream
    let data:RecordSet = {RecordSet
                             {RecordFields
                                 {RecordField name, domain = String},
                                 {RecordField age, domain = int},
                                 {RecordField score, domain = int}
                             },
                             {RecordData name = Matt, age = 33, score = 88},
                             {RecordData name = Sarah, age = 27, score = 79},
                             {RecordData name = Jacob, age = 26, score = 90}
                         }
    
    let cb:CommandButton = {CommandButton
                               label = Save,
                               {on Action do
                                   {if-non-null loc = {choose-file style = save-as} then
                                       {try
                                           set out = {write-open
                                                         error-if-exists? = true,
                                                         loc
                                                     }
                                           {for r:Record in data do
                                               {out.write-one-string
                                                   {format "%s , %s, %s \r\n,
                                                       r[name], r[age], r[score]}
                                               }
                                           }
                                        catch e:IOException do
                                           {popup-message Error writing data"}
                                           {output e.message}
                                        finally
                                           {if-non-null out then
                                               {out.close}
                                           }
                                       }
                                   }
                               }
                           }
    {VBox
        {RecordGrid
            record-source = data,
            width = 10cm,
            height = 3cm
        },
        cb
    }
}

Explanation

RecordSets, like arrays, are collections, so a RecordSet can be passed to a for loop, which will process the contained Records in order.