Sign in to follow this  
Followers 0
Hellouser

How to get the maximum index value of an array in autoit. ?

12 posts in this topic

Hi All

How to get the maximum index value of an array in autoit. ?

Share this post


Link to post
Share on other sites



Thanks KuFu for the reply.

I am having a 2 diamentional array .When i tried the below code i am getting the ubound value as 5 instead of 45.

Can u guide me how to correct it.

#include <Array.au3>

Local $avArray[90][5]

For $i =0 to 45

For $j =0 to 4

$avArray[$i][$j] = $i

Next

Next

_ArrayDisplay($aiResult, UBound($avArray,2))

Share this post


Link to post
Share on other sites

Thanks KuFu for the reply.

I am having a 2 diamentional array .When i tried the below code i am getting the ubound value as 5 instead of 45.

Can u guide me how to correct it.

#include <Array.au3>

Local $avArray[90][5]

For $i =0 to 45

For $j =0 to 4

$avArray[$i][$j] = $i

Next

Next

_ArrayDisplay($aiResult, UBound($avArray,2))

Local $avArray[90][5]

Ubound($avArray,0) = 2

Ubound($avArray,1) = 90

Ubound($avArray,2) = 5

Share this post


Link to post
Share on other sites

Change _ArrayDisplay($avArray, UBound($avArray,2)) to _ArrayDisplay($avArray, UBound($avArray,1))

And it will show you 90 as you have created the array with 90 elements , no matter only 45 are filled with data.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Here ya go...

#include <array.au3>

Local $avArray[90][5]
For $i = 0 To 45
    For $j = 0 To 4
        $avArray[$i][$j] = $i
    Next
Next

MsgBox(0,"",UBound($avArray,1) & @crlf & UBound($avArray,2))
_ArrayDisplay($avArray,"ArrayDisplay()",UBound($avArray,1)-20) ; -20 as an example only
Edited by KaFu

Share this post


Link to post
Share on other sites

Thanks Everybody for the reply.

Here my intention is to get the maximum index value of the array(ie 45 as per the above program).Not the actual size of the array.The above array is filled with 45 values , i want to get the result as 45.How can it be possible. ?

Thanks.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Only by enumarating the array... presumes of course that there are no more values after the first empty was found...

#include <array.au3>

Local $avArray[90][5]
For $i = 0 To 45
    For $j = 0 To 4
        $avArray[$i][$j] = $i
    Next
Next

MsgBox(0,"",UBound($avArray,0) & @crlf & UBound($avArray,1) & @crlf & UBound($avArray,2))
_ArrayDisplay($avArray,"Display max array-rows -20",UBound($avArray,1)-20) ; -20 as an example only

for $i = 0 to UBound($avArray)-1
    if stringlen($avArray[$i][0]) = 0 then
        $first_empty = $i
        MsgBox(0,"","First empty value: " & $first_empty)
        ExitLoop
    EndIf
Next

_ArrayDisplay($avArray,"Display only array rows until first empty value found",$first_empty-1)

_ArraySort($avArray,1)
ReDim $avArray[$first_empty][UBound($avArray,2)]
_ArraySort($avArray,0)

_ArrayDisplay($avArray,"Display whole array after ReDim")

Edit: Maybe a sort and ReDim would be the best solution... updated example.

Edited by KaFu

Share this post


Link to post
Share on other sites

It is possible by creating small size array - [1][1] and then dynamically increasing the size of the array using the "Redim" in the loops

For example :

Local $avArray[1][1]
For $i = 0 To 45
    For $j = 0 To 4
    Redim $avArray[$i+1][$j+1]
        $avArray[$i][$j] = $i
    Next
Next

MsgBox(0,"",UBound($avArray,1)-1 & @crlf & UBound($avArray,2)-1)
exit

Share this post


Link to post
Share on other sites

Aehhmmmm, okay, the first sort should be before the enmuaration...

#include <array.au3>

Local $avArray[90][5]
For $i = 0 To 45
    For $j = 0 To 4
        $avArray[$i][$j] = $i
    Next
Next

_ArraySort($avArray,1)
for $i = 0 to UBound($avArray)-1
    if stringlen($avArray[$i][0]) = 0 then
        $first_empty = $i
        ExitLoop
    EndIf
Next
ReDim $avArray[$first_empty][UBound($avArray,2)]
_ArraySort($avArray,0)

MsgBox(0,"",UBound($avArray,0) & @crlf & UBound($avArray,1) & @crlf & UBound($avArray,2))
_ArrayDisplay($avArray,"Display whole array after ReDim")

Share this post


Link to post
Share on other sites

It is possible by creating small size array - [1][1] and then dynamically increasing the size of the array using the "Redim" in the loops

That's true, but rediming every instances makes the process sloooow. I use something like this all the time, always add a buffer of 100 (or larger) on redim and after the loopp cut it down to the right size again:

; ReDim every instance
Local $avArray[1][1]
$timer = TimerInit()
For $i = 0 To 1000
    For $j = 0 To 4
    Redim $avArray[$i+1][$j+1]
        $avArray[$i][$j] = $i
    Next
Next
MsgBox(0,"","Created" & @crlf & UBound($avArray,1)-1 & @crlf & UBound($avArray,2)-1 & @crlf & "in " & TimerDiff($timer) & " ms")

; ReDim every 100's instance
ReDim $avArray[1][1]
$timer = TimerInit()
For $i = 1 To 1000
    $avArray[0][0] = $i
    if UBound($avArray) = $avArray[0][0] then ReDim $avArray[UBound($avArray)+100][5]
    For $j = 0 To 4
        $avArray[$i][$j] = $i
    Next
Next
ReDim $avArray[$i][5]
MsgBox(0,"","Created" & @crlf & UBound($avArray,1)-1 & @crlf & UBound($avArray,2)-1 & @crlf & "in " & TimerDiff($timer) & " ms")

Share this post


Link to post
Share on other sites

Agreed , This is quite faster...

Local $avArray[1000][1000]

For $i = 0 To 427

For $j = 0 To 678

$avArray[$i][$j] = $i

Next

Next

Redim $avArray[$i][$j]

MsgBox(0,"",UBound($avArray,1)-1 & @crlf & UBound($avArray,2)-1)

exit

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  
Followers 0