Originally published in the 3000 NewsWire
Sometimes at Robelle we tend to forget how quickly we implement new features and move on to the next enhancement in our products. For example, in year 2000, with Suprtool's high speed extract and new and more powerful machines, our focus was to eliminate the number of passes that you need to do in order to get your data in the format you want. One result of that effort was expanded string handling to Suprtool, but some of you may have only just heard about this feature now. With the prospect of migrating data off of the 3000 we see these features as being very helpful for some migrations.
These changes are so extensive that string expressions are described separately for the Extract and If Commands. See the sections below for specific details and examples of string expressions.
EXTRACT target-field = expression
Examples
>extract id-no = warehouse-no + bin-no >extract full-name = first-name + last-name
Incorrect
>extract name = " " + product-desc Error: Missing comma or invalid arithmetic expression
Correct
>extract name = (" " + product-desc)
String constants are created with the exact length of the constant. For example, the string "abc" is three characters long and the string "a" is one.
When assigning the string expression to the target field, Suprtool pads the final string value with spaces to fill out the target field. String expressions longer than the target field generate an error.
>in testfile >def a,1,10,byte >ext a="I'm too long for this container" Error: String is too long for the specified item
>extract new-field = $trim(a + b + c)
>extract city-up = $upper(city) >extract full-name = $upper(first + last)
>extract city-lower-case = $lower(city) >extract city-state = $lower(city + state)
$Trim: Remove leading and trailing spaces from the string expression.
$Ltrim: Remove leading spaces.
$Rtrim: Remove trailing spaces.
You can combine byte-type fields together and use the built-in string functions to create string expressions. String expressions involve the + operator and any of the built-in string functions, which are $lower, $upper, $trim, $ltrim and $rtrim.
String expressions involving the + operator or the $lower, $upper, $trim, $ltrim and $rtrim built-in functions are done using variable-length strings. Suprtool keeps track of the length of every string, and all operations are done using the actual string length. For fields, the length of the string is the length of the field. If you do not want to retain all the spaces in a field, use one of the built-in trimming functions.
When creating string expressions, string constants are created with the exact length of the constant. For example, the string "abc" is three characters long and the string "a" is one.
>define short, 1,10 {10-character field} >define long ,11,15 {15-character field} >if short = longIn this example, Suprtool compares the 10 bytes in the short field with the first 10 bytes of the long field, but ignores the last five bytes of the long field. If the expression on either side of the equal sign consisted of more than one field (using the + operator) or involved any of the built-in string functions, such as $lower, $upper, $trim, $ltrim and $rtrim, Suprtool would have compared both sides of the equal sign by padding the shorter field with spaces. It is only the case where you are directly comparing one byte-type field to another that Suprtool uses the length of the shortest field for the comparison.
$Trim: Remove leading and trailing spaces from the string expression.
$Ltrim: Remove leading spaces.
$Rtrim: Remove trailing spaces.
Because Suprtool pads shorter strings with spaces when doing comparisons, trimming spaces is most useful when creating a combined string with several fields. For example, you might want to combine a person's first and last name (including a space between the two):
>if $trim(first) + " " + $trim(last) = "Joe Smith"
>if $upper(city) = "VANCOUVER" >if $lower(city) = "edmonton"Note that if you use the $upper or $lower functions, Suprtool does not shift any constants in the comparison. You must explicitly specify the constants in the correct case or you can use $upper or $lower with the constant:
>if $upper(city) = $upper("vancouver")Use the $upper or $lower functions for caseless pattern matching. As with other comparison operators, you must specify constants in the correct case when doing pattern matching:
>if $upper(city) == "VAN@" >if $lower(city) == "ed@"You can also use $upper and $lower with string expressions that combine many fields and string functions as shown in the following example:
>if $read - $upper($trim(first) + - " " + - $trim(last)) - = "JOE SMITH" -Say for example you wanted to find all of those customers in New York City, but your data has combinations of New York, NEW YORK and New YOrk. You can find all of these records by doing the following:
>if $upper(city) = "NEW YORK"
The sample file with address information:
>form testaddr File: TESTADDR.GROUP.ACCT (SD Version B.00.00) Entry: Offset NAME X30 1 STREET X20 31 CITY X20 51 PROV-STATE X2 71 COUNTRY X20 73 Limit: 10000 EOF: 15 Entry Length: 92 Blocking: 44You can now combine fields and constants with the spaces trimmed with one single extract command, as follows:
>in testaddr >def address3,1,40 >ext name,street >ext address3 = $trim(city) + "," + prov-state + " " + $trim(country) >num 1 >list >xeqThe result is a neatly formatted address line ready for printing on labels.
>IN TESTADDR.NEIL.GREEN (0) >OUT $NULL (0) NAME = Neil Armstrong STREET = 123 54th St West ADDRESS3 = New York,NY U.S.ANote that if the output had been sent to a self-describing file, the field "address3" would have contained the result of the expression. This technique can be used in a range of applications, including date reformatting, firstname and lastname concatenation (with intervening blank) for generating form letters, etc, all in a single task.