Jump to content
Sign in to follow this  
vcent

Howto add things into an array

Recommended Posts

vcent

I still confuse about how to declare an array and assign values into it dynamically.

For $i = 1 to 10
        $NetBIOS = RegEnumKey("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Netbt\Parameters\Interfaces", $i)
            If @error <> 0 Then ExitLoop
                Local $NetBios_Array[$i]
                _ArrayAdd($NetBios_Array, $NetBIOS)
            ;_ArrayDisplay($NetBios_Array, "Display")
                
            Next

How do you add something into an array. In addition can you declare an array without knowing the max size ? How do you create an array dynamically.

Edited by vcent

Share this post


Link to post
Share on other sites
vcent

For $i = 1 to 10

$NetBIOS = RegEnumKey("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Netbt\Parameters\Interfaces", $i)

If @error <> 0 Then ExitLoop

Local $NetBios_Array

_ArrayAdd($NetBios_Array[$i], $NetBIOS)

Next

_ArrayDisplay($NetBios_Array, "Display")

Some errors. But still couldn't get to work

Share this post


Link to post
Share on other sites
Melba23

vcent,

Look at this:

#include <Array.au3>

; If you know how many elements there will be

Global $aArray[11]

For $i = 1 to 10
    $aArray[$i] = $i
    _ArrayDisplay($aArray, "Display")
Next

; If you do NOT know how many elements there will be

Global $aArray[1]

For $i = 1 To 10
    _ArrayAdd($aArray, $i)
    _ArrayDisplay($aArray, "Display")
Next

Can you see the difference? In the first loop your array already exists and you are filling it as you go along. In the second you are expanding your array as you go along.

The second loop will be very much slower because of the overhead of _ArrayAdd redimensioning the array each time you call it. It does not really matter for small arrays like these, but if you use BIG arrays it can get very noticeable. Look at the difference in times in this very similar code (do not get impatient for the second loop to finish - it does take a looooong time!):

#include <Array.au3>

Global $aArray[5001]
$begin = TimerInit()
For $i = 1 to 5000
    $aArray[$i] = $i
Next
ConsoleWrite(TimerDiff($begin) / 1000 & @CRLF)

Global $aArray[1]
$begin = TimerInit()
For $i = 1 To 5000
    _ArrayAdd($aArray, $i)
Next
ConsoleWrite(TimerDiff($begin) / 1000 & @CRLF)

I hope that helps.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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
vcent

THanks!

I managed to get some output.

Share this post


Link to post
Share on other sites
PsaltyDS

Perhaps you meant to do something more like this:

#include <Array.au3>

Global $NetBios_Array[1] = [0]
For $i = 1 To 10
    $NetBIOS = RegEnumKey("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Netbt\Parameters\Interfaces", $i)
    If @error <> 0 Then ExitLoop
    _ArrayAdd($NetBios_Array, $NetBIOS)
Next
$NetBios_Array[0] = UBound($NetBios_Array) - 1
_ArrayDisplay($NetBios_Array, "Display")

When you declare an array with Local/Global/Dim any previous contents get wiped out. So declaring it inside the loop keeps you from collecting all the results of RegEnumKey() because it gets wiped each time. This version declares the array only once, outside the loop.

RegEnumKey() does not return an array, so $NetBIOS[$i] was an invalid reference.

After the loop is done adding elements, Ubound() is used to set the first element to the count. Element [0] does not have to be the count, but it is a common usage.

:P


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.