• Welcome to OS2World OLD-STATIC-BACKUP Forum.
 

News:

This is an old OS2World backup forum for reference only. IT IS READ ONLY!!!

If you need help with OS/2 - eComStation visit http://www.os2world.com/forum

Main Menu

Difference between changestr and translate

Started by jep, 2008.04.07, 14:26:20

Previous topic - Next topic

jep

Marked as: Easy
Object Rexx
Hello,

Many have the opinion that one should stick with Classic Rexx because it's not that picky and object raxx xan't handle those scripts. A drawbac though is that those who know how Object Rexx was develop can state that the compiler used didn't produce the perfect script engine we'd like.

I've found that Object Rexx contain alot of interesting functions that Classic rexx lack and that almost all classic rexx scripts work without any modifications. Object rexx check the code before execution and is probably stricter, so sloppy code may have to be rewritten a bit though.


f_size = stream( fileName, "c", "QUERY SIZE" )
        call stream fileName, "c", "OPEN READ"
       
        fileContents = charin( fileName, 1, f_size )
   
        call stream fileName, "c", "CLOSE"


say CHANGESTR( '%**P', fileContents, 'C:\Path_to_file' )
say TRANSLATE( fileContents, '/', '\~' )


changestr search and replace the whole string ( '%**P in the example above ) with the other ( 'C:\Path_to_file' ) while translate replace every occurance of a character ( '\' ) with the character at the corresponding position in the other string ( '/' ). Additional characters to search for that doesn't have a replacement char in the other string ( '/' ) are removed.

Translate is also very useful when one want to preform a caseless string match.
  text = TRANSLATE( text ) /* Convert all text to UPPER CASE */
  if text = 'THIS IS A MATCH' then Return 1


the opposite is a bit trickier, but then you may want to use:
  text = TRANSLATE( text, XRANGE( 'a', 'z' ), XRANGE( 'A', 'Z' ) ) /* Convert all text to lower case */
  if text = 'this is a match' then Return 1

but you may not that you may want to add some more characters such as ( üåäö vs. üÅÄÖ ) to support more languages, the problem with those characters are that they're scattered out in the ascii table and that different codepages may have position them somewhere else than you expect. If you use the first method you can be sure that it convert all characters to upper case and that your comparison will work better.