MC Press Online

Monday, Feb 20th

Last updateSun, 19 Feb 2017 12pm

You are here: Home ARTICLES Programming RPG %XFOOT with %LEN

Programming / RPG

%XFOOT with %LEN

Support MC Press - Visit Our Sponsors

NEW BOOK!

Flexible Input, Dazzling Output with IBM i


ORDER YOUR COPY

*******************

Click for this Month's

Bookstore Special Deals

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:

     D qty             S              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:

 

     D qty             S              9P 0 Dim(10)
     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 NAMES           S             20A   Dim(20) VARYING
     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 NAMES           S             20A   Dim(20) VARYING
     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.

 

Robert Cozzi

Bob Cozzi is a programmer/consultant, writer/author, and software developer. His popular RPG xTools add-on subprocedure library for RPG IV is fast becoming a standard with RPG developers. His book The Modern RPG Language has been the most widely used RPG programming book for more than a decade. He, along with others, speaks at and produces the highly popular RPG World conference for RPG programmers.

 

MC Press books written by Bob Cozzi available now on the MC Press Bookstore.

 

RPG TnT RPG TnT

Jam-packed with an unbelievable number of quick, easy-to-implement RPG tips!

List Price $65.00
Now On Sale
 
The Modern RPG IV Language The Modern RPG IV Language
Cozzi on everything RPG! What more could you want?

List Price $99.95
Now On Sale
 
BLOG COMMENTS POWERED BY DISQUS