a = reg[3:0].
what values of "a" return true in: "if(a)"?.
which cell of the register a does the "if" check in the previous format?.
Does it return 0 only for a=0000 or are there other values for a that make if(a)=0?.
If a is reg [3:0], it evaluates to false inside if statement only when a == 4'b0000.
Related
I am struggling to use an if/else on a containsAll() statement. It returns the correct true false value when tested with println(), but when put in an if statement it seems to always evaluate to true -- see below.
def examine_phenotype(pheno){
condition_values = \
Channel
.fromPath(pheno)
.splitCsv(header: true, sep: ',')
.map{ row ->
def condition = row.condition
return condition
}
.toList().view()
println(condition_values.containsAll('control'))
if(condition_values.containsAll('control')){
exit 1, "eval true"
}else{
exit 1, "eval false"
}
}
Console output for two different files, one with 'control' and one without 'control' in the column 'condition', which is the point of the function.
[normal, normal, normal, tumor, tumor, tumor]
DataflowInvocationExpression(value=false)
eval true
[control, control, control, tumor, tumor, tumor]
DataflowInvocationExpression(value=true)
eval true
Using collect() instead of toList() where each item within condition_values is enclosed with single quotes did not resolve the issue either. The clue might be in DataflowInvocationExpression but I am not up to speed on Groovy yet and am not sure how to proceed.
Testing the conditional within the function was not working, but applying filter{} and ifEmpty{} was able to produce the desired check:
ch_phenotype = Channel.empty()
if(pheno_path){
pheno_file = file(pheno_path)
ch_phenotype = examine_phenotype(pheno_file)
ch_phenotype.filter{ it =~/control/ }
.ifEmpty{ exit 1, "no control values in condition column"}
}
def examine_phenotype(pheno){
Channel
.fromPath(pheno)
.splitCsv(header: true, sep: ',')
.map{ row ->
def condition = row.condition
return condition
}
.toList()
}
I have been looking at this simple if/else statement in another larger project and I can't seem to find what I am doing wrong.
I have inserted Logger.log() in both if statements to try to root out the problem.
When I run the code, I get the following Log:
[19-02-24 08:50:05:427 PST] var Campus = Baylor
[19-02-24 08:50:05:428 PST] var TSTCCampus = TSTC
[19-02-24 08:50:05:428 PST] if IS= statement executed
[19-02-24 08:50:05:428 PST] else NOT= executed
The two variables are clearly NOT equal but the if = executes and the else != executes.
What am I doing wrong?
function myFunction() {
// call the Current Reults sheet and identify the Last Row of Responses
var RawFormResponsesSheet = SpreadsheetApp.getActive().getSheetByName("Current Results");
var CurrentSubmission = RawFormResponsesSheet.getLastRow(); // Retruns the Value of the Last Submission Row Number
var Campus = RawFormResponsesSheet.getRange(CurrentSubmission,3).getValue();
// call the Email Data sheet and identify certain cell values
var EmailDataSheet = SpreadsheetApp.getActive().getSheetByName("Email Data")
var TSTCCampus = EmailDataSheet.getRange(3, 4).getValue();
var BaylorCampus = EmailDataSheet.getRange(4, 4).getValue();
Logger.log("var Campus = " + Campus)
Logger.log("var TSTCCampus = " + TSTCCampus)
if (Campus = TSTCCampus){Logger.log ("if IS= statement executed")}
else {Logger.log ("else IS= executed")}
if (Campus != TSTCCampus){Logger.log ("if NOT= statement executed")}
else {Logger.log ("else NOT= executed")}
}
You need to use == as comparison operator.
There are 3 = operators/commands:
= is used to set the value of a variable
== equality operator, returns true if the elements have same value, performs type conversion
=== identity operator, similar to ==, but no type conversion
The following explains what type conversion is.
"5" == "5" returns true
"5" == 5 returns true
"5" === "5" returns true
"5" === 5 returns false
somevar = 5 assigns and returns 5, which is a truthy value, and thus if (x = 5) { will always execute the conditional body. Similarly if (x = 0) { will never execute the conditional body, because it assigns and returns 0, which is falsy.
I have a table model named History and all its attributes are boolean.
How can I select only the attributes set to true?
I tried
History.select {|h| h == true}
what am I missing?
For all ones with all true:
boolean_columns = Model.columns.map(&:name) - %w(id created_at updated_at)
Model.where(boolean_columns.zip([true].cycle).to_h)
Attributes only works on an instance, and in that case you probably just want to query it.
For a single one:
Model.first.attributes.select { |_, v| v == true }
The reason I do v == true instead of v is because you want explicit boolean true, not necessarily truthy.
I'm new to lua, and am trying to write some code to send information to a text file that is overwritten each loop cycle.The info sent to the text file is different based on a value that increases to a specific known number (3 for this example) and then decreases below that number in any order.
Here's where I am stuck:
I want to send different info to the text file using the same values
but based on whether the value has reached 3 or not, if that makes
sense.
Example in English:
if value is 2 and has not reached 3 then write: UP 2
if value is 2 and has reached 3 then write: DOWN 2
if value is 3 then write: TOP
My Question:
Can I get an infinite while loop to start inside another infinite while loop when a value meets a specific elseif condition. I want to try and use the loop structure below if possible.
My Code looks somewhat like this:
while true do
file = assert(io.open("file.txt","w+"))
value = ipc.readSD(0x0BE8)
if value = 0
file:write("UP 0")
file:close()
elseif value = 1
file:write("UP 1")
file:close()
elseif value = 2
file:write("UP 2")
file:close()
elseif value = 3
file:write("TOP")
file:close()
--How do I trigger/start this inner loop?
while true do
--now stay in here until break
file = assert(io.open("file.txt","w+"))
value = ipc.readSD(0x0BE8)
if
value = 0
file:write("DOWN 0")
file:close()
break
elseif
value = 1
file:write("DOWN 1")
file:close()
elseif
value = 2
file:write("DOWN 2")
file:close()
else
break
else
file:write("OUTSIDE")
end
end
There are several syntax errors in your code, it shouldn't compile at all.
Use == to compare equal instead of =
For every if / elseif, there should always be a corresponding
then.
For every if / while / do block, there should always be a
corresponding end.
As for your question, if I understand it correctly, you can use goto to simulate continue when you don't want to enter the inner loop:
while true do
value = getValue()
if value == 1 then
--not enter inner loop
goto continue
elseif value == 2 then
--enter inner loop
end
while true do
--inner loop
end
::continue::
end
I've been taught to program in Java. Lua is new to me and I've tried to do my homework but am not sure what an if statement of the following nature means.
The code is as follows:
local function getMinHeight(self)
local minHeight = 0
for i=1, minimizedLines do
local line = select(9+i, self:GetRegions())
**if(line) then
minHeight = minHeight + line:GetHeight() + 2.5
end**
end
if(minHeight == 0) then
minHeight = select(2, self:GetFont()) + 2.5
end
return minHeight
end
The if statement with the ** before and after is the part I'm not sure about. I don't know what the if statement is checking. If the line is not nil? If the line exists? If what?
In Lua, anything that's not nil or false evaluates to true in a conditional.
If the line is not nil? If the line exists?
Yes to both, because they kinda mean the same thing.
The select function returns a specific argument from it's list of arguments. It's used primarily with ..., but in this case it's being used to select the (i+9)th value returned by self:GetRegions. If there is no such value (for instance, if GetRegions only returns 5 values), then select returns nil.
if(line) is checking to see that it got a value back from select.
if(line) is being used as a shortcut for if(line ~= nil), since nil evaluates to false in a conditional.
It's worth pointing out that this shortcut is not always appropriate. For instance, we can iterate all the values in a table like this:
key, val = next(lookup)
while key do
print(key, val)
key, val = next(lookup, key)
end
However, this will fail if one of the table's keys happens be false:
lookup = {
["fred"] = "Fred Flinstone",
[true] = "True",
[false] = "False",
}
So we have to explicitly check for nil:
key, val = next(lookup)
while key ~= nil do
print(key, val)
key, val = next(lookup, key)
end
As Mud says, in lua anything other than nil and false is considered truthy. So the if above will pass as long as line is not nil or false.
That said, it worries me a bit the way you have phrased the question - "an if with only one argument".
First, it's not called "argument" - it's called expression. And in most languages is always one. In java, for example, you could do something like this:
bool found = false
...
if(found) {
...
}
ifs only care about the final value of the expression; they don't care whether it's a single variable or a more complex construction.