Sie sind auf Seite 1von 4

MC Press Online

%XFOOT with %LEN


Contributed by Robert Cozzi Tuesday, 19 September 2006 Last Updated Tuesday, 27 May 2008

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 9position packed with zero decimals, is specified as follows:

D qty

9P 0 Dim(10)

Apply %size to the QTY array to calculate the number of bytes, as follows:

bytes = %size(qty); 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:

len = %len(qty);

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:

http://www.mcpressonline.com

Powered by Joomla!

Generated: 6 October, 2008, 23:49

MC Press Online

D qty D totDigits /free

S S

9P 0 Dim(10) 10I 0

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 NAMES D totLen /free S

20A Dim(20) VARYING 10I 0

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

http://www.mcpressonline.com

Powered by Joomla!

Generated: 6 October, 2008, 23:49

MC Press Online

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 NAMES D totLen /free S

20A Dim(20) VARYING 10I 0

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.

http://www.mcpressonline.com

Powered by Joomla!

Generated: 6 October, 2008, 23:49

MC Press Online

http://www.mcpressonline.com

Powered by Joomla!

Generated: 6 October, 2008, 23:49

Das könnte Ihnen auch gefallen