Could someone tell me why I can't use a Ternary operator in a mathematical function?
Here's an example:
<cfset test = 1>
<cfdump var="#structKeyExists(Variables, 'test') ? 1 : 0 + 20#">
I would expect the result of this to be 21, however the result actually comes out as 1.
I discovered this on CF10, and when I tested it on Railo it came out the same so I'm wondering: is this a bug or is there a reason I'm not supposed to be able to use a Ternary operator in this context?
The false statement of your ternary doesn't end at the 0, it ends at the end of the line of code. When the check is false, the ternary will return statement right of the :, in your example 0 + 20.
As Beginner has recommended, using brackets will allow you to end the ternary earlier, and continue to manipulate the result. <cfdump var="#(structKeyExists(Variables, 'test') ? 1 : 0) + 20#"> will give you 21 or 20 as expcted.
Related
I've seen answers to posts - Is there a way AND/OR conditional operator in terraform? - where it mentions that terraform will convert a bool to either 1 or 0, however, I can't get this to work - It seems this is specific to version 0.11 and earlier?
I'm trying to run a count using signum(true * 0) however this just keeps returning invalid operand <bool/number> required
This did work in terraform 11 but does not seem to as I try and upgrade to 12/13. We have a large repository and it would be easier if I could get this to work instead of converting all bools to numbers. Can someone please clarify if this is possible in versions 12 or later?
Full example is:
count = signum(var.cluster_size_max * var.with_external_elb)
where an external ELB is built if the cluster_size_max is set to 1 or more and external_elb is true.
Note: I've tried using && instead of signum but this also doesn't work. The error I got was similar:
count = var.cluster_size_max && var.with_external_elb ? 1 : 0
----------------
var.cluster_size_max is 1
Unsuitable value for left operand: bool required.
You do not need to use the signum function for simple conditional statements.
Use && as an and, use > to check the value of var.cluster_size_max & use ? + : as a ternary operator to determine the final integer value of 1 or 0 based on the and result.
Try:
count = var.with_external_elb && var.cluster_size_max > 0 ? 1 : 0
Could you please help me understand why this statement is incorrect (from a quiz). For some reason I can't see a problem.
if total = 140 then status EQ 'works';
Thanks!
eq is the comparison equals operator, not the assignment equals operator. = performs both roles.
So,
if total eq 140 then status='works';
would be perfectly legal.
If my Informatica code looks like this:
IIF(foo,1,0)
and foo = NULL, what would the output be?
Sorry if the answer to this is to obvious or is in a reference somewhere, but I couldn't find anything useful through googling or searching SO.
foo is a expression that should evaluate to either TRUE or FALSE, so here's how Integration Service interprets different integer values when they constitute the entire condition:
IIF( -7, 1, 0) -> 1
IIF( 0, 1, 0) -> 0
IIF(NULL, 1, 0) -> 0
IIF( 7, 1, 0) -> 1
All negative or positive integers yield TRUE.
The values 0 or NULL give FALSE.
If I were you, I'd attempt at rewriting this particular piece. Although its not incorrect, its best not to code like this. wrap the "foo" around a isnull() if you suspect it to be such. Further, use an nvl equivalent and check for those values. Conditional checks dealing with NULL are always expensive.
Both FindNoCase and Find funcions are returning 0 values for all cases. I'm working on Coldfusion 9.
<cfoutput>#Find("aaInternationalbb", "International")#</cfoutput> ->o/p:0
<cfoutput>#Find("aalbb", "International")#</cfoutput> ->o/p:0
<cfoutput>#FindNoCase("aaInternationalbb", "International")#</cfoutput> ->o/p:0
<cfoutput>#FindNoCase("aalbb", "International")#</cfoutput> ->o/p:0
Please let me know how to make this work. Thanks in advance
This is not working because you have given the parameters in wrong order. The syntax for each of find() and findNoCase() are
FindNoCase(substring, string [, start ])
Find(substring, string [, start ])
So you have to try like:
<cfoutput>#Find( "International","aaInternationalbb")#</cfoutput>
<cfoutput>#Find( "International","aalbb")#</cfoutput>
<cfoutput>#FindNoCase("International","aaInternationalbb" )#</cfoutput>
<cfoutput>#FindNoCase("International","aalbb")#</cfoutput>
This will give output as: 3 0 3 0
findNoCase and Find will expect ( stringtosearch, stringtosearchfrom [,start])
So I've been trying to figure out what is wrong with my if-condition, but I am getting nowhere. I am still new to R, so maybe I am not understanding some very basic concept here?
I have a dataframe (dc) to which I appended a column with logical "FALSE". Now I want to change each FALSE into a TRUE based on the values in two columns of dc (dc$Probe and dc$Resp) that I specified using regexpr().
What it does so far is that, for both if-conditions, it changes each FALSE into TRUE regardless of the values in column 5 of dc. When I run the if-conditions seperately, I can see that they seem to be working fine on the OR-part of the condition, meaning the code only generates TRUE when the strings in dc$Probe match one of the strings specified in the OR-part. However, the AND-part seems to be ignored? Thus, when I run the complete code, I get a column with only TRUE, which is not what I want.
Edit: I should get a TRUE only if the string in Probe ends in a certain pattern (as specified in either of the two if conditions I wrote) and if the corresponding value in Resp is a "100" for the patterns specified in my first condition or a "200" for the patterns specified in my second condition. Thus, for strings ending in (sg|s|w1|w3|s1|s2), Resp must be "100" to get a TRUE and for strings ending in (\d\dg|\d\d), Resp must be "200" to get a TRUE. All other cases should be FALSE. For example, if a string ends in s1 and the corresponding value in Resp is 200, the code should return FALSE.
Edit: Some example data:
>dc<-data.frame(Subject=rep("SN",6), item.c=(1:6), Stim=c("XYZc02s03","XYZc01s30","XYZc02s29", "XYZc01s38", "XYZc02s11", "XYZc06w21"), Probe=c("XYzf02s03","XYZf01s30g","XYZf02s29w1","XYZf01s38sg","XYZf02s11s","XYZv06w21s1"), Resp=c(200, 100, 100, 100, 100, 200))
This is my code:
>dc$Resp<-as.character(dc$Resp) #column 5 in dc
dc$Probe<-as.character(dc$Probe)
dc$correct_response <- FALSE
for (i in 1:nrow(dc)) {
if (regexpr("^.*sg$", dc$Probe[i])==1 || regexpr("^.*s$", dc$Probe[i])==1 || regexpr("^.*w1$", dc$Probe[i])==1 || regexpr("^.*w3$", dc$Probe[i])==1 || regexpr("^.*s1$", dc$Probe[i])==1 || regexpr("^.*s2$", dc$Probe[i])==1 && dc[i,5]=="100") {(dc$correct_response[i]<- TRUE)}
if (regexpr("^.*\\d\\dg$", dc$Probe[i])==1 || regexpr("^.*\\d\\d$", dc$Probe[i])==1 && dc[i,5]=="200") {(dc$correct_response[i]<- TRUE)}
}
Is there something wrong with the regular expressions I am using? I checked them with glob2rx() and it seems like they are ok...Is my use of "OR" (||) or/and "AND" (&&) incorrect? How do I implement the AND-part properly? I have also tried the following code for the AND-part, but it didn't change anything:
regexpr("200", dc$Resp[i])==1
I read the R-help on regular expressions and control flow, but I still don't see what I am doing wrong. Consulting other webpages on logical expressions did not help me either.
Please help!
Im wondering if it can all be reduced to the following:
dc<- read.table(header=T,text="Subject item.c Stim Probe Resp
SN 1 XYZc02s03 XYzf02s03 200
SN 2 XYZc01s30 XYZf01s30g 100
SN 3 XYZc02s29 XYZf02s29w1 100
SN 4 XYZc01s38 XYZf01s38sg 100
SN 5 XYZc02s11 XYZf02s11s 100
SN 6 XYZc06w21 XYZv06w21s1 200")
cond1<-regexpr("^.*(sg|s|w1|w3|s1|s2)$", dc$Probe)==1 & dc$Resp==100
cond2<-regexpr("^.*(\\d\\dg|\\d\\d)$", dc$Probe)==1 & dc$Resp==200
dc$correct_response<-cond1|cond2
For one thing, you are missing a logical operator between the 2nd and 3rd clauses of your first if statement.