Curl Global Community
Page 8: Extra Practice - Printable Version

+- Curl Global Community (https://communities.curl.com)
+-- Forum: Tutorials (https://communities.curl.com/forumdisplay.php?fid=3)
+--- Forum: Public Training (https://communities.curl.com/forumdisplay.php?fid=4)
+---- Forum: Curl IDE Made Easy (https://communities.curl.com/forumdisplay.php?fid=6)
+----- Forum: Try 5: Reading Our Score Data (https://communities.curl.com/forumdisplay.php?fid=13)
+----- Thread: Page 8: Extra Practice (/showthread.php?tid=78)



Page 8: Extra Practice - ashimo - 06-20-2011

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.