OrderedArray() Invalid

Get help with using AutoHotkey and its commands and hotkeys
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

OrderedArray() Invalid

15 Mar 2014, 23:20

OrderedArray() Because "static" Command failure。
http://www.autohotkey.com/board/topic/9 ... %20Rbrtryn

Invalid

Code: Select all

Status()
Status(){
static obj:=OrderedArray()
obj.3:=3
obj.1:=1
obj.2:=2
For k, v in obj
    s .= k "=" v "`n"
MsgBox % s
}
Effective

Code: Select all

Status()
Status(){
obj:=OrderedArray()
obj.3:=3
obj.1:=1
obj.2:=2
For k, v in obj
    s .= k "=" v "`n"
MsgBox % s
}
OrderedArray

Code: Select all

; OrderedArray code by Lexikos
; Modifications and additional methods by rbrtryn
; http://tinyurl.com/lhtvalv
OrderedArray(prm*)
{
    ; Define prototype object for ordered arrays:
    static base := Object("__Set", "oaSet", "_NewEnum", "oaNewEnum"
                        , "Remove", "oaRemove", "Insert", "oaInsert", "InsertBefore", "oaInsertBefore")
    ; Create and return new ordered array object:
    return Object("_keys", Object(), "base", base, prm*)
}

oaSet(obj, prm*)
{
    ; If this function is called, the key must not already exist.
    ; Sub-class array if necessary then add this new key to the key list
    if prm.maxindex() > 2
        ObjInsert(obj, prm[1], OrderedArray())
    ObjInsert(obj._keys, prm[1])
    ; Since we don't return a value, the default behaviour takes effect.
    ; That is, a new key-value pair is created and stored in the object.
}

oaNewEnum(obj)
{
    ; Define prototype object for custom enumerator:
    static base := Object("Next", "oaEnumNext")
    ; Return an enumerator wrapping our _keys array's enumerator:
    return Object("obj", obj, "enum", obj._keys._NewEnum(), "base", base)
}

oaEnumNext(e, ByRef k, ByRef v="")
{
    ; If Enum.Next() returns a "true" value, it has stored a key and
    ; value in the provided variables. In this case, "i" receives the
    ; current index in the _keys array and "k" receives the value at
    ; that index, which is a key in the original object:
    if r := e.enum.Next(i,k)
        ; We want it to appear as though the user is simply enumerating
        ; the key-value pairs of the original object, so store the value
        ; associated with this key in the second output variable:
        v := e.obj[k]
    return r
}

oaRemove(obj, prm*)
{
    r := ObjRemove(obj, prm*)         ; Remove keys from main object
    Removed := []                     
    for k, v in obj._keys             ; Get each index key pair
        if not ObjHasKey(obj, v)      ; if key is not in main object
            Removed.Insert(k)         ; Store that keys index to be removed later
    for k, v in Removed               ; For each key to be removed
        ObjRemove(obj._keys, v, "")   ; remove that key from key list
    return r
}

oaInsert(obj, prm*)
{
    r := ObjInsert(obj, prm*)            ; Insert keys into main object
    enum := ObjNewEnum(obj)              ; Can't use for-loop because it would invoke oaNewEnum
    while enum[k] {                      ; For each key in main object
        if (k = "_keys")
            continue 
        for i, kv in obj._keys           ; Search for key in obj._keys
            if (k = kv)                  ; If found...
                continue 2               ; Get next key in main object
        ObjInsert(obj._keys, k)          ; Else insert key into obj._keys
    }
    return r
}

oaInsertBefore(obj, key, prm*)
{
    OldKeys := obj._keys                 ; Save key list
    obj._keys := []                      ; Clear key list
    for idx, k in OldKeys {              ; Put the keys before key
        if (k = key)                     ; back into key list
            break
        obj._keys.Insert(k)
    }
    
    r := ObjInsert(obj, prm*)            ; Insert keys into main object
    enum := ObjNewEnum(obj)              ; Can't use for-loop because it would invoke oaNewEnum
    while enum[k] {                      ; For each key in main object
        if (k = "_keys")
            continue 
        for i, kv in OldKeys             ; Search for key in OldKeys
            if (k = kv)                  ; If found...
                continue 2               ; Get next key in main object
        ObjInsert(obj._keys, k)          ; Else insert key into obj._keys
    }
    
    for i, k in OldKeys {                ; Put the keys after key
        if (i < idx)                     ; back into key list
            continue
        obj._keys.Insert(k)
    }
    return r
}
User avatar
RobertL
Posts: 546
Joined: 18 Jan 2014, 01:14
Location: China

Re: OrderedArray() Invalid

15 Mar 2014, 23:55

Code: Select all

;Chang:
;static obj:=OrderedArray()
;to:
static obj
obj:=OrderedArray()
;Or #include <OrderedArray> before use OrderedArray()
From AHK help(chm):(When I use spoiler, its content is empty?)
Static Initializers: In versions prior to 1.0.46, all static variables started off blank; so the only way to detect that one was being used for the first time was to check whether it was blank. In v1.0.46+, a static variable may be initialized to something other than "" by following it with := or = followed by one of the following: true, false, a literal integer, a literal floating point number, or a literal/quoted string such as "fox". For example: static X:=0, Y:="fox". Each static variable is initialized only once (before the script begins executing).

Initialize of Static Var in Function begins when loading the script (before running the first line of code).
If you use a return of function which contains static var to initialize a outer static var in an outside function, it must follow the order as initialize of static var(from top to bottom).


Example:

Code: Select all

;Unexpect result:
MsgBox % f1()
f1(){
	static a:=f2()
	return a
}
f2(){
	static b:=2
	return	b
}
;correct:
MsgBox % f4()
f3(){
	static a:=1
	return a
}
f4(){
	static b:=f3()
	return	b
}
Last edited by RobertL on 16 Mar 2014, 00:27, edited 1 time in total.
我为人人,人人为己?
arcticir
Posts: 693
Joined: 17 Nov 2013, 11:32

Re: OrderedArray() Invalid

16 Mar 2014, 00:25

Thanks.

Answer:
#include <OrderedArray>

Return to “Ask For Help”

Who is online

Users browsing this forum: arczi_87, Google [Bot], Green Astronaut, jwinfl, mikeyww, Mokardder, Smorgydorg, yousef_badr23 and 60 guests