The HotDocs Computation Archive
Get Extra Help

0090 - Determine Lettercase

Description:

How to determine if a string is upper or lowercase.


• Code •

Computation Case-c:

""
// Clear the dialog
ASK NONE
REPEAT SortByCase
   SET Shift-t TO UNANSWERED
   SET Marker-n TO UNANSWERED
END REPEAT

// Create values
SET Shift-t[1] TO "«TestStr-t:like this»"
SET Marker-n[1] TO 3
SET Shift-t[2] TO "«TestStr-t:LIKE THIS»"
SET Marker-n[2] TO 1
SET Shift-t[3] TO "«TestStr-t»"
SET Marker-n[3] TO 2

// Now let's sort it
REPEAT SortByCase
   ASCEND Shift-t
   ASCEND Marker-n
   RESULT + "«Marker-n»"
END REPEAT
ASK DEFAULT

// Lowercase
IF POSITION( RESULT, "2" ) = 1
   "L"

// Uppercase
ELSE IF POSITION( RESULT, "2" ) = 3
   "U"

// Non-letter
ELSE
   "M"
END IF
 
 
How to use it:

SET TestStr-t TO "some text value"
IF Case-c = "U"
   // it is uppercase
ELSE IF Case-c = "L"
   // it is lowercase
ELSE
   // it is mixed case
END IF

• Explanation •

There is no easy way to check for case in HotDocs. "A" = "a" will always be TRUE. This computation provides an ugly but effective way to check the case of a single character or a string. The computation returns one of three values: "U" if the string is uppercase, "L" if the string is lowercase, and "M" if the string is mixed case. The computation requires the following items:

Set the "Advanced" options for each variable to "Ask only in dialog," "Don't warn if unanswered," and "Don't save in answer file."

This gets a bit ugly, so stay close. The computation takes our TestStr-t and adds it to a repeated dialog, SortByCase, along with two other versions of the same string: one converted to all uppercase, and one converted to all lowercase. We then sort the contents of the dialog box. The result is a list of its contents sorted lowercase first. We can tell the case of our string by where it ends up in the ordered list.

The first step is to clear out the dialog SortByCase in case it has some straggling values in it. Then we add TestStr-t to the box in three different forms: all lowercase, all uppercase, and original case. These are stored in the text variable Shift-t, which is the "shifted" forms of the string. We can now REPEAT SortByCase and ASCEND Shift-t to sort the Shift-t items by case. But we are still left with one problem: determining where our item is in the list.

This is where the number variable Marker-n comes in. A Marker-n value is paired with each Shift-t value. This Marker-n value becomes sort of a bookmark we can use to track the sort order. In fact, notice that the output of the REPEAT SortByCase is a list of the markers rather than a list of the strings. Output from the REPEAT will look like 312, showing the order of the Shift-t items.

The Mark-n number of the original string is 2, and there is a good reason for that. This causes our item to always be sorted before the lowercase version (Mark-n value of 3) if it is lowercase, after the uppercase version (Mark-n value of 1) if it is uppercase, or in the middle if it is mixed case. The line ASCEND Mark-n takes care of this.

Here is an example using "a" as our TestStr-t. For the sake of clarity, we will show it with its Mark-n value of 2 tacked on. So our string is "a2." The lowercase version is "a3," and the uppercase version is "A1." Sorting the list first alphabetically produces "a3a2A1" (because a2 was added last). Then sorting by Mark-n produces "a2a3A1." The actual RESULT string is "231," showing that our string is in position 1, or lowercase. Same example again, but this time with a TestStr-t of "A": "a3" is the lowercase version, "A1" the uppercase version, and "A2" our original version. Sorting the list by both Shift-t and Mark-n produces "a3A1A2" (in reality "312"). Our string is in position 3, meaning that it is uppercase. And, of course, mixed case will place our string in the middle.

We can finally check RESULT to see what position "2" falls in. Position 1 means lowercase, position 2 mixed case, and position 3 uppercase. And that's all there is to it.

 

• Contributors •

Brian Albee   LegalCS