OK, you are right. It works. I guess there are just too many pitfalls and our mutual examples keep on being incorrect (mine too !).
With a larger array-application, always something goes wrong (not working) because of the strange way how to deal with them. This is for that other topic, so never mind that for now (I just try to find excuses 🙂 🙂 ).
Anyway, it requires 100% literal examples, or else something differently happens. Thus your last example
$var[0] = 1000
will always show 1000 indeed, but not because it is going across bars. You just assign it newly each time in this example (it requires an If not to do that).
I repeat : my own examples also work out differently. But a lot is based on mistakes, where you don’t correct me on. Example (implied in my earlier example) :
// Suppose these are THE ONLY lines in a Strategy :
$Arr[2] = $Arr[2] + 1000 // This errors-out internally.
aaa = aaa + 1 // But this is fine (which I personally hate, but alas).
Graph $Arr[2] // Graphs nothing because it errored out earlier.
Graph aaa // Graphs 1, 2, 3, 4, ...
As you can see this is not consistent behaviour and I never saw that (I never recognised it).
This code would be correctly working (tested this time) :
Once ArraySet = 0
If not ArraySet then // THIS ONE !
$Arr[0] = 89 // So Yes, this value is retained.
$Arr[1] = 200
ArraySet = 1
endif
MyVar = 10
MyVar = MyVar + $Arr[0]
Graph MyVar // Always graphs 99.
I like to bring across that this is enormously ambiguous coding because it requires nothing much to let the line denoted “THIS ONE !” to go wrong on the coding side (I forget line one, for example or anything else that makes the If work out to False always) and the code at this line :
MyVar = MyVar + $Arr[0]
will just not do its job. And, if I use arrays like your examples (everywhere I’d say, including the last posts) then you use them locally (within the one bar) and by nature that would not fail. This is because I won’t need any If Not ArraySet stuff.
So …
@fhorus, I don’t understand what your issue is, because all just works (LOL). But I hope that Nicolas sees that his last reply to fhorus is again with an example where the array is used “locally”. And this way we will never understand.
With my way of throwing confusement at you, also not.
The ONCE should be used to define a variable such that its scope is always under the definition. This is ambiguous :
If not ArraySet then
$Arr[0] = 89
$Arr[1] = 200
ArraySet = 1
endif
// What if the code in the If is never executed ?
// We would have unexpected results in :
bb = $Arr[0]
// and all what further uses bb (or $Arr). And no error message anywhere.
Now this :
Once $Arr[] // This initialises all array elements at 0. But which ? so there's more to it (Dim command).
If not ArraySet then
$Arr[0] = 89
$Arr[1] = 200
ArraySet = 1
endif
// Now nothing is ambiguous.
So the above (last code) can not be done because PRT does not allow for it.
Lastly :
Once A = 0 // If I would leave this out, I'd kill myself on the next occasion.
A = A + 1 // Count across bars.
So you guys come up with examples leaving out the first line and deem it fine.
It is of course PRT providing the option (not for arrays !) so we can wonder who is “wrong” to begin with.
Thus @fhorus, with the correct examples everybody turns out to be correct. But the main thing is : it just works as you (and me) want it to. Not with the ONCE but with some kind of decent If.
Apologies for all the confusing stuff. Luckily I was somewhat cautious. 🙁
This was the longest post possible to state that I was wrong after all.