I have found one case that does not make sense.
I have following feature:
test_array_deep_copy: BOOLEAN
local
imp, old_imp: ARRAY[STRING]
do
comment("Test of a deep copy.")
create {ARRAY[STRING]} imp.make_empty
imp.force ("Alan", 1)
imp.force ("Mark", 2)
imp.force ("Tom", 3)
old_imp := imp.deep_twin
imp[2] := "Jim"
Result :=
across
1 |..| imp.count as j
all
j.item /= 2 implies imp [j.item] = old_imp [j.item]
end
check not Result end
end
Since it is deep copy, that means address of imp and old_imp are different, as well as that its attributes in both two also refers to different address.
So, this "Result" after across loop, it should be false because addresses in imp and old_imp at same index are different.
So when I debug this code, it say Result is set to be false after finishing across loop.
The problem is that "check not Result" does not make false to true.
If I run workbench system, it says following:
I do not know why. "not" before "Result" in "check not Result" statement should make its whole check true, so it should say "PASSED" in workbench system, but it fails.
why is that?
Your reasoning is correct and the test query as written should return False. Most probably, the system reports FAILED as soon as the query returns False. So, what needs to be done is to fix the query itself.
The items of the array are cloned and therefore the equality = used in the loop gives False for all elements. To get True from the loop, a different equality operator has to be used: ~. It compares objects rather than references. After that change the query gives True and the test should pass.
An alternative would be to replace the equality operator with a call to the feature is_deep_equal:
imp [j.item].is_deep_equal (old_imp [j.item])
Unlike the operator ~ that uses the user-defined feature is_equal to compare objects – strings in the example – is_deep_equal performs "deep" equality test by traversing the whole object tree. The test should pass in this case as well. But deep equality is rarely used in practice.
Related
I am programming in Fortran and if all single elements of my array are positive I want to execute statement 1, if they are partly positive execute statement 2 and if all are negative execute statement 3.
I know I will probably need a 'do' loop and a 'if' construct but could not figure out how to do it best.
There is no need to use loop for a simple condition
if (ALL(A>0)) then
statement1
else if (ALL(A<0)) then
statement3
else
statement2
end if
Explanation: A>0 is an array of logical values based on evaluating the condition for each element of the original array A. Function ALL() then reduces this logical array and returns true if all elements are true and false otherwise.
You request a do loop in the title. If you really need to fix a particular error with that, you must show us the code from your efforts, your errors and all other important details.
I was answering a postgres question yesterday, and also came across a postgres thread (here) where they describe the following error:
ERROR: operator does not exist: text = text[]
HINT: No operator matches the given name and argument type(s). You
might need to add explicit type casts.
The error seems to appear whenever an ARRAY string type is fed to ANY without using = ANY. This seems completely strange since based on language, logic, and sql conventions, usually you have (e.g. IN):
variable FUNCTION(set)
instead of.
variable = FUNCTION(set) , unless ofcourse operator is a summation/count operation returning one result :)
It would make more senseto have variable ANY(Set/Array) instead of variable=ANY(Set/Array). Similar example is the IN function.
Can anyone explain what is going on here?
IN (...) is basically equivalent to = ANY (ARRAY[...])
Crucially, ANY is not a function. It's syntax defined by the SQL standard, and is no more a function than GROUP BY or the OVER clause in a window function.
The reason that = is required before ANY is that ANY can apply to other operators too. What it means is "Test the operator to the left against every element in the array on the right, and return true if the test is true for at least one element."
You can use > ANY (ARRAY[...]) or whatever. It's a general purpose operator that isn't restricted to =. Notably useful for LIKE ANY (albeit with somewhat bad performance).
There is ALL too, which does much the same thing but returns true only if all results are true.
Why unpack({0,1,1})==unpack({0,0,1}) are the same?
How to compare and proof them and proof they are different in Lua?
When a function call appears inside an expression, its return value is adjusted to one result. table.unpack({0,1,1}) == table.unpack({0,0,1}) is true because their first return value are both 0.
To compare them, iterate the tables and compare elements instead. table.pack might be helpful.
unpack is now table.unpack since Lua 5.2
Just stumbled across a bug in some old code of mine where to check if an array was empty or not I just wrote:
if my_array
...(do stuff)
end
instead of using isempty or something like that.
What I have discovered is that "if my_array" returns 0 only if the array is indeed empty OR if one or more of the components in the array are 0.
Is this expected? What exactly is going on? Is matlab performing an and operation on all the elements?
Many thanks
That is exactly the behaviour as it is documented:
An evaluated expression is true when the result is nonempty
and contains all nonzero elements (logical or real numeric). Otherwise,
the expression is false.
I'm working with some legacy spaghetti code that processes some hairy EDI. Most of the code is indecipherable and has no indentation or comments or even good variable names but there's one line that gives me trouble when I remove the On Error Resume Next statement which causes the script to always timeout.
If UBound(arylin) >= 1 Then
Do Until arylin(0) = "PB" and arylin(1) = "DEF"
' Processing goes here - not relevant for this
Loop
End If
The script executes the conditional but errors at the "Do Until" line, saying:
Microsoft VBScript runtime error '800a0009'
Subscript out of range: '[number: 0]'
Now, why on Earth would this be giving an error if I'm testing the upper bound before checking it, and it's saying that the upper bound is at least 1?
I can post more of the code if I have to but I'd rather not since my predecessor was a complete hack and the code is extremely ugly.
See AnthonyWJones comment)(With VB you can set the base of arrays to start at either 0 or 1 - it should be at or near the top of a module: look for Option Base 1 or Option Base 0.)
LBound will always return 0.(You can also use LBound() to check the lower boundary of the array.)
(Just goes to show: don't expect MS to be consistent!)
Although you check that the array-size is >= 1 - which will ensure that arylin(0) is valid, but not necessarily arylin(1). The index is zero-based so if one element exists, it will be at index=0, and index=1 will be out-of-bounds.
You must check that the array-size >= 2 in this case. If you use two entries following each other consistently, then the check must be made for (array-size) mod 2 = 0.
Also make sure what value UBound actually returns. I googled and got contradicting information: this says it returns the 'count' while this one says it returns the physical limit (= 'count' -1).
About the 'On Error Resume Next' thing, maybe it should stay there ...
UBound() returns the index of the last element in the array. By default, arrays in vb languages prior to vb.net start at 1 rather than 0 (meaning the count and the index are normally the same thing, though you can change the default with Option Base).
Put those together you'll see that it's failing here:
arylin(0) = "PB"