When attempting to calculate the number of bytes of storage an array occupies, the %SIZE built-in function may be used. Include the second parameter *ALL to get the number of bytes of memory the entire array occupies.
But what about determining the length of the elements in an array? For example, an array of 10 elements, each being 9-position packed with zero decimals, is specified as follows:
Apply %size to the QTY array to calculate the number of bytes, as follows:
allBytes = %size(qty : *ALL);
This yields 5 and 50, respectively. A 9-digit packed field occupies five bytes of memory; therefore, each element has a size of 5 bytes, whereas all 10 elements have a combined size of 50 bytes.
When the declared length of an array element is different from its size (bytes occupied), then the %LEN built-in function can be used to retrieve the length of the element. For example:
This yields a length of 9. Unlike %SIZE, the %LEN built-in function does not support an *ALL parameter. Therefore, another approach must be taken. This is where %XFOOT comes in.
An anomaly of %XFOOT is that if it encloses a %LEN that is further enclosing an array, %XFOOT will add up the lengths of the elements in the array.
Here’s how to use %XFOOT to calculate total length of all array elements:
D totDigits S 10I 0
/free
totDigits = %XFoot(%len(qty));
// totDigits is equal to 90
/end-free
The %XFOOT built-in function magically adds up the lengths of each of the array elements for the array specified within the %LEN built-in function.
For arrays with character elements, the length and size care identical, unless the VARYING keyword is used. In this case, the technique can be used to calculate the current length of each of the variable-length elements in the array.
Here’s how to use %XFOOT with VARYING array elements:
D totLen S 10I 0
/free
names(1) = 'Bob';
names(2) = 'Bobby';
names(3) = 'Robert';
totLen = %XFoot(%len(names));
// Only elems 1 to 3 have data
// Therefore elems 4 to 20 are empty
// totLen is equal to 14
/end-free
Another option is to use the %SUBARR built-in function (added in OS/400 V5R3), which allows an array to be subscripted. This allows you to add up the lengths of just the desired elements in the array.
Here’s how to use %XFOOT with %SUBARR to sum up lengths of elements 1 to 3:
D totLen S 10I 0
/free
names(1) = 'Bob';
names(2) = 'Bobby';
names(3) = 'Robert';
totLen = %XFoot(%len(%subarr(names:1:3)));
// Only elems 1 to 3 are used
// totLen is equal to 14
/end-free
Bob Cozzi is a programmer/consultant, writer/author, and software developer of the RPG xTools, a popular add-on subprocedure library for RPG IV. His book The Modern RPG Language has been the most widely used RPG programming book for nearly two decades. He, along with others, speaks at and runs the highly-popular RPG World conference for RPG programmers.



Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.
IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn:
LATEST COMMENTS
MC Press Online