Arrays in Sage X3 4GL

By | September 30, 2025

Arrays in Sage X3 4GL provide a powerful and efficient way to store and manage multiple data elements under a single variable. They help streamline your code, improve readability, and reduce complexity when handling collections of related data.

Sage X3 supports both single-dimensional and multi-dimensional arrays, with up to four dimensions allowed, giving developers flexibility to structure data as needed.

In this blog, we’ll focus specifically on single-dimensional arrays—covering how to declare them, assign values, check their size, and iterate through them using For loops.

New Stuff: Automating RFQ (Request for Quotation) Creation in Sage X3

Declaring Single-Dimensional Arrays

In Sage X3 4GL, there are two primary ways to declare a single-dimensional array:

  1. Using the Number of Elements

You can define an array by specifying the total number of elements it should hold. The syntax differs slightly based on the data type.

Examples:
Local Integer Num(5) # Integer array with 5 elements
Local Char String(50)(5) # Character array with 50 characters per element and 5 elements

Notes:
For Integer, simply specify the number of elements in parentheses after the variable name.
For Char, first define the character size (e.g., 50), followed by the number of elements.

  1. Using a Start/End Index Range

You can also declare an array by defining a custom start and end index. This provides more control over how elements are indexed.

Examples:
Local Integer Num(0..4) # Array from index 0 to 4 (5 elements total)
Local Char String(50)(1..5) # Array from index 1 to 5 (5 elements total)

Notes:
This method lets you define both the start and end index explicitly.
If you specify only the start index, Sage X3 automatically sets the end index to the maximum allowed (32,767).

Local Integer BigArray(1..) # Starts at index 1, ends at 32,767

Checking Array Size with dim() Function

In Sage X3 4GL, you can determine the total number of elements in an array using the built-in dim() function. This is especially useful when working with loops or when the array size isn’t hardcoded.
Syntax:
dim(array_variable)

Example:
Local Integer Numbers(0..3)
Infbox(num$(dim(Numbers))) # Outputs: 4

Accessing and Concatenating Values

You can iterate through the array using a For loop along with the dim() function to retrieve or process each element.

Example:
Local Char Word(50) : Word = “”

For I = 1 To dim(Letters)
Word += Letters(I)
Next

Result:
The final value of Word will be:
“ABCDE”

Tips:
Always ensure your loop starts at the same index as your array’s defined start index (e.g., 1 in this example).
If your array starts at index 0, subtract 1 from the loop’s upper limit to avoid an “index not found” error:

Example for array with index 0:
Local Char Codes(1)(0..4) # Array starts at index 0

Local Char Result(50) : Result = “”

For I = 0 To dim(Codes) – 1
Result += Codes(I)
Next

[about_us_blog_common]