VLU Trim To Most Significant Digit

Removes leading array elements that contain zero.

To include only the trim function in a DLL directly from this source file put the following lines in the DLL source:

%In_DLL = 1

#INCLUDE "MSD_VLUTrim.bas" 
The #IF will skip the PBMAIN() for you.

Source Code

The function TrimVLU_to_MSD should compile in PowerBASIC For Windows (PBWin) Versions 8, 9 or 10 and PowerBASIC Console Compiler (PBCC) Versions 4, 5 or 6. The DEC$() function is new in PBWin 10 and PBCC 6, for earlier versions change to STR$() or FORMAT$(). All PBCC versions will ignore $$CRLF and wrap to next line at console width.


'file name: MSD_VLUTrim.bas

'for stand alone testing

#if not %def(%In_DLL)

  #compile exe

  #dim all

  function pbmain () as long

    local TestArray1(), TestArray2(), TestArray3() as dword

    local R0, R1, UB1, R2, UB2, R3, UB3 as long

    'test undimensioned  - - - - - - - - - - - - - - - - -

    R0 = TrimVLU_to_MSD(TestArray1())

    'test two leading zero elements  - - - - - - - - - - -

    dim TestArray1(4)

    TestArray1(2) = 321

    R1 = TrimVLU_to_MSD(TestArray1())

    UB1 = ubound(TestArray1())

    'test ubound non-zero (no change needed)  - - - - - - -

    dim TestArray2(4)

    TestArray2(4) = 1

    R2 = TrimVLU_to_MSD(TestArray2())

    UB2 = ubound(TestArray2())

    'test all zero elements  - - - - - - - - - - - - - - - -

    dim TestArray3(4)

    R3 = TrimVLU_to_MSD(TestArray3())

    UB3 = ubound(TestArray3())

    ? dec$(R0) + " should be -2" + $$crlf + _

      dec$(R1) + dec$(UB1, 0, 1, 1) + "should be 0 2" + $$crlf + _

      dec$(R2) + dec$(UB2, 0, 1, 1) + "should be 0 4" + $$crlf + _

      dec$(R3) + dec$(UB3, 0, 1, 1) + "should be -1 0"

  end function

#endif

'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

'Function to be included in DLL.

function TrimVLU_to_MSD alias "TrimVLUtoMSD" (byref VLU_Num() as dword) export _

                                                                         as long

  local UpBnd, CurDig as long 'vars to hold upper bound and current digit

  UpBnd = ubound(VLU_Num)

  if UpBnd = -1 then 'test for undimensioned array, also see the web page.

    function = -2    'error code for undimensioned array

    exit function

  end if

  if VLU_Num(UpBnd) then

    exit function 'ubound is already MSD, not a leading zero

  else 'at least one leading 0

    for CurDig = UpBnd - 1 to 0 step -1

      if VLU_Num(CurDig) then

        redim preserve VLU_Num(CurDig)

        exit function

      end if

    next

  end if

  redim VLU_Num(0)

  function = -1 'error code for all digits 0.

end function


Created on 21 December 2021; last edit 21 Dec 2021.