What Integers Are Considered "true" in a ColdFusion "if" statement [duplicate] - coldfusion

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
What are all the values that ColdFusion considers “falsy” and “truthy”?
In ColdFusion, what integers are considered true and what integers are considered false in an if statement? I know that 1 is true and 0 is false, but is this it?
For example, it would be nice to know that I can do an if on a query data structure's record count to determine if the query has at least one record in it. For example, let's say our query is called, employees.
<cfif variables.employees.recordCount>
There ARE employees.
<cfelse>
There ARE NO employees.
</cfif>
Would the statement "There ARE employees" be displayed if the record count was, say, 2?

Any non zero number is true. Only 0 will return false.

The expression inside the cfif tag must evaluate to a Boolean value (a true/false). Boolean values are represented as either "true" or "false", "yes" or "no", or "0" or any number.
Reference
So it looks like 0 is for false, or anything else for true in the case of integers.

Very similar question:
What are all the values that ColdFusion considers "falsy" and "truthy"?
Extensive blog post about how variables are evaluated for truthiness in ColdFusion:
What are all the values that ColdFusion considers "falsy" and "truthy"?

Related

Two conditions in Google Sheets Function

I have a question on using two functions with an if statement in Google Sheets as one complete function. Both variables have to be true, otherwise it returns false. I need one function to check the date 20 months back from today. If said cell is less than today's date 20 months back it's true, naturally. However, for the complete function to return true it also searches for another text value in another cell and has to be an exact match. Both conditions have to be true (the date and the exact match) for the function to be true. So if the date in the cell is less than today's date 20 months back and the text value in the other cell is an exact match, function is true.
Problem is that it seems like the date function does not seem to apply.
=IF(D2<DATE(YEAR(TODAY()),MONTH(TODAY())-20,DAY(TODAY())),AND(REGEXMATCH(M2,"text")),TRUE,FALSE)
You current formula is not set up correctly (nor logically). Given only what you've shown here, this should work:
=IF(AND( D2<DATE(YEAR(TODAY()),MONTH(TODAY())-20,DAY(TODAY())), REGEXMATCH(M2,"text") ),TRUE,FALSE)
Notice that the AND( ) contains both conditions here, whereas your original formula had it only around the second condition.
However, a shorter version of this would be as follows:
=AND( D2<DATE(YEAR(TODAY()),MONTH(TODAY())-20,DAY(TODAY())), REGEXMATCH(M2,"text") )
... since the result of a properly functioning AND( ) is always TRUE or FALSE anyway.
It looks like you're supplying 4 arguments to the IF statement:
=IF(DATECHECK,AND(TEXTCHECK),TRUE,FALSE)
The IF statement expects 3 arguments instead. 1) the condition, 2) the value if true, and 3) the value if false. You can combine your two conditions using an AND statement like this:
AND(DATECHECK,TEXTCHECK)
The final formula would then be:
=IF(AND(D2<DATE(YEAR(TODAY()),MONTH(TODAY())-20,DAY(TODAY())), REGEXMATCH(M2,"text")),TRUE,FALSE)

Make =IF Function Output Numbers For "Scoring": Google Sheets

I'm am exploring methods of giving scores to different datapoints within a dataset. These points come from a mix of numbers and text string attributes looking for certain characteristics, e.g. if Col. A contains more than X number of "|", then give it a 1. If not, it gets a 0 for that category. I also have some that give the point when the value is >X.
I have been trying to do this with =IF, for example, =IF([sheet] = [Text], "1","0").
I can get it to give me 1 or 0, but I am unable to get a point total with sum.
I have tried changing the formatting of the text to both "number", "plain text", and have left it as automatic, but I can't get it to sum. Thoughts? Is there maybe a better way to do this?
FWIW - I'm trying to score based on about 12 factors.
Best,
Alex
The issue here might be that you're having the cell evaluate to either the string "0" or the string "1" rather than the number 0 or the number 1. That would explain why you're seeing the right things but the math isn't coming out right - the cell contents look like numbers, but they're really text, which the summation would then ignore.
One option would be to drop the quotation marks and write something like this:
=IF(condition, 1, 0)
This has the condition evaluate to 1 if it's true and 0 if it's false.
Alternatively, you could write something like this:
=(condition) * 1
This will take the boolean TRUE or FALSE returned by condition and convert it to either the numeric value 1 (true) or the numeric value 0 (false).

Test answer of the boolean 'true' explaination

Im very confused why the first 3 arnt all correct. As isn't true a keyword, it of course is a Boolean literal and is interchangeable with 1?
True and False are not keywords, they are boolean values because they are associated with 1 and 0 respectively. For a complete list of keywords see Is it possible to get a list of keywords in Python?

trying to validate string as boolean Coldfusion

I'm currently trying to validate if a string is exactly true or false as I'm building a spreadsheet importer using CF10.
My users will either enter in one of the following.
NULL, which will equate to false
TRUE which obviously equates to true
FALSE which again will equate to false.
I'm trying to use the isValid('boolean',variable.data) to validate if the data is in fact a boolean string or not. However after reading quite a few posts I can see that it will validate true if its a positive number and false if it is a negative one etc.
I'm wondering if anyone has an easy fix to getting boolean validation working for strings easily ? Is this more a regular expressions scenario to make it work properly ?
Any help greatly appreciated.
Assuming you mean the literal string "NULL", one option is using list functions. Search a list of allowed values, ie "true,false,null". If the entry is found, it is valid:
<cfif listFindNoCase("true,false,null", theValue)>
this is a valid boolean value
<cfelse>
not found. do something here...
</cfif>
Then a simple string comparison would return false for everything other than the literal string "true":
isTrue = compareNoCase(e, "true") eq 0 ? true : false`
If you need to evaluate whether a string is exactly "true" or "false", then you are not doing a boolean comparison, you are doing a string comparison.
So to that end, you'd be wanting to use compare() or compareNoCase() (depending on how rigid you need to be).
You could just do some logic or am I missing something.
<cfset booleanResult = enteredValue eq 'TRUE' ? true : false />

Determining if a string is not null/blank and is a number and not 0?

I normally don't work in ColdFusion but there's a FTP process at work I have to create a report for with the only option right now being a ColdFusion 8 server. This FTP feed has a few issues (trash too).
So, I make the query and then I need to convert some of the string values during the output to do some math. Before that:
How do I tell if a field in the output loop: is not blank or null, is string that can be converted into a valid number, and is not 0?
Is there a simple way of doing this w/o a lot of if statements?
Thanks!
So you want to make sure that the variable is numeric but not zero?
Then you want this:
<cfif IsNumeric(MyVar) AND MyVar NEQ 0 >
Determining if a string is not null/blank and is a number and not 0?
Here's the code I would use in this case.
<cfif isDefined(stringVar) and len((trim(stringVar))) and isNumeric(stringVar)>
do stuff here
</cfif>
isDefined returns a true if the variable exists. If you know the scope of the variable, i.e., its in the form or url scope for instance, you can use structkeyExists(form,"stringVar"). I would recommend using this approach if you know the scope of the variable.
Len(trim(stringVar)) is the second check. First off it trims any leading or trailing empty spaces from the string - this makes sure that any empty variables are not passed along. Then if something is there it will return the length of the string. If its empty len will return a 0.
isNumeric(stringVar) returns a true if the variable is a number and false otherwise.
<cfif Len(field) and Val(field)>
Len() will verify the field has length (not blank--there are no NULLs in CF) and Val() will automatically convert the first character in the string into into a number--or return 0 if it cannot.
Take note of Peter's comment below; although this is the least verbose answer, Val() may fail in certain edge conditions below, ie. The field is a string but starts with a number, incorrectly converting it to a number, and evaluating to TRUE.
<cfif isNumeric(myfield) and myfield gt 0>