How do I prevent strings containing the word "yes" or "no" from printing out as true or false? [duplicate] - coldfusion

This question already has answers here:
How can I prevent SerializeJSON from changing Yes/No/True/False strings to boolean?
(7 answers)
Closed 6 years ago.
I'm currently setting a number of variables like so:
<cfset printPage = "YES">
Eventually, when I print these variables out, anything that I try to set to "YES" prints out as "true". Anything set to "NO", prints out as "false". I'm not opposed to using the YesNoFormat function. In fact I might end up using that function in this application, but in the mean time I would like to know if ColdFusion is actually storing the words "YES" and "NO" in memory, or if it is converting them to a boolean format behind the scenes.
If CF is storing my variables exactly the way that I declare them, how would I go about retrieving these variables as strings? If CF is changing the variables in some way, are there any special characters or keywords that I could use to force it to store the variables as strings?
Thank you to everyone that commented / answered. I did a little more experimenting and reading, and it seems that the serializeJSON function will automatically convert "Yes" to "true" and "No" to "false". I either need to deal with this problem in my javascript, or I can add a space in the affected properties to circumvent this behavior.

You already know how to display the boolean value as "yes" or "no" (using YesNoFormat()). I don't think there is a way to force ColdFusion to store a variable a certain way. It just doesn't support that. I guess you could call out the Java type directly by using JavaCast(). I just don't see why you would want to go through that extra work for something like this. You can certainly research that a bit more if you like. Here is a link to the document for JavaCast.
Have a look at this document regarding data types in ColdFusion. I will post some of the relevant points from that document here but please read that page for more information.
ColdFusion is often referred to as typeless because you do not assign types to variables and ColdFusion does not associate a type with the variable name. However, the data that a variable represents does have a type, and the data type affects how ColdFusion evaluates an expression or function argument. ColdFusion can automatically convert many data types into others when it evaluates expressions. For simple data, such as numbers and strings, the data type is unimportant until the variable is used in an expression or as a function argument.
ColdFusion variable data belongs to one of the following type categories:
Simple One value. Can use directly in ColdFusion expressions. Include numbers, strings, Boolean values, and date-time values.
Binary Raw data, such as the contents of a GIF file or an executable program file.
Complex A container for data. Generally represent more than one value. ColdFusion built-in complex data types include arrays, structures, queries, and XML document objects. You cannot use a complex variable, such as an array, directly in a ColdFusion expression, but you can use simple data type elements of a complex variable in an expression. For example, with a one-dimensional array of numbers called myArray, you cannot use the expression myArray * 5. However, you could use an expression myArray[3] * 5 to multiply the third element in the array by five.
Objects Complex constructs. Often encapsulate both data and functional operations.
It goes on to say this regarding Data Types:
Data type notes
Although ColdFusion variables do not have types, it is often convenient to use “variable type” as a shorthand for the type of data that the variable represents.
ColdFusion provides the following functions for identifying the data type of a variable:
IsArray
IsBinary
IsBoolean
IsImage
IsNumericDate
IsObject
IsPDFObject
IsQuery
IsSimpleValue
IsStruct
IsXmlDoc
ColdFusion also includes the following functions for determining whether a string can be represented as or converted to another data type:
IsDate
IsNumeric
IsXML
So in your code you could use something like IsBoolean(printPage) to check if it contains a boolean value. Of course that doesn't mean it is actually stored as a boolean but that ColdFusion can interpret it's value as a boolean.

Related

Get values of nested json with regex

I'm trying to select all of the lowest tier(in the "nest") possible values that are numbers, and not strings, in a nested json. This will include arrays and objects. Currently, im using this code
(?<=:)\s*([+-]?\d+(?:\.\d*(?:E-?\d+)?)?)\b\s*(?=(?:\{|,)\s*\"[^\"]*\":)
(which is mostly from this question)
and it works, if it isn't the last value of an object, or in a list. My main problem is, that it can be fooled. for example, if i have these two "a":":1,", ":":akey-value pairs next to eachother, it sees the number 1 as a value, except that the actual value is ":1,". How can i make a foolproof system for this?
Im excpecting it to not select anything, that couldn't be an integer or a float. I would like to keep the middle part of my original regex, beacuse it also needs to work with scientific notations.

TCL: check if variable is list

set var1 A
set var2 {A}
Is it possible to check if variable is list in TCL? For var1 and var2 llength gives 1. I am thinking that these 2 variables are considered same. They are both lists with 1 element. Am I right?
Those two things are considered to be entirely identical, and will produce identical bytecode (except for any byte offsets used for indicating where the content of constants are location, which is not information normally exposed to scripts at all so you can ignore it, plus the obvious differences due to variable names). Semantically, braces are a quoting mechanism and not an indicator of a list (or a script, or …)
You need to write your code to not assume that it can look things up by inspecting the type of a value. The type of 123 could be many different things, such as an integer, a list (of length 1), a unicode string or a command name. Tcl's semantics are based on you not asking what the type of a value is, but rather just using commands and having them coerce the values to the right type as required. Tcl's different to many other languages in this regard.
Because of this different approach, it's not easy to answer questions about this in general: the answers get too long with all the different possible cases to be considered in general yet most of it will be irrelevant to what you're really seeking to do. Ask about something specific though, and we'll be able to tell you much more easily.
You can try string is list $var1 but that will accept both of these forms - it will only return false on something that can't syntactically be interpreted as a list, eg. because there is an unmatched bracket like "aa { bb".

losing type information when recognizing tokens in LEX

I am trying to parse a config file using lex and yacc/bison.The sample file is shown below.
[section1]
attr1=1234567
attr2=848329832499934
[section2]
attr3=1233422
attr2=849999934834798
To recognize the values for the attributes listed above,I use the following regular expressions.
DIGIT [0-9]
NUM {DIGIT}+
Now in my specific example attr1 has a type of uint32_t and attr2 has a type of uint64_t
However I cannot recognize this.In otherwords I need to have a symbol table (which I statically define) from where I can lookup the type so that I can populate the appropriate type.
Is there any other approach to solve this problem? How do dynamically typed languages like python solve this issue.?
The usual way is to type the constant (or other token) based on the token itself. So if a sequence of digits token has value in the range 0..232-1, then it would be a unit32_t, otherwise it would be some other type (and corresponding token) capable of holding the value.
In the case of python, there's int type capable of holding integers that fit in a single word (what that is depends on the machine you're running on), and then there's long type capable of handling any larger integer.

Regular Expression Equivalent for other data types used for Data Validation

I am creating a data quality framework for a database that looks at single cells of each data type and sees whether or not their values are acceptable.
For data type string:
I just use a regular expression to define what is valid
For other data types (Integer, Timestamp, Boolean, TimeDelta, Float, ... ):
I don't have any standard way of recording what is valid
Is there an equivalent to Regular Expressions for other data types? Like IntegerRegEx's?
For example, lets say I have a field that must contain numbers between 0 and 65535, or I have a field that can only contain odd numbers...
It would be nice if this IntegerRegEx was also a string (just like normal RegEx's), so I could store IntRegEx's and StringRegEx's in the same table.
Thanks in advance!
If you want something that's a string and regex-like, you could just use regexes. Just have a standard way of converting each type to a string, and write regexes against the string form. It might be awkward for some and error-prone for others, but it's simple and doesn't involve creating your own expression language or loading code straight from the db and evaling it.
I guess depending on what language you're programming in, say PHP, you could store a mathematical expression (a string), for example $x >= 0 && $x <= 65535 or $x % 2 == 1.
With regex, you would write something like this, right?
if (!preg_match($regexFromDb, $fieldValueFromDb)) {
// validation fails
}
So with mathematical expressions, you'd do the same thing, e.g.
$x = $fieldValueFromDb;
if (!eval("return $mathExprFromDb")) {
// validation fails
}
This is just exemplary code. Of course you should safeguard your code against the dangers of running arbitrary stored executable code, and also against gibberish expressions crashing your script.
I think this is as close as you're gonna get, because the "IntegerRegEx" you seek already has a name... Math. ;)

Casting in Informix

In Informix, how can I cast a char(8) type into a money type, so that I can compare it to another money type?
Using "tblAid.amt::money as aid_amt" did not work.
Using "(tblAid.amt * 1) AS aid_amt" did not work.
try this -->
select (disb_amt::NUMERIC) disb_amt from tmp_kygrants;
You may be able to compare the amounts as numeric.
First question - why on earth are you not storing a numeric value in a numeric column? This would make the rest of your question moot. It would also mean that your system will perform better. When you need to store data values, use the obvious type; do not use a string type unless the data is a string.
As already noted, you can use the non-standard Informix cast notation:
SELECT some_column::MONEY FROM WhereEver;
You can also be more careful about the cast type - using MONEY(8,2) for example. You can also use the standard notation:
SELECT CAST(some_column AS MONEY(8,2)) FROM WhereEver;
This assumes you are using IDS 9.x or later -- older products do not support casts at all. However, in general, Informix is pretty good about doing conversions automatically (for example, converting numbers to strings). However, strings are compared lexicographically and not numerically, so a CAST is probably wiser in this context -- but avoiding the need for a cast by using the correct type in the first place is wiser still.
'tis been a while since I played around with informix and I don't have a running instance handy at the moment. However, there are two things that can cause a problem here:
1) since it is a char(8) it can contain values that can not be casted to numeric without a bit of 'cleanup'. E.g. "abc". Or "1,234,567.00".
2) Trailing spaces. (char as opposed to varchar).
What informix error do you get on your explicit cast (::money)?