Skip to main content
Skip table of contents

Built-in functions

This table lists the built-in language functions for day to day data processing tasks.

The functions expect sane arguments and will result in runtime error or bogus result if supplied with invalid data.

System functions

Name

Description

Usage Example

ABORT(x)

Aborts script execution by triggering runtime error, with message x.

A=5;B=0;
IF(B==0, ABORT("Cannot divide by zero"), A/B)

COUNT(x)

Count array/list elements of x.

A=(1,2,1,3);
COUNT(A) # evals to 4

INDEXES(x)

Returns a list of x array indexes.
Useful to iterate over arrays with string-based keys.

B[“APPLES”] = 1; B[“BANANS”] = 2;
IND = INDEXES(B)
IND[2] # evals to “BANANAS”

DEFUN(x, y, z)

Defines a new named function.

Read more in section Defining your own

Read more in section Defining your own

HASH(x)

Calculates SHA256 message digest (known also as hash) of the scalar value provided in x.

CODE
a="TEST MESSAGE";
b = hash(a);
substr(b,0,6);    # evals to 59B954

LAMBDA(x, y)

Defines a new in-place function, not named.

Read more in section Defining your own

Read more in section Defining your own

LEN(x)

Returns the length (number of characters) of x when interpreted as a scalar value.

If x is a list/table, the length of the first element will be returned.

A='123'; LEN(A) # evaluates to 3
A=123; LEN(A) # evaluates to 3
LEN(123) # evaluates to 3 as well
A=(123456789,2,3,4);
LEN(a) & "," & COUNT(A);
# evaluates to 9,4
# where 9 is lenght of '123456789'
# and 4 is count of A list elements

LOCALS(x, y)

Controls variables' lexical scope.

Read more in section Defining your own

Read more in section Defining your own

QSORT(x, y)

Applies Quicksort algorithm and user defined comparison function y to sort a list or table, returns a sorted copy of x.

B=(7,3,5);
DEFUN("CMP",("A","B"),A<B);
A = QSORT(B, "CMP");
A[1] & A[2] & A\3 # returns 357

STL(x)

Scalar to list - takes a scalar value of the variable and separates it into single characters, returning a list of them.

COUNT(STL(“ABC”)) # evals to 3
STL(“ABC”)[2] # evals to B
# Unicode is respected
STL("ałć")[2] # ł

UNDEF(x)

Un-defines a function named x.

UNDEF(“UNDEF”);UNDEF(“A”)
# error - function not found

UNSET(x)

Removes array/list element by provided access definition in x.

A=(1,2,3); UNSET(A[1]); COUNT(A)
# evals to 2

WITH(x, y)

Controls variables' lexical scope.

Read more in section Defining your own

Read more in section Defining your own

Text processing functions

Name

Description

Usage Example

BACKWARDS(x)

Puts x characters in reverse order.

BACKWARDS(“ABC”) # evals “CBA”

CREGEX(x,y[,z])

Runs a full regular expression matching on y using string x as the regular expression.

Returns a complex table with matches and subgroups, offsets, indexes and values.

Optionally parameter z may be provided, to control the regular expression engine behaviour.

Control params

  • U - not greedy matching + and *

  • G - global (find all matches)

For further information, read here: More details on Regular Expressions processing

Greedy

V=CREGEX(
"([^ABC]+)",
"ABCDEFABCDEFG", "G");

# V[1]["SUB"][1]["STR"] $== "DEF"
# V[2]["SUB"][1]["STR"] $== "DEFG"

Not greedy

V=CREGEX(
"([^ABC]+)",
"ABCDEFABCDEFG", "UG");

# V[1]["SUB"][1]["STR"] $== "D"
# V[2]["SUB"][1]["STR"] $== "E"
# …
# V[7]["SUB"][1]["STR"] $== “G”
# COUNT(V) == 7

CXML_PARSE(x, y)

Parses XML text in x into a tree structure that allows manipulation and browsing.

Parameter y represents optional control parameters. Provide S for strict operation - throws an error for invalid input.

CODE
xml='<?xml version="1.0"?>
     <order number="4711"
      xmlns:demo="http://www.sap.com/abapdemos">
      <demo:head>
      <demo:status>confirmed</demo:status>
      <demo:date format="mm-dd-yyyy">07-19-2012</demo:date>
      </demo:head>
      <demo:body>     
 <demo:item units="2" price="17.00">Part No. 0110</demo:item>
      <demo:item units="1" price="10.50">Part No. 1609</demo:item>
      <demo:item units="5" price="12.30">Part No. 1710</demo:item>
      </demo:body>
     </order>';

p = cxml_parse(xml);
r = cxml_render(p);
cxml_parse(r) EQL p  # true

CXML_RENDER(x, y)

Converts a tree structure, as created by CXML_PARSE (or manually, if conforming) to XML representation.

Parameter y represents optional control parameters. None supported in this version.

CXPATH(x,y)

Runs XPath matching on XML string y using string x as the XPath expression. Returns a list of matching nodes.

CODE
<codedb><cl id="3">
'<enumeration value="AAA"/><enumeration value="AAB"/>
'<enumeration value="AAC">AAC value</enumeration>
'<enumeration value="AAD"/><enumeration value="AAE"/>
</cl></codedb>

XP='//cl[@id=3]/enumeration[@value="AAC"]'
R=CXPATH(XP,XML);
# R[1] == “AAC value"

FIND(x,y)

Finds position of first occurence of x in y , zero based. Returns -1 if not found.

FIND("A","BACA") # evals to 1

JSON_PARSE(x, y)

Parses JSON text in x into a tree structure that allows manipulation and browsing.

Parameter y represents optional control parameters. Provide S for strict operation - throws an error for invalid input.

Similar to CXML_PARSE / CXML_RENDER example above.

JSON_RENDER(x, y)

Converts a tree structure, as created by JSON_PARSE (or manually, if conforming) to JSON representation.

Parameter y represents optional control parameters. None supported in this version.

LEFT(x,y)

Gets y leftmost characters from x.

LEFT(“abc”,2) # evals “ab”

LOWER(x)

Converts x to lower case.

LOWER(“Abc”) # evals “abc”

REPLACE(x, y, z)

Replaces all occurrences of x in z with y.

REPLACE("ABC","DEF","ABCDEF")
# “DEFDEF”

RFIND(x, y)

Finds position of first occurence of x in y , zero based. Returns -1 if not found. Uses POSIX-based regular expression matching.

RFIND("[cde]+","BAcA") # evals to 2

RIGHT(x,y)

Gets y rightmost characters from x.

RIGHT(“abc”,2) # evals “bc”

RREPLACE(x, y, z)

Replaces all occurrences of x in z with y. Uses POSIX-based regular expression matching.

RREPLACE("[ABC]","DEF","ABCDEF")
# “DEFDEFDEFDEF”

SUBSTR(x,y,z)

Gets z characters from x, after skipping first y characters.

SUBSTR("abc",1,2) # evals "bc"

UCHAR(x)

Returns a character based on provided 32 bit Unicode character number x
Unicode Map.com - Public Map of Unicode Characters.

UCHAR(65) # evals to A

UPPER(x)

Converts x to upper case.

UPPER(“Abc”) # evals “ABC”

Mathematical functions

Name

Description

Usage Example

ABS(x)

Absolute value of the number x.

ABS(-3) # evals to 3

CEIL(x)

Returns closest integer not smaller than x.

CEIL(3.1) # evals to 4

FLOOR(x)

Returns closest integer
not larger than x.

FLOOR(3.7) # evals to 3

LOG(x,y)

Calculates the logarithm in base x of the number y.

LOG(10, 1000)
# evals to approx. 3 (2.999…)

POWER(x,y)

Calculates the y-th power of x.

POWER(2,3) # evals to 8

RANDOM(x,y)

Draws a decimal number between [x, y).

never less than x, surely less than y

x = RANDOM(5,10) # 5<=x<10

ROUND(x, y)

Rounds the number x, leaving up to ydecimal numbers.

ROUND(3.5, 0) # evals to 4
ROUND(3.14, 1) # evals to 3.1

SIGN(x)

Returns number sign (1 or -1) of x.

SIGN(-3) # evals to -1

SQRT(x)

Calculates the square root of x.

SQRT(25) # evals to 5

TRUNC(x)

Truncates the decimal value of x.

TRUNC(3.14) # evals to 3

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.