Jump to content
Sign in to follow this  
c.haslam

[Solved] Static array redimensioned

Recommended Posts

In my script, an array is created and populated multiple times, and each time (for a run) it is populated with the same number of elements and the same contents. But on a subsequent run, the number of elements may be different. Execution of the script would be faster if the array were populated only once in a run.

The obvious way is to create and populate it once, store it as a static the first time in a run, and thereafter check whether it has been populated and if it has, return the value from the function rather than populating again.

So how do I do this?

Here's my attempt (in outline), which doesn't work.

static a10User[0]

If Ubound(a10User)=0 then
    Redim $a10User[50]
    ; populate it
    Redim $a10User[$n]
    Return $a10User
Else
    Return $a10User
Endif

 

Edited by c.haslam

Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Share this post


Link to post
Share on other sites

c.haslam,

Something like this seems to work:

#include <Array.au3>

; Array just to provide different array sizes
Global $aBounds[] = [4, 15, 26, 17]

For $i = 0 To UBound($aBounds) - 1

    ; Call func first time
    $aRet = _Func()
    _ArrayDisplay($aRet, "After first call", Default, 8)

    ; Call func a second time
    $aRet = _Func()
    _ArrayDisplay($aRet, "After second call", Default, 8)

    ;Call func to clear the array
    $aRet = _Func(1)
    If UBound($aRet) = 0 Then
        MsgBox($MB_SYSTEMMODAL, "Array", "Cleared")
    Else
        MsgBox($MB_SYSTEMMODAL, "Array", "NOT cleared")
    EndIf

Next

Func _Func($iClear = 0)

    Local Static $aArray[0]

    If $iClear= 1 Then
        ReDim $aArray[0]
        Return
    EndIf

    If Ubound($aArray) = 0 Then
        Redim $aArray[50]
        ; Populate it
        For $j = 0 To $aBounds[$i] - 1
            $aArray[$j] = $aBounds[$i]
        Next
        ReDim $aArray[$aBounds[$i]]
    EndIf

    ; You return the array in each case, so just do it once
    Return $aArray

EndFunc

You need to clear the array for each "run" as otherwise its Static nature means it will keep the same size.

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

 

Share this post


Link to post
Share on other sites

Melba23: Thank you, but the solution you propose is not the solution to my problem: between runs of my script:

$n is unknowable in advance. It is the number of sub-folders under a folder. A user might add a subfolder to the folder between runs of my script. So $n does not change during a run but may change between runs.

The cause of my problem is demonstrated by this snippet:

Func foo()
    Static Local $a10[0]

    If UBound($a10)=0 Then
        $a10 = [1,2,3]
    EndIf
    Return $a10
EndFunc

Au3Check says syntax error for the assignment statement.

It reports the same error if Static is removed.

This:

Func foo()
    Local $a10[0]

    If UBound($a10)=0 Then
        ReDim $a10[3]
        $a10 = [ 1,2,3]
    EndIf
    Return $a10
EndFunc

gives the same error.

I do not consider this to be a bug in AutoIt; rather my last snippet to me is perhaps a desirable feature.


Spoiler

CDebug Dumps values of variables including arrays and DLL structs, to a GUI, to the Console, and to the Clipboard

 

Share this post


Link to post
Share on other sites

c.haslam,

My code does not seem too far removed from what you appear to want. Here it is amended to have a random size for the array on each run:

#include <Array.au3>


; Each loop here is a "run"
For $i = 0 To 5

    ; Call func first time
    $aRet = _Func()
    _ArrayDisplay($aRet, "After first call", Default, 8)

    ; Call func a second time
    $aRet = _Func()
    _ArrayDisplay($aRet, "After second call", Default, 8)

    ;Call func to clear the array
    $aRet = _Func(1)
    If UBound($aRet) = 0 Then
        MsgBox($MB_SYSTEMMODAL, "Array", "Cleared")
    Else
        MsgBox($MB_SYSTEMMODAL, "Array", "NOT cleared")
    EndIf

Next

Func _Func($iClear = 0)

    Local Static $aArray[0]

    ; If the run is ended - clear the array so that the next call will set a new size
    If $iClear= 1 Then
        ReDim $aArray[0]
        Return
    EndIf

    If Ubound($aArray) = 0 Then
        
        ; Generate a random value to simulate the number of subfolders
        $iRandom = Random(1, 10, 1)
        ; Resize the array
        Redim $aArray[$iRandom]
        ; And populate it
        For $j = 0 To UBound($aArray) - 1
            $aArray[$j] = $iRandom
        Next
    EndIf

    ; You return the array in each case, so just do it once
    Return $aArray

EndFunc

M23

Edited by Melba23
Added missing words - interrupted by grandson!

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

 

Share this post


Link to post
Share on other sites

c.haslam,

Glad I could help.

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

 

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...