%XFOOT with %LEN

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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.

 

BOB 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 Robert Cozzi available now on the MC Press Bookstore.

RPG TnT RPG TnT
Get this jam-packed resource 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

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  •  

  • LANSA 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.

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra 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: