Jump to content

Script that adds ReDim functionality


Recommended Posts

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
    $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]
    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  
    FOR $i = 0 TO $nLowest-1; FOR loop to copy the contents
  $aTmp[$i] = $array[$i]
    $array = $aTmp; Set the original array to the newly resized array
Link to comment
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...