Jump to content
Sign in to follow this  
ghetek

need help managing an array

Recommended Posts

ghetek

hey can somebody give me some input on my array cleanup?

update() doesnt seem to update the $users array. I grabbed that function from another thread. Yes I HAVE searched the forums and yes i have seen arrayunique.

#include <Constants.au3>
#Include <Array.au3>
#NoTrayIcon

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1)

Global $sockserver, $sockclient, $recv, $users[2], $time

$codemsg        = "#01"
$codeupdate  = "#02"
$updateinterval = 5000
$port          = 65335
$iplocal        = @IPAddress1
$users[0]      = 1
$users[1]      = $iplocal
$aDivIP      = StringSplit($iplocal, ".")
$ipremote      = $aDivIP[1] & "." & $aDivIP[2] & "." & $aDivIP[3] & "." & "255"

$sendmsg = TrayCreateItem("Broadcast")
    TrayItemSetOnEvent(-1, "sendmsg")
$display = TrayCreateItem("Display Hosts")
    TrayItemSetOnEvent(-1, "display")
$quit = TrayCreateItem("Quit")
    TrayItemSetOnEvent(-1, "quit")
TraySetState()

UDPStartup()

$sockserver = UDPBind($iplocal, $port)
Sleep(1000)
$sockclient = UDPOpen($ipremote, $port)
While 1
    $recv=UDPRecv($sockserver, 50)
    Select
        Case $recv <> ""
            $code=StringLeft ($recv,3)
            $data=StringTrimLeft($recv, 3)
            Select
                Case $code = $codemsg And $data <> ""; message recieved, it is NOT empty. show message box
                    MsgBox(0,"",$data,3)
                Case $code = $codeupdate And $data <> $iplocal; update revieved, add to and sort in array
                    update($users, $data)
            EndSelect
    EndSelect
WEnd

Func sendmsg()
    UDPSend($sockclient,$codemsg & InputBox("What to broadcast?", "text:", ""))
EndFunc  ;==>sendmsg

Func update($str,$newuser)
    Local $count, $final = "", $split = StringSplit($str, ",")
    For $X = 1 To UBound($split) - 1
        $count = ""
        For $i = $X To UBound($split) - 1
            If StringInStr($final, $split[$i]) Then ContinueLoop
            If StringInStr($split[$X], $split[$i]) Then $count += 1
        Next
        If $count >= 1 Then $final &= ",[" & $count & "] " & $split[$X]
    Next
    $updatedarray   = _ArraySort (StringTrimLeft($final, 1))
    $updatedarray[0] = UBound ($updatedarray)
    Return _ArraySort (StringTrimLeft($updatedarray, 1))
EndFunc

Func display()
    _ArrayDisplay($users)
EndFunc

Func quit()
    UDPCloseSocket($sockserver);works ok?
    UDPShutdown()
    Exit
EndFunc  ;==>quit

is there any simple way to do the following?:

remove duplicates from an array

count the number of elements in the array and update $array[0]

sort the array (_arraysort)

Share this post


Link to post
Share on other sites
BrettF

Share this post


Link to post
Share on other sites
Authenticity

The update function need to be declared as accepting variables ByRef like:

Func Update(ByRef $aUsers, $sNewUser = 'SomeDefaultValue')
 
    ReDim $aUsers[UBound($aUsers)+1]
   $aUsers[UBound($aUsers)-1] = $sNewUser
EndFunc

- Removing duplicates from the array is a simple task, just iterate and compare the 1 with the rest in the first iteration (inner and outer loops for single dimension), 2 with the rest in the second iteration, etc..

- Counting the array size for 1 or 2 dimensional arrays can be done using UBound() function. About storing the subscript count in the first subscript it can be done using _ArrayInsert once and modifying it's value using the result of UBound().

- Isn't _ArraySort simple enough for you?

Share this post


Link to post
Share on other sites
ghetek

OK, I've made all the suggested changes but for some reason I still get duplicates in the _ArrayDisplay. I decided to go with _ArrayFindAll in my update function instead of writing nested for-next loops.

#include <Constants.au3>
#Include <Array.au3>
#NoTrayIcon

Opt("TrayOnEventMode", 1)
Opt("TrayMenuMode", 1)

Global $sSockServer, $sSockClient, $sRecvData, $aUsers[2]

$sCodeMsg       = "#01"
$sCodeUpdate     = "#02"
$updateinterval = 5000; still need to implement
$sPort         = 65335
$sIPLocal       = @IPAddress1
$aUsers[0]     = 1
$aUsers[1]     = $sIPLocal
$aDivIP      = StringSplit($sIPLocal, ".")
$sIPRemote     = $aDivIP[1] & "." & $aDivIP[2] & "." & $aDivIP[3] & "." & "255"

UDPStartup()

$sSockServer = UDPBind($sIPLocal, $sPort)
Sleep(1000)
$sSockClient = UDPOpen($sIPRemote, $sPort)

$sendmsg = TrayCreateItem("Broadcast")
    TrayItemSetOnEvent(-1, "sendmsg")
$sendupdate = TrayCreateItem("Send Update")
    TrayItemSetOnEvent(-1, "sendupdate")
$display = TrayCreateItem("Display Hosts")
    TrayItemSetOnEvent(-1, "display")
$quit = TrayCreateItem("Quit")
    TrayItemSetOnEvent(-1, "quit")
TraySetState()


While 1
    $sRecvData=UDPRecv($sSockServer, 50)
    Select
        Case $sRecvData <> ""
            $sLiveCode=StringLeft ($sRecvData,3)
            $sData=StringTrimLeft($sRecvData, 3)
            Select
                Case $sLiveCode = $sCodeMsg And $sData <> ""; message recieved, it is NOT empty. show message box
                    MsgBox(0,"",$sData,3)
                Case $sLiveCode = $sCodeUpdate And $sData <> $sIPLocal; update revieved, add to and sort in array
                    update($aUsers, $sData)
            EndSelect
    EndSelect
WEnd

Func sendmsg()
    UDPSend($sSockClient,$sCodeMsg & InputBox("What to broadcast?", "text:", ""))
EndFunc  ;==>sendmsg

Func update(ByRef $aUsers,$sNewUser)
    _ArraySort($aUsers) 
    $sNewUserDuplicate = _ArrayFindAll($aUsers,$sNewUser)
        For $i=0 to UBound($sNewUserDuplicate,2)
            _ArrayDelete($aUsers,$i)
        Next
        _ArrayAdd($aUsers,$sNewUser)
        ReDim $aUsers[UBound($aUsers)+1]
        $aUsers[UBound($aUsers)-1] = $sNewUser
EndFunc

Func sendupdate()
    UDPSend($sSockClient,$sCodeUpdate & $sIPLocal)
EndFunc

Func display()
    _ArrayDisplay($aUsers)
EndFunc

Func quit()
    UDPCloseSocket($sSockServer);works ok?
    UDPShutdown()
    Exit
EndFunc  ;==>quit

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  

×