Examine... Translate

Hi,

I am little confused if the following can be acomplished with the help of EXAMINE…TRANSLATE statement

Display all the customer names and set the first character of every word into upper case, the rest to lower case and connect separate words by ‘-‘

Input: MR JOHN SMITH

OUTPUT-SAMPLE : Mr-John-Smith

I have done EXAMINE #STRING FOR ’ ’ REPLACE WITH ‘-’. This gives me MR-JOHN-SMITH.

Now how can I only translate the first character of each word to Upper case? If I use TRANSLATE TO LOWER CASE, it will make the entire string to lower case.

Is it possible to translate a specific character to upper case?

EXAMINE SUBSTRING(#STRING,1,1) TRANSLATE INTO UPPER CASE

Hi Wolfgang,

It will only translate the first character of the first word to upper case. How about my remaining words in the string?

That won’t work without parsing the string in one or the other way.

The easiest way would probably be to SEPARATE the string into words,
translate the first character of every word to upper case, then COMPRESS
them again with ‘-’ as the filler character.

Thanks Wolfgang… I was just wondering if there is something that we can do with only help of EXAMINE statement…

Afraid not, EXAMINE is a VERY powerful statement, but that’s just asking a bit too much :wink:

I love a good puzzle. As an exercise in the EXAMINE statement, here is one solution. I’m not sure if I would recommend it or not.


DEFINE DATA LOCAL                                                 
1 #A  (A15) INIT <'MR JOHN DOE'>                                  
1 #LC (A2/26) INIT <' a',' b',' c',' d',' e',' f',' g',' h',      
                    ' i',' j',' k',' l',' m',' n',' o',' p',' q', 
                    ' r',' s',' t',' u',' v',' w',' x',' y',' z'> 
1 #UC (A2/26) INIT <' A',' B',' C',' D',' E',' F',' G',' H',      
                    ' I',' J',' K',' L',' M',' N',' O',' P',' Q', 
                    ' R',' S',' T',' U',' V',' W',' X',' Y',' Z'> 
1 #I  (I4)                                                        
END-DEFINE                                                        
*                                                                 
EXAMINE #A                TRANSLATE INTO LOWER CASE               
EXAMINE SUBSTRING(#A,1,1) TRANSLATE INTO UPPER CASE               
FOR #I = 1 TO 26                                                  
  EXAMINE #A FOR #LC(#I) REPLACE WITH #UC(#I)                     
END-FOR                                                           
EXAMINE #A FOR ' ' REPLACE WITH '-'                               
DISPLAY '=' #A                                                    
END                                                               

Actually, that might not be too bad.

1 Like

I think, a real regexp-implementation would be much more powerful and a big help for Mishra’s task…

And here is my workaround:

define data local
01 #string (A50) init <'MR JOHN SMITH'>
01 #i1 (i1)
end-define
display #string
examine substr(#string,2) translate into lower
repeat
  EXAMINE #STRING FOR ' ' REPLACE first with '-' giving position #i1
  if #i1 > 0
    examine substr(#string,#i1,2) translate into upper
  else
    escape bottom
  end-if
end-repeat
end
2 Likes

“The people are silent” :slight_smile:

These are the concluding words of the last scene of the tragedy “Boris Godunov” (1825), Alexander Pushkin

Thank you, Matthias! I did not expect the EXAMINE statement was that powerful

Thank you Jerome and Matthias. Both the work arounds are great help to me. The approches are good enough.

Hats off to all forum members for the help you extend in times of my need. This is the place where I learn Natural stuffs.

Not too bad, but can be simplified a bit :wink:


DEFINE DATA LOCAL                                                
1 #A  (A15) INIT <'MR JOHN DOE'>                                 
1 #LC (A2/26) INIT <' a',' b',' c',' d',' e',' f',' g',' h',     
                    ' i',' j',' k',' l',' m',' n',' o',' p',' q',
                    ' r',' s',' t',' u',' v',' w',' x',' y',' z'>
1 #UC (A2/26) INIT <'-A','-B','-C','-D','-E','-F','-G','-H',     
                    '-I','-J','-K','-L','-M','-N','-O','-P','-Q',
                    '-R','-S','-T','-U','-V','-W','-X','-Y','-Z'>
1 #I  (I4)                                                       
END-DEFINE                                                       
*                                                                
EXAMINE #A                TRANSLATE INTO LOWER CASE              
EXAMINE SUBSTRING(#A,1,1) TRANSLATE INTO UPPER CASE              
FOR #I = 1 TO 26                                                 
  EXAMINE #A FOR #LC(#I) REPLACE WITH #UC(#I)                    
END-FOR                                                          
DISPLAY '=' #A                                                   
END                                                              

Never mind; read the rest of the posts, and this is the same as Matthias’s solution. Looks like the most efficient as well as the most innovative.

will try later today for efficiency test.

steve

I do not have time to try this, and have to leave soon, so perhaps someone else could try this.

Define something like:

1 #extra (a51)
1 redefine #extra
2 #fill (a1) init <’ '>
2 #string (a50) init <‘MR. JOHN BROWN’>

examine and translate #extra to lower case
examine and translate first character of #string to upper case
repeat loop
examine #string for a blank giving number and position replace with ‘-’
if number is zero, escape, you are done
else
add 1 to position
examine and translate one character at position (after adding the one) to upper case

steve