|
|||||||
Массивы / Arrays
Время создания: 14.04.2017 09:56
Текстовые метки: code
Раздел: AutoHotkey - gui - Типы - Массивы / Arrays
Запись: xintrea/mytetra_db_mcold/master/base/149214919806y4phe555/text.html на raw.githubusercontent.com
|
|||||||
|
|||||||
Array := {Key1: "Value1", Key2: "Value2"} Array.Insert("Value3") For k, v in Array ; последовательный перебор членов массива { MsgBox, % Array[k] MsgBox, % v } Self-contained associative arrays can be created by calling Object() . For example: ; Create the array, initially empty: Array := Object() ; Write to the array: Loop, Read, C:\Guest List.txt ; This loop retrieves each line from the file, one at a time. { Array.Insert(A_LoopReadLine) ; Append this line to the array. } ; Read from the array: ; Loop % Array.MaxIndex() ; More traditional approach. for index, element in Array ; Recommended approach in most cases. { ; Using "Loop", indices must be consecutive numbers from 1 to the number ; of elements in the array (or they must be calculated within the loop). ; MsgBox % "Element number " . A_Index . " is " . Array[A_Index]
; Using "for", both the index (or "key") and its associated value ; are provided, and the index can be *any* value of your choosing. MsgBox % "Element number " . index . " is " . element } This shows only a small subset of the functionality provided by objects . Items can be set, retrieved, inserted, removed and enumerated. Strings and objects can be used as keys in addition to numbers. Objects can be stored as values in other objects and passed as function parameters or return values. Objects can also be extended with new functionality. Though Insert() and enumerators have their uses, some users might find it easier to use the more traditional approach: ; Each array must be initialized before use: Array := Object() ; Array%j% := A_LoopField Array[j] := A_LoopField ; Array%j%_%k% := A_LoopReadLine Array[j, k] := A_LoopReadLine ArrayCount := 0 Loop, Read, C:\Guest List.txt { ArrayCount += 1 ; Array%ArrayCount% := A_LoopReadLine Array[ArrayCount] := A_LoopReadLine } Loop %ArrayCount% { ; element := Array%A_Index% element := Array[A_Index] ; MsgBox % "Element number " . A_Index . " is " . Array%A_Index% MsgBox % "Element number " . A_Index . " is " . Array[A_Index] } ArrayCount is left as a variable for convenience, but can be stored in the array itself with Array.Count := n or it can be removed and Array._MaxIndex() used in its place. If a starting index other than 1 is desired, Array._MinIndex() can also be used. Pseudo-arrays are mostly conceptual: Each array is really just a collection of sequentially numbered variables or functions , each one being perceived as an element of the array. AutoHotkey does not link these elements together in any way. In addition to array-creating commands like StringSplit and "WinGet List ", any command that accepts an OutputVar or that assigns a value to a variable can be used to create an array. The simplest example is the assignment operator (:=) , as shown below: Array%j% := A_LoopField Multidimensional arrays are possible by using a separator character of your choice between the indices. For example: Array%j%_%k% := A_LoopReadLine The following example demonstrates how to create and access an array, in this case a series of names retrieved from a text file: ; Write to the array: ArrayCount = 0 Loop, Read, C:\Guest List.txt ; This loop retrieves each line from the file, one at a time. { ArrayCount += 1 ; Keep track of how many items are in the array. Array%ArrayCount% := A_LoopReadLine ; Store this line in the next array element. } ; Read from the array: Loop %ArrayCount% { ; The following line uses the := operator to retrieve an array element: element := Array%A_Index% ; A_Index is a built-in variable. ; Alternatively, you could use the "% " prefix to make MsgBox or some other command expression-capable : MsgBox % "Element number " . A_Index . " is " . Array%A_Index% } A concept related to arrays is the use of NumPut() and NumGet() to store/retrieve a collection of numbers in binary format. This might be helpful in cases where performance and/or memory conservation are important. |
|||||||
Так же в этом разделе:
|
|||||||
|
|||||||
|