Jump to content

How do i find out the free space available in a 2D array


 Share

Recommended Posts

Hi All,

I have a 2D array with maximum users of 10000 ($maxuser[10000][6]) ,I am writing a network program where all the clients will connect to the server application.Server application has a capacity of 10000 users. When the clients are start connecting to the server it will automatically increment the array index value and push the values into the respective array locations.When the same users is again trying to connect again ,everytime i am checking the array whether any values are present with the same data.If the data is present there then the program will delete the previous data and it will add the new data..

Here the problem i am facing is , Every time i need run one additional loop to check for any previous data .This is reducing the program performance.Can it possible to get the maximum index of the array ? .So that i can start the loop from 1 to Maxindex. Or If there any possibility to get the number of vacant spaces in the array ? or Can anyone modify the below code.I tried all the possibilities but i am not able to figure it out.

i am not able to sort the array also.Because some parameter are required for future processing.

Please Help.!!

Func _DelSocket($Monitoring_Ip)

For $x = 0 To $MaxUsers

If $User_List[$x][1] = $Monitoring_Ip Then

$User_List[$x][0] = ""

$User_List[$x][1] = ""

$User_List[$x][2] = ""

$User_List[$x][3] = ""

$User_List[$x][4] = ""

$User_List[$x][5] = ""

EndIf

Next

EndFunc

Link to comment
Share on other sites

Usually you don't predefine the size of the array. You use ReDim to extend the array everytime you need a new entry.

Global $User_List[1][2] = [[0,0]]

; Test if the IP exists in the array
$bFound = 0
For $iCount = 1 To $User_List[0][0]
    If $User_List[$iCount][0] = $Monitoring_Ip Then
        $User_List[$iCount][1] = "Your user value"
        $bFound = 1
    EndIf
Next

; IP doesn't exist in the array, append
If $bFound = 0 Then
    ReDim $User_List[UBound($User_List,1)+1][UBound($User_List,2)]
    $iCount = UBound($User_List,1)-1
    $User_List[$iCount][0] = $Monitoring_Ip
    $User_List[$iCount][1] = "Your user value"
    $User_List[0][0] = $iCount
EndIf
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I've expanded the script a bit. If you don't need an IP-address any more in your array free the space so it can be reused. In my example I simply replace the IP-address with space.

If speed matters I would search the forum for binary trees.

#include <array.au3>
Global $User_List[1][2] = [[0,0]]
Global $IP_Table[5] = ["10.11.12.13","10.99.120.13","10.101.12.13","10.11.127.13","10.11.12.130"]
Global $bDelete = 0

; Insert all values into the array
For $i = 1 To 5
    $Monitoring_Ip = $IP_Table[$i-1]
    _InsertArray($Monitoring_Ip,$i,$bDelete)
Next
_ArrayDisplay($User_List)

; Delete 2nd entry
_InsertArray("10.99.120.13","",1)
_ArrayDisplay($User_List)

; Insert new entry. It uses the free entry
_InsertArray("10.22.12.130","XX",0)
_ArrayDisplay($User_List)

Func _InsertArray($Ip, $Value, $Delete)

    ; Test if the IP exists in the array
    $bFound = 0
    For $iCount = 1 To $User_List[0][0]
        If $User_List[$iCount][0] = $IP Then
            If $Delete Then
                $User_List[$iCount][0] = "" ; Delete a value = set the first entry of the row to ""
            Else
                $User_List[$iCount][1] = $Value
            EndIf
            $bFound = 1
        ElseIf $User_List[$iCount][0] = "" Then
            $User_List[$iCount][0] = $IP
            $User_List[$iCount][1] = $Value
            $bFound = 1
        EndIf
        If $bFound Then ExitLoop
    Next

    ; IP doesn't exist in the array, append
    If $bFound = 0 Then
        ReDim $User_List[UBound($User_List,1)+1][UBound($User_List,2)]
        $iCount = UBound($User_List,1)-1
        $User_List[$iCount][0] = $IP
        $User_List[$iCount][1] = $Value
        $User_List[0][0] = $iCount
    EndIf

EndFunc

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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

  • Recently Browsing   0 members

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