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

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Raz-Lee Security

    Start your Road to Zero Trust!

    Firewall Network security, controlling Exit Points, Open DB’s and SSH. Rule Wizards and graphical BI.

    Request Demo

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • 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 Feeds and Speeds of the New System i5s

    Leave a Reply Cancel 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

  • FAX/400 And CICS For i Are Dead. What Will IBM Kill Next?
  • Fresche Overhauls X-Analysis With Web UI, AI Smarts
  • Is It Time To Add The Rust Programming Language To IBM i?
  • Is IBM Going To Raise Prices On Power10 Expert Care?
  • IBM i PTF Guide, Volume 27, Number 20
  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19

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