Jump to content

Howto add things into an array


Recommended Posts

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Moderators

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

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

 

Link to comment
Share on other sites

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
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...