Answer to this post:
link.
kidbit wrote:So when I fulfill my array of arrays (which is an Object, right?)
Yes, in the example above,
processesSnapshot is an object (if you do
processesSnapshot := {} before the
for loop). The object whose _values_ store references to other objects. The array is really a special kind of object (a special kind of object that only has _numeric_ keys). Internally, the object and the array are the same thing.
kidbit wrote:
But
can I somehow omit specifying indexes ("A_Index" for 1st array and "indexScripts" for 2nd)
?
The documentation contradicts self a little bit:
since "Index" is not an attribute in square brackets - it is mandatory.
That's quite confusing, since "Index" is not anyways used in cases Object.Insert(Value) and Object.Insert(Key, Value), so it should be applicable for case 1, which is Object.Insert(Index, Value1 [, Value2, ... ValueN ]), which is exactly my case.
The
.Insert() method can be used in three different 'forms'. Those forms are:
Code: Select all
Object.Insert(Index, Value1 [, Value2, ... ValueN ])
Object.Insert(Value)
Object.Insert(Key, Value)
Depending on the number of parameters / the _type_ of the first parameter in the particular function call, a given form of
.Insert() is invoked. I.e. if the first parameter is a number, then either of those forms might be invoked:
Code: Select all
Object.Insert(Index, Value1 [, Value2, ... ValueN ])
Object.Insert(Value)
(for the second form, the
Value doesn't have to be a number, _but_ there has to be only one parameter passed to
.Insert()).
As for the ambiguity, I think this is what the documentation meant. In this syntax:
Code: Select all
Object.Insert(Index, Value1 [, Value2, ... ValueN ])
Index is _always_ mandatory, because this fails:
Code: Select all
obj := {}
obj.Insert("a", "b", "c") ; no Index
msgbox, % obj.a ; ""
=> inserting multiple values at the end of the array through one
.Insert() call, if the user hasn't specified the index, is not supported.
If
Index _is omitted_, then the only valid
.Insert() call is:
_in which case_ (=> not in the case of
.Insert(Index, Value1, *))
Index defaults to
MaxIndex()="" ? 1 : MaxIndex() + 1.
I guess
.Insert("a", "b", "c") is not supported because it would introduce too much ambiguity between:
.Insert(Value, Value1) and
.Insert(Key, Value), which might have two separate meanings:
- insert Value and Value1 at the end of the array
- insert Key with value Value
My thoughts on this:
And, in the future, if named parameters are supported for method calls, a syntax like this one would be ambiguous:
obj.Insert(params*), because one would have to check, how many keys are in the params object, to know, which form of .Insert() would be called. Hmm, actually, it wouldn't because params would look like this?: params := {Index: 3, Value1: {}, Value2: "Hello", Value3: "Chicken"}. But in case of variadic function calls, if would be ambiguous:
params := ["one", "two", "three"]
obj := {}, Obj.Insert(params*)
If params* contains only one key-value pair then .Insert(Index, Value) is ambiguous with .Insert(Key, Value).
Another thing is that ObjInsert() is a built-in function? and yet skipping Insert doesn't work:
Code: Select all
obj := {}, obj[1] := "3"
ObjInsert(obj, , "a", "b", "c")
for k, v in obj
o .= (A_Index != 1 ? "`n" : "")
. "k = " (IsObject(k) ? ("<Object>, n = " (ObjHasKey(k, n) ? : k.n ? "<no_name>")) : k)
. "`tv = " (IsObject(v) ? ("<Object>, n = " (ObjHasKey(v, n) ? : v.n ? "<no_name>")) : v)
MsgBox, % o ; 1: "3"
Note the word _Optional_ [parameters] in the quote:
Changelog wrote:1.1.12.00 - August 14, 2013
Optional parameters can be omitted by writing two consecutive commas, as in InStr(a, b,, 2). Unlike previous versions, this now works for objects (including COM objects) and built-in functions. [a,,b] can be used to create a sparse array.
Index is not an _optional_ parameter.
In case of this method call:
Code: Select all
processesSnapshot.Insert(A_Index, {"pid": Process.ProcessId, "exe": Process.ExecutablePath, "cmd": Process.CommandLine})
the variable
A_Index contains _a number_ and _two or more parameters_ are passed to
.Insert() (two or more parameter, w/o the implicit 'this' parameter), thus _the first_ form of
.Insert() is invoked in this case.
kidbit wrote:
Or it isn't and my case is actually Object.Insert(Key, Value) and thus "A_Index" and "indexScripts" can't be omitted?
Object.Insert(Key, Value) form would be invoked if the first parameter of
.Insert() was a string or a variable that contains a non-numeric string or an expression that 'evaluates' to a non-numeric string.
The conclusion is that it is possible to omit the
Index in
.Insert(), like this:
Code: Select all
processesSnapshot.Insert({"pid": Process.ProcessId, "exe": Process.ExecutablePath, "cmd": Process.CommandLine})
But if
Index is omitted, one has to remember that _only one_
Value can be inserted at a time at the end of the array with
.Insert(). To circumvent this, always specify the index manually.