Sign in to follow this  
Followers 0
Valik

Script that adds ReDim functionality

1 post in this topic

Since Jon hasn't added ReDim functionality into AutoIt3 yet, I had to roll my own. Should be straight forward to use. Arguments are as follows:

Argument 1: Variable - If it is an array, it will be resized copying as many elements as possible (Shrinking an array will cause truncation, growing an array preserves all elements). If used on a variable that isn't an array, it makes the variable an array of the size specified.

Argument 2: New size of the array.

Here's the function, beware word wrap as usual. (It's named AReDim just to avoid any errors or conflict that might arise when Jon introduces ReDim keyword)

FUNC AReDim(BYREF $array, $nNewSize)
    IF $nNewSize < 1 THEN; Invalid size, set @error and return
  SetError(1)
  RETURN
    ENDIF
    $nArraySize = UBound($array); Get the size of the array
    IF $nArraySize < 1 THEN; It's not an array, so make it an array and return
  DIM $array[$nNewSize]
  RETURN
    ENDIF
    DIM $aTmp[$nNewSize]; Temporary array used to store elements
    LOCAL $nLowest; The most elements both arrays can have in common, prevents overflow in FOR loop
    IF $nArraySize < $nNewSize THEN; New size is bigger
  $nLowest = $nArraySize
    ELSE; New size is smaller, truncation will ocurr...
  $nLowest = $nNewSize  
    ENDIF
    FOR $i = 0 TO $nLowest-1; FOR loop to copy the contents
  $aTmp[$i] = $array[$i]
    NEXT
    $array = $aTmp; Set the original array to the newly resized array
ENDFUNC

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0