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?

演習 1 : データ入力ルーチンの変更

  1. validName 関数を記述します。

    
    public function validName(name As %String) As %String
    ' validate a name - just checks for 2 pieces using ","
    ' returns 0 for an invalid name and writes error message
    ' else returns formatted name
    if len(name, ",") = 2 then
        return formatName(name)
    else
        print "Enter Last,First" : println
        return 0
    end if
    end function
    
  2. formatName() 関数を記述します。

    
    public function formatName(name As %String) As %String
    ' change user's entry into proper name format
    ' SMITH,JOHN and smith,john -> Smith,John
        dim ln, fn
        ln = piece(name, ",", 1) : fn = piece(name, ",", 2)
        ln = ucase(left(ln, 1)) & lcase(mid(ln, 2, len(ln)))
        fn = ucase(left(fn, 1)) & lcase(mid(fn, 2, len(fn)))
        return ln & "," & fn
    end function
    
  3. validPhone 関数を記述します。電話番号の検証後、必要な場合は既定エリア・コードを追加します。

    
    public function validPhone(phone As %String) As %String
    ' validate a phone - just checks for 3 pieces using "-" and length
    ' returns 0 for an invalid phone and writes error message
    ' else returns unchanged phone with default area code added
        if (len(phone) = 8 and len(phone, "-") = 2) then
            phone = "617-" & phone ' add default area code
        end if
        
        if (len(phone) = 12 and len(phone, "-") = 3) then
            return phone
        else
            print "Enter ###-###-#### or ###-####" : println
            return 0
        end if
    end function
  4. validDOB 関数を記述します。無効データには、On Error Goto を使用する必要があります。未来の日付を許可しないようにします。

    
    public function validDOB(dob As %String) As %Integer
    ' validate a date of birth - a valid date before or equal to today
    ' returns 0 for invalid dates and writes error message
    ' else returns internal format for valid dates
    
    On Error Goto BadDate
        if DateDiff("d", dob, Date) < 0 then
            print "Enter a date in the past" : println
            return 0
        else
            return DateConvert(dob, vbToInternal)
        end if
    
    BadDate:
        print "Invalid date" : println
        return 0
    end function
  5. prompt サブルーチンを編集します。Do/Loop While 文で、新規の関数 “$$valid” を 3 つ使用し、データが正しく入力されるまでプロンプトを繰り返します。

    
    private sub prompt()
    ' subroutine for prompting
    
        do
            input "Name: ", name : println
            if (name = "") then exit sub
            name = validName(name)
        loop while name = 0
        do
            input "Phone (617): ", phone : println
            phone = validPhone(phone)
        loop while phone = 0
        do
            input "DOB: ", dob : println
            intdob = validDOB(dob)
        loop while intdob = 0
        println
        
    end sub
  6. display サブルーチンを編集します。外部形式にデータを変換するコードを追加します。

    
    public sub display()
    ' subroutine for displaying data
    
        println "Name:", space(15), name
        println "Phone:", space(14), phone
        println "DOB:", space(16), DateConvert(intdob, vbToExternal)
        println
    end sub
FeedbackOpens in a new tab