` Printed Icetips Article

Icetips Article



Par2: Dynamically Dimensioned Arrays
2002-05-25 -- Randy Goodhew
 
Don't dismiss Routines as useless.
They certainly have their place.
Here's an example that uses a Routine to
create Dynamically Dimensioned Arrays.
Normally the DIM() value must be a constant,
but in the context of a Routine, the DIM()
value can be a variable, e.g.:
----------------------
DatHash  PROCEDURE(STRING password)! ,STRING
Dim      LONG,AUTO                 ! Ushorts array dim
Sum      USHORT,AUTO               ! sum of Ushorts
SumBytes BYTE,DIM(2),OVER(Sum)     ! parse bytes from Ushort
Result   STRING(16),AUTO           ! two char password
  CODE
  Dim  = LEN(CLIP(password)) / 2   ! calc array dimensions
  DO R_Calc
  Result = '''<<' & SumBytes[1] & ',' & SumBytes[2] & '>'''
  RETURN(Result)                   ! string like '<99,88>'

R_Calc     ROUTINE
  DATA                             ! Routine-level dynamic data
Ushorts    USHORT,DIM(Dim),OVER(password)  ! dynamic array
J          LONG,AUTO               ! loop counter
  CODE
  Sum = 0                          ! init Sum
  LOOP J = 1 TO Dim                ! loop thru password as Ushorts
    Sum += Ushorts[J]              ! sum of Ushorts
  END !loop
  EXIT
------------------

BTW, this little algorithm will calculate the 16 bit Relative Password
Hash for a Clarion DAT file (if the password is known). 
Note: Regardless of how large a password is for a Clarion DAT file, 
they can be reduced to one of 64K possible two character clones.
Thus, the longer the password, the more coincidental dups there
will be, essentially one in every 64K of character combinations.

Routines are also useful in writing recursive algorithms, where
the Routine can call itself, i.e. classical QuickSort algorithm.



Printed May 4, 2024, 5:00 am
This article has been viewed/printed 35117 times.