Thread Rating:
  • 517 Vote(s) - 2.7 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Page 8: Extra Practice
06-20-2011, 01:34 PM, (This post was last modified: 06-23-2011, 04:28 PM by ashimo.)
#1
Page 8: Extra Practice
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.


Messages In This Thread
Page 8: Extra Practice - by ashimo - 06-20-2011, 01:34 PM

Possibly Related Threads...
Thread Author Replies Views Last Post
  Page 7: Summary ashimo 0 3,046 06-20-2011, 01:23 PM
Last Post: ashimo
  Page 6: Structure of a Program that Reads our Score Data ashimo 0 2,970 06-20-2011, 01:21 PM
Last Post: ashimo
  Page 5: Application: Reading Our Score Data ashimo 0 2,775 06-20-2011, 01:20 PM
Last Post: ashimo
  Page 4: Structure of a Program that Writes Data to a File ashimo 0 3,148 06-20-2011, 01:18 PM
Last Post: ashimo
  Page 3: Basics 2: Writing a Control Value into a File ashimo 0 3,022 06-20-2011, 01:16 PM
Last Post: ashimo
  Page 2: Structure of a Program that Reads Data from a File ashimo 0 3,039 06-20-2011, 01:13 PM
Last Post: ashimo
  Page 1: Reading Our Score Data ashimo 0 3,171 06-20-2011, 01:12 PM
Last Post: ashimo
Forum Jump:


Users browsing this thread:
1 Guest(s)

MyBB SQL Error

MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
1017 - Can't find file: 'mybb_threadviews' (errno: 2)
Query:
INSERT INTO mybb_threadviews (tid) VALUES('78')