Jump to content

Recommended Posts

Posted (edited)

So I was looking everywhere about getting an array's number of slots in use... and found nothing.

Is this even possible? for example

Global $array[5]

$array[1] = "1 slot in use"

$arrayslotsinuse = ArrayGetSlotsInUse($array)

Thanks in advance :)

...I tried making a counter but didn't work so well.. (it crashed app)

Edited by TheNewHunter

Hello.

If in someway I have helped, please consider liking my post(s).

The formula for the right answer: You + Right Question = Answer

  Reveal hidden contents

(Think) BEFORE you post.

  • Moderators
Posted

TheNewHunter,

UBound will tell you how many elements are in the array, but not how many are "in use" - often the coder uses the [0] element to keep a count as the array fills.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

Yea how many elements, great, thanks!

Hello.

If in someway I have helped, please consider liking my post(s).

The formula for the right answer: You + Right Question = Answer

  Reveal hidden contents

(Think) BEFORE you post.

Posted

If you're looking to include a counter, I would suggest creating a function for adding data to your array.

ReDim is your friend here! 

Example;

#Include<Array.au3> ;Used to display results (_ArrayDisplay)
local $array[1] ; Initialise the array

;Now lets add something to it
AddValueToArray($array,"Dave")
AddValueToArray($array,"Phil")
AddValueToArray($array,"Jake")
_ArrayDisplay($Array) ;Show Results
;Now lets remove Phil, But we want our counter and array to remain neat
RemValueFromArray($array,"Phil")
_ArrayDisplay($Array) ;Show Results



func AddValueToArray( byRef $iArray,$iValue)
    local $uBound = uBound($iArray,1) ;Get max element number of array
    reDim $iArray[$uBound + 1] ;Re Dimension the array, Without removing information
    $Array[$uBound] = $iValue ; Add the new value
    $Array[0] += 1 ; Increment the counter by 1
EndFunc

Func RemValueFromArray( byRef $iArray,$iValue)
    local $tArray = $iArray ;Store data in a temporary array
    Dim $iArray[1] ;Reset the array to empty (so we can rebuild it)
    for $i = 1 to $tArray[0] ;Loop through temporary array
        if $tArray[$i] = $iValue then ContinueLoop ;We've found the remove value! Lets continue the loop and ignore it.
        AddValueToArray($iArray,$tArray[$i]) ;The above line was false, So we can run the AddValueToArray Command to add the data
    Next ;Continue the for loop
EndFunc

Cheers

Javi

give a man an application, and he'll be frustrated for the day, Teach him how to program applications and he'll be frustrated for a lifetime.

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
×
×
  • Create New...