Variable with multiple values to use - variables

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

Related

Trying to setState using eval() function (React Native)

I'm trying to set multiple states in a for loop to be false or true depending on whether they meet the (if statement) requirement. The for loop will loop through an array of strings, each string represents a state. But I can't seem to use eval within this.setState function...
I have tried researching online but none of the solutions match my problem or what I'm trying to solve. I even tried eval(this.state.anything) = false but it still doesn't work and shows a left hand assign invalid error.
let businessState = [
"this.state.groupName",
"this.state.groupOwnerName",
"this.state.groupDesc",
"this.props.profile._id",
"this.state.businessName",
"this.state.businessDesc",
"this.state.businessRegNo",
"this.state.businessType",
"this.state.businessEmail",
"this.state.businessTel",
"this.state.businessWeChat",
"this.state.businessRegPhotoUri",
"this.state.businessSignPhotoUri"
];
var temp = ""
for (i = 0; i < businessState.length; i++) {
if (eval(businessState[i]) == ""){
temp = businessState[i]+ "Error"
this.setState({
eval(temp): true
})
}
}
As you can see from the code above, I want to evaluate the state, and if the value that this particular state holds is an empty string "", I want to set this state name + "Error" (For example, if this.state.email is empty string "" I want to set this.state.emailError to true.
Instead of this.setState({eval(temp): true}) try this.setState({[temp]: true}). The brackets will output the string value stored in temp as a variable name in setState.
This article gives a good explanation
This Stack Overflow question and the accepted answer also should help

Define multiple variables in a condition in ST

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.

Why does R.all with R.both does not equal R.allPass with the same arguments?

I'm just learning while doing ramda.js. Well, there are many ways to reach a goal with ramda, but there is on thing I do not understand.
I would like to check the input for an array of strings that all match one regular expression. I thought I could do it R.all(R.both(isString, isRegExp)), but it seems to deliver a true when the input is a number.
As expected R.allPass([isString, isRegExp]) gives a false with a number input.
But can anyone please explain me why R.all is returning a true? Or what and where is mistake (in thinking)?
Complete code:
var isString = R.is(String),
isMyRegExp = R.test(/^[a-z]+$/),
isMyRegExpString = R.both(isString, isMyRegExp),
isArrayOfMyRegExpStrings = R.all(isMyRegExpString),
isArrayOfMyRegExpStringsPass = R.allPass([isString, isMyRegExp]),
result = {
'all': isArrayOfMyRegExpStrings(9),
'allPass': isArrayOfMyRegExpStringsPass(9)
};
console.log(result);
// {
// all: true,
// allPass: false
// }
https://codepen.io/Eisenhardt/pen/PKLZqj
PS:
I know that I could shorten conditions with just the regexp, but there could be other situations where I need both conditions to be true. eg. isArrayOfNumber and sumOfNumbersOver50.
The second argument to R.all is expecting a list of values to test. Due to the way the function is implemented it is treating the 9 in your example as an empty list, resulting in a vacuous truth and evaluating to true.

Adding to List Prints true in Velocity

I am trying to add some string values to a list in Velocity. When I run the code it works alright. But the line where it adds the value prints true. Is it always like that in Velocity? I am new to Velocity templates, so cant figure it out myself.
#set ($uniqueInterfaces = [])
#if($ipv4interfaceName == $ipv6interfaceName)
$uniqueInterfaces.add($ipv4interfaceName)
#end
Its part of larger code with a nested foreach. It has two matches in it, so the output is:
true
true
I do not need this true being printed at all!
Java's List#add method returns boolean, that's why this return value is printed in your html output.
You can hide it simply by assigning the output of the add method to a dummy variable:
#set ($uniqueInterfaces = [])
#if($ipv4interfaceName == $ipv6interfaceName)
#set ($swallow = $uniqueInterfaces.add($ipv4interfaceName))
#end

Lua metatables and metamethod - How to call a different member function

I have the following Class
local PROGRESS = {}
PROGRESS.__index = function(self,key)
if key~="__group" and self.__group[key] then
return self.__group[key]
else
return rawget(self,key)
end
end
What this does is when You access table[key] it performs a lookup in table.__group (which is an object of another class) and returns table.__group[key] ,if it is not nil.
Now I am trying to do the same for member functions.
i.e If I call table:key() a lookup must be performed in table.__group and if the function is present, then table.__group:key() should be called.
How do I accomplish this?
I tried to do this.
local PROGRESS = {}
PROGRESS.__index = function(self,key)
if key~="__group" and self.__group[key] then
local val = self.__group[key]
if type(val) == "function" then
self.__group:val()
return function() end
end
return self.__group[key]
else
return rawget(self,key)
end
end
But there are 2 things wrong here.
I am unable to retrieve the original function's arguments
Event if I just ACCESS table[key].function without calling it, the function will be called
And I've got the feeling that I am trying to complicate things and the solution is way simpler.
Any help is appreciated.
UPDATE
#Mud
The problem with the original code is that the object passed as 'self' to the member function is an object of the new class. Not of the old class.
Consider this code
GROUP_CLASS = {}
GROUP_CLASS.__index = GROUP_CLASS
function GROUP_CLASS:showSum (a,b) print(self);print(a + b) end
group_object = setmetatable({},GROUP_CLASS)
group_object:showSum(1,2)
local PROGRESS_CLASS = {}
PROGRESS_CLASS.__index = function(self,key,value)
if key~="__group" and self.__group[key] then
return self.__group[key]
else
return rawget(self,key)
end
end
progress_object = setmetatable( {__group = group_object} , PROGRESS_CLASS)
progress_object:showSum(3,3)
--progress_object is passed as first argument to showSum. But i need group_object to be passed
In the above code, When progress_object:showSum(3,3) is called,
is it possible to pass group_object (or in other words progress_object.__group) as self instead of progress_object.
Hope that makes sense.
Response to updated post:
progress_object is passed as first argument to showSum. But i need group_object to be passed
If you're going to ignore the state of the object a method is called on, and substitute the state of some other object, why is it even a method on that object? That's like overriding the addition operator to do multiplication, a recipe for confusion.
In other words, you want this:
progress_object:method("foo")
To resolve, via bizarre internal machinery, into this:
group_object:method("foo")
Why not skip a step and just make the latter call?
If you must, you could achieve this by returning a proxy for the method which replaces self with __group
local PROGRESS_CLASS = {}
PROGRESS_CLASS.__index = function(self,key)
local groupval = self.__group[key]
if key == '__group' or not groupval then
return rawget(self,key)
elseif type(groupval) ~= 'function' then
return groupval
else
return function(...)
if self == ... then -- method call
-- replace self argument with __group
return groupval(self.__group,select(2,...))
else
return groupval(...)
end
end
end
end
Response to original post:
How I am trying to do the same for member functions. i.e If I call table:key() a lookup must be performed in table.__group and if the function is present, then table.__group:key() should be called.
How do I accomplish this?
Do nothing. Your original code handles this.
Lua doesn't know what a "member function" is. A member is a member (i.e. an element in a table), and whether the value of that member is a function is irrelevant.
Remember:
obj:method(a,b,c) is exactly equivalent to obj.method(obj,a,b,c)
obj.method is exactly equivalent to obj["method"].
Your code already resolves obj["method"] into obj.__group["method"]
So you're done.
For instance, say we have:
group = {}
function group:showSum (a,b) print(a + b) end
function group:showProduct(a,b) print(a * b) end
Using your first code, we can write:
foo = setmetatable({__group = group}, PROGRESS)
foo:showSum(3,3) -- 6
foo:showProduct(3,3) -- 9
That's it.
Now, as long as we're here, let's look at what your second function is doing:
local val = self.__group[key]
if type(val) == "function" then
self.__group:val()
return function() end
end
First you grab the function value from __group. At this point you're done. Simply return that value, and the caller is going to call that value (i.e. (...)). Instead, you call __group["val"] which is likely a totally different function from __group[key] (unless key=="val"), then you pass the caller a function which does nothing.