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

COMPRES$  PowerBASIC  Function

         

COMPRES$ function evaluates to its first parameter with all successive occurrences of characters specified by the second parameter being compressed to a single occurrence.  Special case is taken care of (see function description below).  If the second parameter is empty, all successive duplicate characters of the first parameter get compressed.



 COMPRES$  Source  Program                     Debugging program                   Debugging logout

      ' COMPRES$(0.0)  Compress Character String                 12/22/1988-02/01/2010
      ' ------------------------------------------------------------------------------
      ' Copyright (C) 1988-2010 by Vladimir Veytsel                      www.davar.net

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

      '    Function

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

      '    COMPRES$ function returns its first parameter with all successive
      '    occurrences of characters specified by the second parameter being
      '    compressed to a single occurrence.

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

      '    Strng$  - Character string to be compressed.
      '    Chars$  - Characters, all successive occurrences of which
      '              should to be compressed to a single occurrence.

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

      '    Character string compressed as specified by Chars$ parameter.

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

      '    Empty Chars$ parameter specifies compression of ALL successive
      '    duplicate characters of the string.

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

      '    COMPRES$(""      ,""   )=""
      '    COMPRES$("ABBCCC",""   )="ABC"
      '    COMPRES$("ABBCCC","A"  )="ABBCCC"
      '    COMPRES$("ABBCCC","B"  )="ABCCC"
      '    COMPRES$("ABBCCC","C"  )="ABBC"
      '    COMPRES$("ABBCCC","ABC")="ABC"

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

           DEFINT A-Z  ' All defaulted variables are integer

           FUNCTION COMPRES$(Strng$,Chars$)

      ' Check Special Case (Compression Is Impossible) -------------------------------

           IF (LEN(Strng$)<2) THEN
              COMPRES$=Strng$
              EXIT FUNCTION
           END IF

      ' Form Compressed String (with the Exception of Last Symbol) -------------------

           Chars_Lngth=LEN(Chars$)
           FOR I=1 TO LEN(Strng$)-1
               Curr_Str_Symb$=MID$(Strng$,I  ,1)
               Next_Str_Symb$=MID$(Strng$,I+1,1)
               IF (( Curr_Str_Symb$<>Next_Str_Symb$)OR  _
                   ((Curr_Str_Symb$= Next_Str_Symb$)AND _
                    (Chars_Lngth>0)                 AND _
                    (INSTR(Chars$,Curr_Str_Symb$)=0))) THEN
                  Work_Str$=Work_Str$+Curr_Str_Symb$
               END IF
           NEXT I

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

           COMPRES$=Work_Str$+RIGHT$(Strng$,1)

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

           END FUNCTION
  
         

 COMPRES$  Debugging  Program                     Source program                   Debugging logout

      ' COMPRES$(0.0)  Compress Character String                 05/29/1997-02/01/2010
      ' ------------------------------------------------------------------------------

        #INCLUDE "COMPRES"

        FUNCTION PBMAIN

        PRINT "COMPRES$(0.0)  Compress Character String  ";DATE$;
        PRINT "  ";LEFT$(TIME$,5)
        PRINT STRING$(59,"-")
        PRINT

        PRINT "COMPRES$(''      ,''   )='"; _
               COMPRES$(""      ,""   ); "'"
        PRINT "COMPRES$('ABBCCC',''   )='"; _
               COMPRES$("ABBCCC",""   ); "'"
        PRINT "COMPRES$('ABBCCC','A'  )='"; _
               COMPRES$("ABBCCC","A"  ); "'"
        PRINT "COMPRES$('ABBCCC','B'  )='"; _
               COMPRES$("ABBCCC","B"  ); "'"
        PRINT "COMPRES$('ABBCCC','C'  )='"; _
               COMPRES$("ABBCCC","C"  ); "'"
        PRINT "COMPRES$('ABBCCC','ABC')='"; _
               COMPRES$("ABBCCC","ABC"); "'"

        END FUNCTION
  
         

 COMPRES$  Debugging  Logout                   Source program                   Debugging program


   COMPRES$(0.0)  Compress Character String  02-02-2010  17:58
   -----------------------------------------------------------

   COMPRES$(''      ,''   )=''
   COMPRES$('ABBCCC',''   )='ABC'
   COMPRES$('ABBCCC','A'  )='ABBCCC'
   COMPRES$('ABBCCC','B'  )='ABCCC'
   COMPRES$('ABBCCC','C'  )='ABBC'
   COMPRES$('ABBCCC','ABC')='ABC'
        

         

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