User login

Formula No 2 - Make Caps to Small and vice versa using Built in function %XLate - RPG IV Translator

Okay, Several times, we need to convert capital letters of a word or an alphanumeric characters to small letters. One practical example which I can think of now is when we receive some input in the form of a flat file. While validating input fields from the flat file, say the Name, we should convert them to all small or all capital before comparing with the system value. Here's a quick and easy way to do the conversion.

The Formula:-

We can capitalize all letters of a particular character variable, using the RPG 400 built in function %XLate.

The variable "Caps" will have 'TUTORIAL INDIA' after the below statement is executed.

Caps = %XLate('abcdefghijklmnopqrsuvwzyz':'ABCDEFGHIJKLMNOPQRSTUVWZYZ':'Tutorial India')

Similary, the variable, Small will have 'tutorial india' after the statement is executed.
Small = %XLate('ABCDEFGHIJKLMNOPQRSTUVWZYZ': 'abcdefghijklmnopqrsuvwzyz': 'TUToriAl InDiA')

Here, I have used a constant literal, A variable containing the characters will also work the same way.


All right, The above example is just one usage of the built in function %XLate. Let us learn the anatomy of %XLate function now.

RPG ILE Built In Function %XLate - Translate:-
This function takes four arguments in this order

  1. The character or characters which are to be transalated from
  2. The character/s which are to be translated to
  3. The alphanumeric variable which is to be translated.
  4. Optional position value. The translation starts from here.

%Xlate function scan for argument 1 in argument 3 from position given at argument 4 and if it finds it, it replaces it with the corresponding character in argument 2. Simple! Isn't it.

I hope that things have become a little clearer by now. Here's an example which I ran on the AS400 Box. This will further clarify the things.

D @ToParm         S             20                                     
D @Small          C                   'abcdefghijklmnopqrstuvwxyz'     
D @Caps           C                   'ABCDEFGHIJKLMNOPQRSTUVWXYZ'     
C     *Entry        PList                                              
C                   Parm                    @Entry           20        
C                   Parm                    @To               5        
C                   If        @To = 'SMALL'                            
C                   Eval      @ToParm = %XLate(@Caps: @Small: @Entry)  
                                                                       
C                   Else                                               
                                                                       
C                   Eval      @ToParm = %XLate(@Small: @Caps: @Entry)  
C                   EndIf                                              
C     @ToParm       Dsply                                              
C                   Eval      *InLr = *On                              

Sample Run

>>>Command used to call this program.

call xlate parm('Tutorial India' 'SMALL')

And its output is

DSPLY tutorial india

>>>Command used to call this program
call xlate parm('Tutorial India' 'CAPS')

And its output is
DSPLY TUTORIAL INDIA

We could have used the optional argument "position" value to start translation from a particular position.


What happens when the from argument of the %XLate function does not have exactly same number of To argument.

Well, In this situation the argument having lesser no of character, wins. Only those characters which have corresponding character in From argument are considered for replacement. Anything extra in the "To" argument is simply ignored.

Example:-
If in the example given above, we remove 't' from @Small, Final output when we translate to CAPS would be 'TTtORIAL INDIA'. Simlarly, if the "To" parm has lesser count, extra "From" Parm is ignored.

This final table should clarify everything we have learnt so far. We can have following three scenarios possible, we've discussed. This table just keeps together what we have learned so far. We are playing here will letter t only.

@Small @Caps Input Output
abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ Tutorial India TUTORIAL INDIA
abcdefghijklmnopqrsuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ Tutorial India TTtORIAL INDIA
abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRS Tutorial India TutORIAL INDIA

RPG III equivalent of the built in function %XLate is XLate Opcode. It's pretty easy to use, with some restrictions. The format is something like From:To XLate String:Start Position Result. Try taking F4(prompt) for this function. in SEU.

All right. Time to sleep now. Send me how you like or dislike this post at admin@tutorialindia.com. Or put a comment below. By the way, in the first paragraph, in fact we should convert both flat file value as well as our database value to either small or caps.

Thanks for reading!!

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Recent comments