I am currently programming/simulating a small plant in CODESYS.
I have several outputs (that correspond to engines) that I need to test several times, so I want to create a condition that incorporates this test so I dont need to write the entire condition.
For instance, i have the condition that verifies if
A=TRUE AND B=TRUE AND C=TRUE AND D=TRUE
Can I create a condition like "verify engine" to use each time ?
Thank you
There are many ways to do this (if I understood you correctly).
Here are two ways for example:
1. Create a variable that has the condition result and use the variable. You have to assign the variable at beginning, and then you can use the variable instead of that long code.
VAR
EnginesOK : BOOL;
END_VAR
//Check engines
EnginesOK := (A = TRUE AND B = TRUE AND C = TRUE AND D = TRUE);
//.. Later ..
IF EnginesOK THEN
//Do something
END_IF
2. Create a function, for example F_VerifyEngines that contains checks and returns the state as BOOL. Note: In this example A,B,C and D need to be global variables. You could also pass them as parameters for the function.
FUNCTION F_VerifyEngines : BOOL
VAR_INPUT
//Add A,B,C,D here if needed
END_VAR
VAR
END_VAR
//Return the result
F_VerifyEngines := (A = TRUE AND B = TRUE AND C = TRUE AND D = TRUE);
Then you can use the function in code:
IF F_VerifyEngines() THEN
//Do something
END_IF
The 2nd way is probably the one you were thinking.
By the way, there is no need to write A = TRUE AND B = TRUE AND C = TRUE AND D = TRUE, in my opinion, it's more clear to read when you use A AND B AND C AND D instead.
Related
Im editing a script for fivem and Im trying to store multiple values into a variable then using an if statement to see if a player has one of the values in his inventory. So far i have this
Config.ItemVapeLiquid = 'liquid', 'liquidice', 'liquidberry'
the problem I'm having is it only runs when the player has 'liquid' on them, when they don't, it returns a nil value even if they have 'liquidice' or 'liquidberry'. what im trying to get do is:
Config.ItemVapeLiquid = 'liquid', 'liquidice', 'liquidberry'
if the player has a value from ItemVapeLiquid then
allow player to add liquid
I want it to check all the values in the variable before returning nil as it seems to be checking 1 value and returning nil if its not in the inventory
I appreciate any help!
While the first line compiles, it will ignore any unassigned values ('liquidice', 'liquidberry').
It expects three variables:
local a, b, c = 'liquid', 'liquidice', 'liquidberry'
Since that's impractical you need an array instead, as well as a function to search within that array:
Config.ItemVapeLiquid = {'liquid', 'liquidice', 'liquidberry'}
function check(array, value)
for _,v in ipairs(array) do
if v == value then
return true
end
end
return false
end
print(check(Config.ItemVapeLiquid, 'liquid'))
print(check(Config.ItemVapeLiquid, 'liquidice'))
print(check(Config.ItemVapeLiquid, 'liquidberry'))
print(check(Config.ItemVapeLiquid, 'something'))
true
true
true
false
If you plan on calling checks more often, consider transforming the array into a set instead, since this is much faster:
-- feel free to make an array-to-set function for this
Config.ItemVapeLiquid = {
['liquid'] = true,
['liquidice'] = true,
['liquidberry'] = true
}
print(Config.ItemVapeLiquid['liquid'])
print(Config.ItemVapeLiquid['liquidice'])
print(Config.ItemVapeLiquid['liquidberry'])
print(Config.ItemVapeLiquid['something'])
true
true
true
nil
Notice that a missing element is now nil, which evaluates to false and is therefore usually fine. If you really need a false value, append:
Config.ItemVapeLiquid["something"] or false
I have a problem with two lists which contain duplicates
a = [1,1,2,3,4,4]
b = [1,2,3,4]
I would like to be able to extract the differences between the two lists ie.
c = [1,4]
but if I do c = a-b I get c =[]
It should be trivial but I can't find out :(
I tried also to parse the biggest list and remove items from it when I find them in the smallest list but I can't update lists on the fly, it does not work either
has anyone got an idea ?
thanks
You see an empty c as a result, because removing e.g. 1 removes all elements that are equal 1.
groovy:000> [1,1,1,1,1,2] - 1
===> [2]
What you need instead is to remove each occurrence of specific value separately. For that, you can use Groovy's Collection.removeElement(n) that removes a single element that matches the value. You can do it in a regular for-loop manner, or you can use another Groovy's collection method, e.g. inject to reduce a copy of a by removing each occurrence separately.
def c = b.inject([*a]) { acc, val -> acc.removeElement(val); acc }
assert c == [1,4]
Keep in mind, that inject method receives a copy of the a list (expression [*a] creates a new list from the a list elements.) Otherwise, acc.removeElement() would modify an existing a list. The inject method is an equivalent of a popular reduce or fold operation. Each iteration from this example could be visualized as:
--inject starts--
acc = [1,1,2,3,4,4]; val = 1; acc.removeElement(1) -> return [1,2,3,4,4]
acc = [1,2,3,4,4]; val = 2; acc.removeElement(2) -> return [1,3,4,4]
acc = [1,3,4,4]; val = 3; acc.removeElement(3) -> return [1,4,4]
acc = [1,4,4]; val = 4; acc.removeElement(4) -> return [1,4]
-- inject ends -->
PS: Kudos to almighty tim_yates who recommended improvements to that answer. Thanks, Tim!
the most readable that comes to my mind is:
a = [1,1,2,3,4,4]
b = [1,2,3,4]
c = a.clone()
b.each {c.removeElement(it)}
if you use this frequently you could add a method to the List metaClass:
List.metaClass.removeElements = { values -> values.each { delegate.removeElement(it) } }
a = [1,1,2,3,4,4]
b = [1,2,3,4]
c = a.clone()
c.removeElements(b)
I want to fill two variables in the same line, but I don't know the best way to do it at kotlin
var a:String? = null
var b:String? = null
a, b = "Text"
Not possible in Kotlin (unless you are ready to resort to some contrived constructs with repetition as described in other answers and comments). You cannot even write
a = b = "Text"
because weirdly enough, assignments are not expressions in Kotlin (as opposed to almost everything else like if, return, throw, swicth, etc., which are expressions in Kotlin, but not in Java, for example).
So, if you want to assign exactly the same value without repetition (of the assigned value), you'll have to write
a = "Text"
b = a
Note, that there is also an also function (pun intended), so technically you can write the following if you really want to stay on one line
a = "Text".also { b = it }
but I doubt it is really worth it.
var a: String? = null; var b: String? = null
or
var (a: String?, b: String?) = null to null
But please don't ever do so
Simply create an inline array, iterate through and assign values.
arrayListOf(a, b, c, d).forEach { it = "Text" }
In real project, TEST_TABLE would contain much of TEST_TABLE_NESTED, each with its own testVariable and bunch of testScript. test function from testScript would be used in C++ code, and TEST_TABLE_NESTED tables would be added automatically from C++ code too.
TEST_TABLE =
{
TEST_TABLE_NESTED =
{
testVariable = 5,
testScript =
{
test = function()
print(testVariable, "hello") --How to access 'testVariable'?
end
}
}
}
EDIT :
This is the actual scenario of using this script:
GameObjectScriptTables =
{
GameObject_1 = --Container of scripts corresponding to some gameObject
{
gameObjectOwner = actual_object_passed_from_c++, --This is an actual object passed from c++
GameObjectScript_1 = --This is a script with update(dt) method which will be called somwhere in c++ code
{
update = function(dt)
--here I want to use some data from gameObjectOwner like position or velocity
end
}
}
GameObject_2 =
{
gameObjectOwner = actual_object_passed_from_c++,
GameObjectScript_1 =
{
update = function(dt)
--here I want to use some data from gameObjectOwner like position or velocity
end
},
GameObjectScript_2 =
{
update = function(dt)
--here I want to use some data from gameObjectOwner like position or velocity
end
}
}
--And so on
}
Idea is that exists some testVariable object (passed from C++), which data is used all over TEST_TABLE_NESTED. For me, above example looks natural for this task, but it prints nil instead of 5. So how to acces a testVariable from testScript without printing a full path like TEST_TABLE.TEST_TABLE_NESTED.testVariable?
You're asking for something like a "parent" pointer, which tells table B about table A, but that doesn't exist. Internally, the only association they have is that one of A's values happens to be B, but any number of tables could contain B as a value. Which is B's parent?
If you want B to know about A, you'll need to tell it. You can add an extra parameter to update which receives the game owner object, or update can be a closure which contains the game owner as a bound variable, so on and so forth.
I made it work by providing a gameObjectOwner instance for each GameObjectScript_N. However I don't know is it expensive solution or not.
I need to build an array of objects of class ID using arrayfun:
% ID.m
classdef ID < handle
properties
id
end
methods
function obj = ID(id)
obj.id = id;
end
end
end
But get an error:
>> ids = 1:5;
>> s = arrayfun(#(id) ID(id), ids)
??? Error using ==> arrayfun
ID output type is not currently implemented.
I can build it alternatively in a loop:
s = [];
for k = 1 : length(ids)
s = cat(1, s, ID(ids(k)));
end
but what is wrong with this usage of arrayfun?
Edit (clarification of the question): The question is not how to workaround the problem (there are several solutions), but why the simple syntax s = arrayfun(#(id) ID(id), ids); doesn't work. Thanks.
Perhaps the easiest is to use cellfun, or force arrayfun to return a cell array by setting the 'UniformOutput' option. Then you can convert this cell array to an array of obects (same as using cat above).
s = arrayfun(#(x) ID(x), ids, 'UniformOutput', false);
s = [s{:}];
You are asking arrayfun to do something it isn't built to do.
The output from arrayfun must be:
scalar values (numeric, logical, character, or structure) or cell
arrays.
Objects don't count as any of the scalar types, which is why the "workarounds" all involve using a cell array as the output. One thing to try is using cell2mat to convert the output to your desired form; it can be done in one line. (I haven't tested it though.)
s = cell2mat(arrayfun(#(id) ID(id), ids,'UniformOutput',false));
This is how I would create an array of objects:
s = ID.empty(0,5);
for i=5:-1:1
s(i) = ID(i);
end
It is always a good idea to provide a "default constructor" with no arguments, or at least use default values:
classdef ID < handle
properties
id
end
methods
function obj = ID(id)
if nargin<1, id = 0; end
obj.id = id;
end
end
end