• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru Classic: A Bevy Of BIFs — Updates

    February 5, 2020 Jon Paris

    In my previous tip I returned to the “Bevy” series to tell you about the latest addition to the family: %ScanRpl. Shortly after completing that tip I realized that there have been a number of more recent BIFs such as %SubArr and some enhancements to existing BIFs like %Trimx that seem to have escaped people’s notice. In this tip I attempt to fill those omissions.

    %Trim: Specify Characters to Trim

    Let’s start with a BIF that hopefully you are all familiar with: %Trim. Before going any further I should point out that when I say %Trim, I am referring to the entire Trim family, i.e., %Trim, %TrimL, and %TrimR.

    What you might not realize though is that %Trim now offers an optional second parameter that allows you to specify exactly which character(s) you want to trim off. This can be really useful when processing data from CSV files or Web services where you encounter numeric values with leading asterisks and/or a currency symbol. For example $250.95 or ***125. The sample code below shows how this new option would allow you to “clean up” such values.

    Running it will result in the field target receiving the value 123.45 and having a length of six, at this point it is able to be processed by %Dec() for subsequent use in numeric calculations.

    dcl-s  charAmt2  char(20)  Inz('***$123.45');
    dcl-s  target         varchar(20);
    target = %Trim( charAmt2 : ' *$' );
    

    Note that in order to have blanks removed (trailing blanks in the example) I had to include a space in the string of characters to be removed. This would not have been needed if the intent was simply to use the resulting value with %Dec() since that BIF will automatically ignore leading and trailing spaces.

    %Addr: *Data Option

    This is one that I’m sure a lot of people missed, judging from the number of examples I see posted in online forums that do not make use of it.

    Introduced back in V5R4, the *Data option of the %Addr BIF returns the address of the data portion of a varying-length field, automatically bypassing the two (or four) byte count header portion. I make a lot of use of this capability in programs that build a string iteratively. For example, records to be written to CSV files, JSON, HTML and XML, etc. These sorts of strings are much more efficiently built in varying-length fields than in fixed length, but until this option was introduced it was necessary to manually add two (or four) to the address before passing it to an API. As a result you may encounter this kind of code.

    pParmData = %Addr(ParmData) + 2; 
    

    Using this new option allows you to code it this way:

    pParmData = %Addr(ParmData: *Data);
    

    Not only is this code simpler and more obvious to anyone who has to maintain the code in the future, but it is also much safer.

    Why so? Suppose that ParmData was originally 50,000 bytes long, but we subsequently need to increase the size to 100,000 bytes. At this point we have to hope that the programmer tasked with making this change is aware that a varying length field of 100,000 bytes requires a 4-byte header, not a 2-byte. As a result, simply changing the field length is not enough. They must also remember to change the “+ 2” to “+ 4” or the address created will be incorrect. Had the original programmer used the *Data option then this would never have become a problem since the compiler would have automatically calculated the correct address.

    Arrays, Anyone?

    Ever since I started coding in PHP, where arrays are a way of life, I realized that RPGers don’t use arrays as much as they should. But even among those who do make use of them, the %SubArr BIF seems to have slipped by unnoticed. Until the advent of the V7.4 release, RPG lacked the ability to use dynamic arrays in the sense that languages like PHP do. %SubArr however does allow us to surmount some of the problems that fixed length arrays can present.

    %SubArr allows you to specify that an operation, such as a SORTA, is to take place on only a portion of the array. So, for example, if while loading the array myArray we keep track of the number of active elements in the field count we can subsequently use that value with %SubArr like so:

    SORTA %SubArr( myArray : 1 : count );
    

    This allows us to sort only the active portion of the array. Not only is this faster, but it also avoids the need to have previously loaded all of the array elements with high (or low) values to “keep them out of the way” after the sort.

    %SubArr() can also be used to assign one portion of an array to another. Similar in some ways to the way in which the old MOVEA operation could be used. In the first example below, elements 5 to 9 of arrayZone are copied to elements 1 to 5 of arrayPack. In the second example the elements 1, 2 and 3 of arrayZone are copied into elements 2, 3 and 4 of arrayPack.

    dcl-s  arrayPack  packed(5)  dim(5);
    dcl-s  arrayZone  zoned(7:2)  dim(10);
    dcl-s  i  int(5);
    // Fill up arrayZone elements
    for i = 1 to %Elem(arrayZone);
       arrayZone(i) = i * 3.3333;
    EndFor;
    
    // Copy elements 5 - 9 of arrayZone to elements 1 - 5 of  arrayPack 
    arrayPack = %SubArr( arrayZone : 5  );
    
    // Copy elements 1, 2 & 3 of arrayZone into elements 2, 3 & 4 of arrayPack 
    
    
    %SubArr( arrayPack : 2 : 3 ) = arrayZone;    
    

    While On The Subject Of Arrays . . .

    It may have slipped past you that in V7.1 we finally got the ability to perform lookup operations on Data Structure arrays. We have had DS arrays since V5R2, but to in order to search them we had to resort to APIs such as bsearch().

    While bsearch still has a role for complex lookups, it is no longer needed for simple lookup operations. As of V7.1 we can perform lookups directly on DS arrays as in the example below. Notice that (*) is used as the array index to indicate that the entire array is to be searched:

    dcl-s  element  int(5); 
    dcl-s  product  char(7);  
    dcl-ds productInfo  Dim(999)  Qualified;  
       productCode Like(product); 
       description Varchar(60); 
       price       Packed(7:2); 
       cost        Packed(7:2); 
    end-ds; 
    
    product = 'A1234456';
    element = %LookUp( product : productInfo(*).productCode );
    

    That’s a whole lot easier to code. The only downside is that currently only the basic %LookUp BIF is supported, not other members of the “family” such as %LookUpGE.

    Jon Paris is one of the world’s foremost experts on programming on the IBM i platform. A frequent author, forum contributor, and speaker at User Groups and technical conferences around the world, he is also an IBM Champion and a partner at Partner400 and System i Developer. He hosts the RPG & DB2 Summit twice per year with partners Susan Gantner and Paul Tuohy.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guruclassic, API, BIF, CSV, FHGC, Four Hundred Guru Classic, HTML, IBM i, JSON, V5R2, V5R4, XML

    Sponsored by
    UCG Technologies

    CYBER-ATTACKS ON THE RISE. PROTECT WITH THE TRIPLE PLAY.

    COVID-19 has not only caused a global pandemic, but has sparked a “cyber pandemic” as well.

    “Cybersecurity experts predict that in 2021, there will be a cyber-attack incident every 11 seconds. This is nearly twice what it was in 2019 (every 19 seconds), and four times the rate five years ago (every 40 seconds in 2016). It is expected that cybercrime will cost the global economy $6.1 trillion annually, making it the third-largest economy in the world, right behind those of the United States and China.”1

    Protecting an organization’s data is not a single-faceted approach, and companies need to do everything they can to both proactively prevent an attempted attack and reactively respond to a successful attack.

    UCG Technologies’ VAULT400 subscription defends IBM i and Intel systems against cyber-attacks through comprehensive protection with the Triple Play Protection – Cloud Backup, DRaaS, & Enterprise Cybersecurity Training.

    Cyber-attacks become more sophisticated every day. The dramatic rise of the remote workforce has accelerated this trend as cyber criminals aggressively target company employees with online social engineering attacks. It is crucial that employees have proper training on what NOT to click on. Cyber threats and social engineering are constantly evolving and UCG’s Enterprise Cybersecurity Training (powered by KnowBe4) is designed to educate employees on the current cutting-edge cyber-attacks and how to reduce and eliminate them.

    A company is only as strong as its weakest link and prevention is just part of the story. Organizations need to have a quick response and actionable plan to implement should their data become compromised. This is the role of cloud backup and disaster-recovery-as-a-service (DRaaS).

    Data is a company’s most valuable asset. UCG’s VAULT400 Cloud Backup provides 256-bit encrypted backups to two (2) remote locations for safe retrieval should a cyber-attack occur. This is a necessary component of any protection strategy. Whether a single click on a malicious link brings down the Windows environment or an infected SQL server feeds the IBM i, once the data is compromised, there is no going back unless you have your data readily available.

    Recovery is not a trivial task, especially when you factor in the time sensitive nature of restoring from an active attack. This leads to the third play of the Triple Play Protection – DRaaS.  Companies have myriad concerns once an attack is realized and a managed service disaster recovery allows employees to keep focus on running the business in a crisis state.

    The combination of training employees with secure backup and disaster recovery offers companies the best chance at avoiding financial disruption in an age of stronger, more frequent cyber-attacks.

    Reach out to UCG Technologies to discuss your company’s security needs and develop a data protection plan that fits you best.

    ucgtechnologies.com/triple-play

     800.211.8798 | info@ucgtechnologies.com

     

    1. https://theconversation.com/cyberattacks-are-on-the-rise-amid-work-from-home-how-to-protect-your-business-151268

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Guru Classic: Looking For Stuff With iSphere Guru Classic: Find a View of a View of a View. . .

    Leave a Reply Cancel reply

TFH Volume: 30 Issue: 9

This Issue Sponsored By

  • RPG & DB2 Summit
  • RPG & DB2 Summit
  • RPG & DB2 Summit

Table of Contents

  • Guru Classic: Find a View of a View of a View. . .
  • Guru Classic: A Bevy Of BIFs — Updates
  • Guru Classic: Looking For Stuff With iSphere

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Doing The Texas Two Step From Power9 To Power10
  • PHP’s Legacy Problem
  • Guru: For IBM i Newcomers, An Access Client Solutions Primer
  • IBM i 7.1 Extended Out To 2024 And Up To The IBM Cloud
  • Some Practical Advice On That HMC-Power9 Impedance Mismatch
  • IBM Extends Dynamic Capacity Pricing Scheme To Its Cloud
  • Here’s What You Should Do About The IBM i Skills Shortage
  • Matillion Founder Recounts Midrange Roots
  • Four Hundred Monitor, February 24
  • IBM i PTF Guide, Volume 23, Number 8

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2021 IT Jungle

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.