Strip Leading Zeros

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

From: Marc Salvatori To: All

I know others have shared their approach for stripping leading zeros from character fields that had numeric values moved into them. I want to share mine for the benefit of others, and I'm interested in any comments others might have.

1 contains a subroutine called STRIPZ which strips the leading zeros from a character field. I could have avoided using the BLANKS field if the SUBST operation would allow me to use the *BLANKS constant. However, this routine works regardless of the size of the fields I work with.

Figure 1 contains a subroutine called STRIPZ which strips the leading zeros from a character field. I could have avoided using the BLANKS field if the SUBST operation would allow me to use the *BLANKS constant. However, this routine works regardless of the size of the fields I work with.

From: Matt Sargent To: Marc Salvatori

I guess my main problem with this is the BLANKS field. You're initializing it each time the subroutine is called, which serves no purpose except to provide a place to define the field. Since its value is never altered, it might be better if BLANKS were a constant field. Also, its length should be extended to 30 bytes since RPG can handle numeric fields with up to 30 digits.

The routine I like to use is shown in 2. It's very compact and requires only one work field (@ZS). CFIELD is the character field containing the numeric data to be processed. I translate all the zeros to blanks; then I translate all the blanks past the first nonblank character back to zeros. The XLATE operations make this very easy.

The routine I like to use is shown in Figure 2. It's very compact and requires only one work field (@ZS). CFIELD is the character field containing the numeric data to be processed. I translate all the zeros to blanks; then I translate all the blanks past the first nonblank character back to zeros. The XLATE operations make this very easy.


Strip Leading Zeros

Figure 1 Subroutine STRIPZ

 *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 C MOVE NUMFLD NUMBER C EXSR STRIPZ C MOVE NUMBER CHRFLD 7 *================================================================ * Strip leading zeros from alpha-numeric integer *================================================================ C STRIPZ BEGSR C MOVE *BLANKS BLANKS 15 C ' 0' CHECKNUMBER C 20 C SELEC C C WHEQ *ZERO C MOVE '0' NUMBER 15 P C C WHGT 1 C SUB 1 C C C SUBSTBLANKS NUMBER C ENDSL C ENDSR *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 
Strip Leading Zeros

Figure 2 Strip Leading Zeros

 *. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 C '0':' ' XLATECFIELD CFIELD C ' ' CHECKCFIELD @ZS 30 C @ZS IFNE *ZERO C ' ':'0' XLATECFIELD:@ZSCFIELD C ENDIF 
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: