LAST()

LAST()

Top  Previous  Next

 

The LAST() function returns the final element of a string delimited by a specified character. The LASTS() function is similar to LAST() but operates on a multivalued string, returning a similarly structured dynamic array of results.

 

 

Format

 

LAST(string, delimiter)

 

where

 

stringis the string from which the final substring is to be extracted.

 

delimiterevaluates to the delimiter character.

 

 

Multivalue applications frequently need to extract the final element of a delimited string. Traditionally, this is done using the FIELD() and DCOUNT() functions:

ITEM = FIELD(STR, '*', DCOUNT(STR, '*'))

The LAST() function in QM allows this to be done in a simpler and more efficient manner.

 

 

The COL1() and COL2() functions can be used to find the character positions of the extracted substring, returning the positions of the character immediately before and after the extracted item respectively. The COL2() value will always be one greater than the string length except when processing a null string in which case both COL1() and COL2() return zero. When using the LASTS() function, the values returned by the COL1() and COL2() functions relate to the final extracted item.

 

Use of the $NOCASE.STRINGS compiler directive makes the delimiter case insensitive.

 

 

Examples

 

A = "1*2*3*4*5"

S = LAST(A, '*')

 

This program fragment assigns the string "5" to variable S.

 

 

A = "1*2*3*4*5" : @VM : "A*B*C"

S = LASTS(A, '*')

 

This program fragment assigns the string "5VMC" to variable S.

 

 

See also:

COL1(), COL2(), FIELD(), FIELDSTORE()