vcent Posted March 8, 2009 Share Posted March 8, 2009 (edited) 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 March 8, 2009 by vcent Link to comment Share on other sites More sharing options...
vcent Posted March 8, 2009 Author Share Posted March 8, 2009 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 More sharing options...
Moderators Melba23 Posted March 8, 2009 Moderators Share Posted March 8, 2009 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") NextCan 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 Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
vcent Posted March 8, 2009 Author Share Posted March 8, 2009 THanks! I managed to get some output. Link to comment Share on other sites More sharing options...
PsaltyDS Posted March 9, 2009 Share Posted March 9, 2009 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. 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now