Jump to content

Get array's number of slots in use?


Recommended Posts

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

(Think) BEFORE you post.

Link to comment
Share on other sites

  • Moderators

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:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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.

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
 Share

×
×
  • Create New...