STRPDM.COM                  

   Home    |    IBM Manuals Direct    |    iBytes Library   |    Links    |   About     |   Book Store   |  On-Line Code

 

iByte

Subject – Using EVAL, %TRIM, and %EDITC to build strings

One of my favorite things about EVAL is it’s ability to build a character field. Used in conjunction with the %TRIM, and %EDITC built in functions you can construct a string to your exact specifications with the ability to incorporate edited numeric fields into the build.

Say we have the following data: (of course we would have to go back a few years for the data to be true)

D* Field values

D TVlisting          S     50

D TVshow             C          ' Seinfeld    '

D TVtime             C          '9pm          '

D TVday              C          ' Thursdays '

D TVnetwork          C          '     NBC     '

D TVchannel          S     3 0  INZ(032)

If we would like to build the following TV listing in field TVlisting. We could do this with EVAL.

"Catch Seinfeld 9pm Thursdays on NBC 32 "

C                 EVAL TVlisting = 'Catch' + %TRIMR(TVshow) +

C                                 ' ' + %TRIM(TVtime) + TVday +

C                                 'on ' + %TRIM(TVnetwork) +

C                                 %EDITC(TVchannel:'Z')

 

We use %TRIMR to trim the right blanks from Seinfeld. We keep the left blank to use between ‘Catch’ and ‘Seinfeld’.

We add a blank between Seinfeld and 9pm, and trim all blanks around 9pm using %TRIM. Since all blanks are to the right we could have used %TRIMR again.

We just tagged on ‘ Thursdays ‘ to take advantage of the blank before and after.

NBC is surrounded by blanks so %TRIM is used to remove all surrounding blanks.

Because channel 32 is a numeric field %EDITC must be used. An actual edit code of ‘Z’ is used to cut the leading zero.