• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • A FUNction to Align Text

    February 1, 2006 Hey, Ted

    I read with interest your article on the IIF function. I have a function called alignText that I use in just about every program. I use it primarily to center headings, but have found it useful for right adjust as well. I have had this for a long time, so it is in traditional form, not freeform. Notice that there are three optional parameters. The alignment defaults to centering, and then two additional parameters are used to change the display attribute if I want to.

    — Gordon Larkins, ASK Senior Technical Consultant

    It was nice of Gordie to share his routine with the rest of us. Centering text is a common task in green-screen and report programming. Everybody needs one of these routines in the old toolbox.

    Here’s the procedure prototype for alignText. There are several ways to handle the storage of prototypes, but I think the easiest is to place the prototype in a member of a source physical file called PROTOTYPES. All callers will need to use a /COPY or /INCLUDE directive to bring the prototype in at compilation.

    D alignText       pr           132                          
    D  inText                      132    value                 
    D  inLen                        10i 0 value                 
    D  inAlign                       1    value options(*nopass)
    D  inBegAtt                      1    value options(*nopass)
    D  inEndAtt                      1    value options(*nopass)
    

    Here’s the source code for an ALIGNTEXT module.

    H nomain                                                    
                                                                
    D/copy prototypes,alignText                                 
    P alignText       b                   export                
    D alignText       pi           132                          
    D  inText                      132    value                 
    D  inLen                        10i 0 value                 
    D  inAlign                       1    value options(*nopass)
    D  inBegAtt                      1    value options(*nopass)
    D  inEndAtt                      1    value options(*nopass)
                                                                
    D outText         s            132                          
    D cx              s             10i 0                       
                                                                
    C                   Do                                      
                                                                
    C                   If        inText = *Blank               
    C                             or inLen < %Len(%Trim(inText))
    C                   Eval      outText = inText                   
    C                   Leave                                        
    C                   Endif                                        
                                                                     
    C                   Select                                       
                                                                     
    C                   When      %Parms > 4                         
    C                   Eval      outText = inBegAtt + %Trim(inText) 
    C                                       + inEndAtt               
    C                   When      %Parms > 3                         
    C                   Eval      outText = inBegAtt + %Trim(inText) 
                                                                     
    C                   Other                                        
    C                   Eval      outText = %Trim(inText)            
    C                   Endsl                                        
                                                                     
     * Right Align Text
    C                   If        %Parms > 2 and inAlign = 'L'           
    C                   Eval      cx = inLen - %Len(%Trim(outText)) + 1  
    C                   Eval      %Subst(outText : cx) = outText         
    C                   Eval      %Subst(outText : 1 : cx - 1) = *Blank  
    C                   Leave                                            
    C                   Endif                                            
                                                                         
     * Center Text                                                       
                                                                         
    C                   Eval      cx = (inLen - %Len(%Trim(outText))) / 2
    C     ' '           Cat(p)    outText : cx  outText                  
                                                                         
    C                   Enddo                                            
                                                                         
    C                   Return    outText                                
                                                                         
    P align Text       e
    

    I recommend that the module be converted to a service program. Anyone who already has a service program for the manipulation of character strings might want to add this routine to that service program.

    Gordie also sent a couple of code fragments showing how to call alignText. The first one does not use my IIF routine.

    D hdrOne          s             45a 
    D parmInqMnt      s              1a 
    
    D/copy prototypes,alignText         
    
     /free                      
        *inlr = *on;            
        hdrOne = 'Customer Master'; 
    
        if parmInqMnt = 'M';        
            hdrOne = %Trim(hdrOne) + ' Maintenance'; 
        else;                       
            hdrOne = %Trim(hdrOne) + ' Inquiry'; 
        endif;               
                                     
        hdrOne = alignText(hdrOne : %Len(hdrOne));
    

    The second fragment uses IIF, which shortens the code considerably. This is a good demonstration of how the creation of well-designed, reusable subprocedures can reduce the programming effort and thereby increase productivity.

    D hdrOne          s             45a   
    D parmInqMnt      s              1a   
                                          
    D/copy prototypes,alignText           
    D/copy prototypes,iif                 
                                          
     /free                                
        *inlr = *on;                      
        hdrOne = 'Customer Master';       
    
        hdrOne = alignText('Customer Master '  
               + iif(parmInqMnt = 'M' : 'Maintenance' : 'Inquiry')
               : %Len(hdrOne)); 
    

    These program fragments center the text. For right-adjust, pass an L into the third parameter.

    hdrOne = alignText(hdrOne : %Len(hdrOne):'L');
    

    To learn more about the P-codes that can be passed into the fourth and fifth parameters, see the discussion of P-field values in the online DDS reference manual. The following command causes the hdrOne variable to blink on monochrome terminals and to be displayed in red on color terminals.

    hdrOne = alignText(hdrOne : %Len(hdrOne):'C': x'28': x'20');
    

    –Ted

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags:

    Sponsored by
    New Generation Software

    Put your IBM i Data to Work

    NGS-IQ makes it easy to securely access, format, analyze, and share critical business data across your organization.

    Join us for a look at the top challenges companies attending COMMON NAViGATE at TechXchange 2026 shared when it comes to getting reliable reports from their DB2 data — and how NGS-IQ can help provide the answers you need without breaking the budget.

    Register for the Webinar

    www.ngsi.com – (800)824-1220

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Sponsored Links

    Computer Keyes:  Rapidly convert *SCS printer files into black and white or full color PDF documents
    Cosyn Software:  Audit Trail/400 for Sarbanes Oxley
    COMMON:  Join us at the Spring 2006 conference, March 26-30, in Minneapolis, Minnesota

    Avnet, Lakeview Partner on iSeries High Availability Alternatives to Clear Physical File Member

    Leave a ReplyCancel reply

Volume 6, Number 2 -- February 1, 2006
THIS ISSUE SPONSORED BY:

WorksRight Software
Advanced Systems Concepts
COMMON

Table of Contents

  • A FUNction to Align Text
  • Alternatives to Clear Physical File Member
  • Admin Alert: Six Simple Rules for OS/400 Group Profiles
  • Alternate SQL Row-Selection Criteria, Take 3
  • Indicate Negative Numbers with Parentheses
  • Admin Alert: Limiting *PUBLIC Access to i5/OS Objects, Part 2

Content archive

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

Recent Posts

  • Fall TRs Expected Next Week At Combined IBM-COMMON Conference
  • GenAI Brings A New Kind Of Agile Computing That Needs Controls Even More
  • IBM i, AI, And The Cost Of “We’ve Always Done It That Way”
  • Infor Says IBM i Customers Are Welcome To Use New AI
  • IBM i PTF Guide, Volume 28, Number 36
  • Help Us Understand The IBM i Marketplace By Taking The Survey
  • IBM Goes GA With Power Autonomous Operations
  • IBM Wheels And Deals To Sell Power Systems, Storage, and Software
  • Guru: When RNF0722 Won’t Let Your Legendary RPG Program Compile
  • A Read-Only MCP Server For Db2 for i

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 © 2025 IT Jungle