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.

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 .  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 with 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-02/03/2010
      ' ------------------------------------------------------------------------------
      ' Copyright (C) 1997-2010 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.

      ' 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$)

      ' 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-02/03/2010
      ' ------------------------------------------------------------------------------

        #INCLUDE "TRANS"

        FUNCTION PBMAIN

        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",""   ); "'"

        END FUNCTION
  
         

 TRANS$  Debugging  Logout                    Source program             Debugging program


   TRANS$(0.0)  Translate Character String Symbols  09-17-2016  14:41
   ------------------------------------------------------------------

   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   '
        

         

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–2010 by
Go to:  Davar site entry | Site contents | Site index | Personal Computer | PowerBASIC | Text top