
-----------------------------------
ericfourfour
Wed Jan 10, 2007 8:35 pm

Useful functions
-----------------------------------
I have decided to post a bunch of simple functions that are commonly used. Feel free to post more.

These can be used, for example, in a "yes or no" situation:
fcn getChoiceCustom (trueChoice, falseChoice,
        displayMessage, invalidInputMessage : string) : boolean

    var choice : string
    var trueChoiceLower : string := Str.Lower (trueChoice)
    var falseChoiceLower : string := Str.Lower (falseChoice)

    loop
        if displayMessage not= "" then
            put displayMessage
        end if

        get choice

        choice := Str.Lower (choice)

        exit when choice = trueChoiceLower or choice = falseChoiceLower

        if invalidInputMessage not= "" then
            put invalidInputMessage
        end if
    end loop
    result choice = trueChoiceLower
end getChoiceCustom

fcn getChoice (trueChoice, falseChoice : string) : boolean
    result getChoiceCustom (trueChoice, falseChoice,
        trueChoice + " or " + falseChoice + "?", "Invalid Input")
end getChoice

This one capitalizes a string:
fcn capitalize (text : string) : string
    if text not= "" then
        result Str.Upper (text (1)) + text (2 .. *)
    end if
    result ""
end capitalize"

This one swaps the case of a string:
fcn swapCase (text : string) : string

    var newText := ""
    
    for i : 1 .. length (text)
    
        var upperCase := Str.Upper (text (i))

        if upperCase = text (i) then
            newText += Str.Lower (text (i))
        else
            newText += upperCase
        end if
    end for

    result newText
end swapCase

Here are a few for getting integers from the keyboard:
fcn getIntCustom (displayMessage, invalidInputMessage : string) : int
    loop
        var numInput : string

        if displayMessage not= "" then
            put displayMessage
        end if

        get numInput

        if numInput >= "0" and numInput = low and num 