Skip to main content

This is documentation for Caché & Ensemble. See the InterSystems IRIS version of this content.Opens in a new tab

For information on migrating to InterSystems IRISOpens in a new tab, see Why Migrate to InterSystems IRIS?

演習 3 : データ入力ルーチンの更新

  1. スタジオを開始し、mydatent ルーチンを開きます。

  2. ユーザ定義関数 $$validName を入力します。

    validName(name) PUBLIC ; validate a Name
        /* returns 0 for an invalid name and writes error message
           returns the unchanged name otherwise */
        {
        if (name?1u.l1","1u.l) {
            quit name }
        else {
            write !,"Last,First"
            quit 0
           }
        }
  3. ユーザ定義関数 $$validPhone を入力します。電話番号の検証後、必要な場合は既定のエリア・コードを追加します。

    validPhone(phone) PUBLIC ; validate a phone number
        /* returns 0 for invalid phone numbers and writes error message
           returns the valid phone number with default area code added 
           if necessary */
        {
        if (phone?.1(3n1"-")3n1"-"4n) {
            set:(phone?3n1"-"4n) phone = "617-" _ phone ; add default area code
            quit phone
           }
        else {
            write !, "###-###-#### or ###-####"
            quit 0
           }
        }
  4. ユーザ定義関数 $$validDOB を入力します。未来の日付を許可しないようにします。

    validDOB(date) PUBLIC ; validate a Date of Birth
        /* returns 0 for invalid dates and writes error message
           returns internal format for valid dates */
        {
        set convdate = $zdateh( date, 5,,,,,,, -1)
        if (convdate = -1) {
            write !,"Date in the past"
            quit 0 ; invalid date
           }
        elseif (convdate > $piece( $horolog, ",", 1)) {
            write !,"Date in the past"
            quit 0 ; invalid because it's in the future
           }
        else {
            quit convdate ; valid date
           }
        }
  5. prompt プロシージャを編集します。Do/While 文で、新規のユーザ定義関数 “$$valid” を 3 つ使用し、データが正しく入力されるまでプロンプトを繰り返します。

    prompt() [name, phone, intdob]
      ; procedure for prompting
      {
        do {
            read !, "Name: ", name
            quit:(name = "")  ; user entered nothing
            set name = $$validName( name )
        }
        while name = 0
        quit:(name = "")  ; exit procedure
    
        do {
            read !, "Phone (617): ", phone
            set phone = $$validPhone( phone )
        }
        while phone = 0
    
        do {
            read !, "DOB: ", dob
            set intdob = $$validDOB( dob )
        }
        while intdob = 0
        write !!
      }
  6. display プロシージャを編集します。希望の外部形式でデータの変換コードを追加します。

    display() [name, phone, intdob]
        ; display the data
        {
        write !, "Name:", ?20, name
        write !, "Phone:", ?20, phone
        write !, "DOB:", ?20, $zdate( intdob, 2)
        write !!
        }
FeedbackOpens in a new tab