Go to:  Davar site entry | Site contents | Site index | Personal Computer | PowerBASIC | Text bottom

TRANS$  PowerBASIC  Function

         

TRANS$ function evaluates to the first parameter with its symbols translated according to the rules specified by its other two parameters (symbol-per-symbol replacement).  Special cases are taken care of (see function description below).  With PowerBASIC TRANS$ serves merely as a functional interface for the REPLACE ANY operator, and its sole purpose is to support the functional style of programming.  (Why was REPLACE made an operator by PowerBASIC developers instead of being made a function is beyond my comprehension).

With the older and less sophisticated Microsoft BASICs I also used to have the TRANS$ function, but that one had its logic fully coded.  With PowerBASIC this is simply a matter of changing the coding style, and I definitely prefer the functional style for all sorts of data conversion.

Example below illustrates this style difference (see DSC2REN routine code).  It is necessary here to convert description into file name by getting rid of punctuation, and substituting spaces between words by underscores.  This can be achieved in three conversion steps:

  • Translate punctuation into spaces.
  • Compress all successive spaces into one space.
  • Translate spaces into underscores.

Here's how this could be done using REPLACE ANY operator and COMPRES$ function:


REPLACE ANY ".,:;" WITH "    " IN Comment$
Comment$=COMPRES$(Comment$," ")
REPLACE ANY " " WITH "_" IN Comment$
        

TRANS$ function makes the same sequence not only shorter, but also more clear (IMHO, of course).


Comment$=TRANS$(COMPRES$(TRANS$(Comment$,".,:;","    ")," ")," ","_")
        


 TRANS$  Source  Program                           Debugging program               Debugging logout

      ' TRANS$(0.0)  Translate Character String Symbols      05/30/1997-04/04/2002
      ' --------------------------------------------------------------------------
      ' Copyright (C) 1997-2002 by Vladimir Veytsel                  www.davar.net

      ' Type ---------------------------------------------------------------------

      '    Function

      ' Description --------------------------------------------------------------

      '    TRANS$ function returns the first parameter with its symbols translated
      '    according to the rules specified by its other two parameters.

      ' Declaration --------------------------------------------------------------

      '    DECLARE FUNCTION TRANS$(Strng$,Source$,Target$)

      ' Parameters ---------------------------------------------------------------

      '    Strng$   - Source character string to be changed by translation
      '    Source$  - Source symbols to be translated
      '    Target$  - Target symbols for translation

      ' Value --------------------------------------------------------------------

      '  - If specified string is EMPTY,
      '       then empty string is returned to the point of invocation

      '  - If specified string is   NOT empty and
      '       source symbols are either empty or
      '       not found within specified string,
      '       THEN original string is returned to the point of invocation

      '  - If specified string is NOT empty and
      '       source symbols are found within specified string,
      '       then result of replacement of every entry of source symbol
      '            by corresponding (same position) target symbol
      '            is returned to the point of invocation

      ' Note ---------------------------------------------------------------------

      '    Since translation is in fact symbol-per-symbol replacement, Target$
      '    parameter should have the SAME length as Source$ parameter.  If this is
      '    not the case, the length of Target$ parameter gets adjusted to be equal
      '    that of the Source$ parameter.  This is done either by truncating
      '    Target$ parameter value, or padding it by blanks in order to match the
      '    length of Source$ parameter value.

      ' Examples -----------------------------------------------------------------

      '    TRANS$(""               ,"XYZ","*"  )=""
      '    TRANS$("ABCDEF"         ,""   ,"*"  )="ABCDEF"
      '    TRANS$("ABCDEF"         ,"XYZ","*"  )="ABCDEF"
      '    TRANS$("XYZABCDEF"      ,"XYZ","xyz")="xyzABCDEF"
      '    TRANS$("ABCXYZDEF"      ,"XYZ","xyz")="ABCxyzDEF"
      '    TRANS$("ABCDEFXYZ"      ,"XYZ","xyz")="ABCDEFxyz"
      '    TRANS$("XYZABCXYZDEFXYZ","XYZ","xyz")="xyzABCxyzDEFxyz"
      '    TRANS$("XYZABCXYZDEFXYZ","XY" ,"xyz")="xyZABCxyZDEFxyZ"
      '    TRANS$("XYZABCXYZDEFXYZ","XYZ",""   )="   ABC   DEF   "

      ' Start Function -----------------------------------------------------------

           DEFINT A-Z  ' All defaulted variables are integer

           FUNCTION TRANS$(Strng$,Source$,Target$) PUBLIC

      ' Check Special Cases (Translation is Impossible) --------------------------

           IF ((LEN(Strng$)=0) OR _
               (LEN(Source$)=0)) THEN
              TRANS$=Strng$
              EXIT FUNCTION
           END IF

      ' Adjust Target$ Value to Match the Length of Source$ Value ----------------

           Target.Str$=LEFT$(Target$+SPACE$(LEN(Source$)),LEN(Source$))

      ' Form and Return Function Value to the Point of Invocation ----------------

           Work.Strng$=Strng$
           REPLACE ANY Source$ WITH Target.Str$ IN Work.Strng$

           TRANS$=Work.Strng$

      ' Finish Function ----------------------------------------------------------

           END FUNCTION
  
         

 TRANS$  Debugging  Program                         Source program               Debugging logout

      ' TRANS$(0.0)  Translate Character String Symbols      05/30/1997-04/04/2002
      ' --------------------------------------------------------------------------

        $INCLUDE "TRANS"

        DECLARE FUNCTION TRANS$(Strng$,Source$,Target$)

        CLS
        PRINT "TRANS$(0.0)  Translate Character String Symbols  ";DATE$;
        PRINT "  ";LEFT$(TIME$,5)
        PRINT STRING$(66,"-")
        PRINT

        PRINT "TRANS$(''               ,'XYZ','*'  )='"; _
               TRANS$(""               ,"XYZ","*"  ); "'"
        PRINT "TRANS$('ABCDEF'         ,''   ,'*'  )='"; _
               TRANS$("ABCDEF"         ,""   ,"*"  ); "'"
        PRINT "TRANS$('ABCDEF'         ,'XYZ','xyz')='"; _
               TRANS$("ABCDEF"         ,"XYZ","xyz"); "'"
        PRINT "TRANS$('XYZABCDEF'      ,'XYZ','xyz')='"; _
               TRANS$("XYZABCDEF"      ,"XYZ","xyz"); "'"
        PRINT "TRANS$('ABCXYZDEF'      ,'XYZ','xyz')='"; _
               TRANS$("ABCXYZDEF"      ,"XYZ","xyz"); "'"
        PRINT "TRANS$('ABCDEFXYZ'      ,'XYZ','xyz')='"; _
               TRANS$("ABCDEFXYZ"      ,"XYZ","xyz"); "'"
        PRINT "TRANS$('XYZABCXYZDEFXYZ','XYZ','xyz')='"; _
               TRANS$("XYZABCXYZDEFXYZ","XYZ","xyz"); "'"
        PRINT "TRANS$('XYZABCXYZDEFXYZ','XY' ,'xyz')='"; _
               TRANS$("XYZABCXYZDEFXYZ","XY" ,"xyz"); "'"
        PRINT "TRANS$('XYZABCXYZDEFXYZ','XYZ',''   )='"; _
               TRANS$("XYZABCXYZDEFXYZ","XYZ",""   ); "'"

        PRINT
        PRINT "Execution completed - hit [Enter] to continue..."
  
         

 TRANS$  Debugging  Logout                    Source program             Debugging program

         

   TRANS$(0.0)  Translate Character String Symbols  04-04-2002  18:58
   ------------------------------------------------------------------

   TRANS$(''               ,'XYZ','*'  )=''
   TRANS$('ABCDEF'         ,''   ,'*'  )='ABCDEF'
   TRANS$('ABCDEF'         ,'XYZ','xyz')='ABCDEF'
   TRANS$('XYZABCDEF'      ,'XYZ','xyz')='xyzABCDEF'
   TRANS$('ABCXYZDEF'      ,'XYZ','xyz')='ABCxyzDEF'
   TRANS$('ABCDEFXYZ'      ,'XYZ','xyz')='ABCDEFxyz'
   TRANS$('XYZABCXYZDEFXYZ','XYZ','xyz')='xyzABCxyzDEFxyz'
   TRANS$('XYZABCXYZDEFXYZ','XY' ,'xyz')='xyZABCxyZDEFxyZ'
   TRANS$('XYZABCXYZDEFXYZ','XYZ',''   )='   ABC   DEF   '

   Execution completed - hit [Enter] to continue...
        
      

         

View [and save] TRANS.BAS text       View [and save] ZTRANS.BAS text
(Use [Back] button or [Alt]+[CL] to return here from the viewed text)
Copyright © 1997–2005 by
Go to:  Davar site entry | Site contents | Site index | Personal Computer | PowerBASIC | Text top