Ternary operator blowing up - coldfusion

I would like some help with my syntax for the following ternary operator (which keeps blowing) in Coldfusion:
iif(structKeyExists(session, "newUser") ? session.newUser.planId : 0)
It's part of the following model call:
user = model("user").new(UUID=createUUID(), planId=iif(structKeyExists(session, "newUser") ? session.newUser.planId : 0));
It keeps blowing up, however:
Parameter validation error for the IIF
function. The function takes 3
parameter.

According to Adobe's documentation, the function syntax for iif looks like this:
IIf(condition, string_expression1, string_expression2)
So in your case, you would call it like this:
iif(structKeyExists(session, "newUser"), session.newUser.planId, 0)
This is different from the ternary operator (?:), which is described here and follows the following syntax:
(Boolean expression)? expression1 : expresson2
Which, in your case, would look like this:
planId=structKeyExists(session, "newUser") ? session.newUser.planId : 0

Related

How I can evaluate arithmetic expression of an macro function to pass to another macro function in C Preprocessor?

how can I make this example code work?(In C or C++)
The cout is just for example.I want evaluate the correct decremented number
#define PRINT_1 std::cout<<"One : " <<1;
#define PRINT_2 std::cout<<"Two : " <<2;
#define DEC_AND_PRINT(number) PRINT_##number-1
When I call DEC_AND_PRINT(3) , I expect this:
DEC_AND_PRINT(3) PRINT_##(3-1) -> PRINT_2 -> std::cout<<"Two : "<<2
But the compiler give-me an error :
GCC : error:
'PRINT_3' was not declared in this scope...
GCC : note:
in expansion of macro 'DEC_AND_PRINT' DEC_AND_PRINT(3)
How I can decrement the argument?
Basically, I'm trying to make a macro function get a number and call another macro function in syntax _name_of_macro_decremented_number.
Thanksx for all.
There is no way to do what i wanna do.The Preprocessor cannot evalute an expression an use the result in an concatenation ##.
So im change the code to use recursion , and write all combinations in hand,like:
CALL DefinePointer(T)
DefinePointer_1(T)
Define T * -> Call DefinePointer_2( T*)
DefinePointer_2( T*)
Define T** -> Call DefinePointer_3(T**)
DefinePointer_3(T**)
Define T*** -> Call DefinePointer_4(T****)
This is only a piece of code, but there is a lot more. I had to write a lot but in the end it worked, and it was possible to write algorithms and data structures for any kind of data.

If statement inside parenthesis changes the value in a vector

I have a following problem.
I have two vectors correct_data_labels.label and data_labels.label , label is the vector<int > whereas data_labels and correct_data_labels are the instance of my class.
I have a method where I have if statement inside it. When I use IF statement, the argument that I used inside the parenthesis does an arithmetic and changes the value. As seen in the code below: When I run the code it replaces the value written inside an IF statement. So data_labels.label[row] is replaced by correct_data_labels.label[row]
unsigned int num=0;
double percentage=0.00;
for(register unsigned int row=0;row<data_labels.label.size();row++)
{
if((data_labels.label[row]=correct_data_labels.label[row]))
{
num=num+1;
}
}
percentage = (num/data_labels.label.size());
This code is written in c++, I suppose other programming paradigms can answer the above query too.
You used the assignment operator = instead of the equality operator ==. You probably got a warning about it which is why you have an extra set of parenthesis around the expression: suggest parentheses around assignment used as truth value (on my compiler at least)
Replace = with == to do the comparison instead of the assignment.

What does the minus/subtraction operator in front of function do in C++?

I have seen people using the subtraction (the char '-') operator in front of a function.
For example calling a function like this:
-myFunction(someParameter);
I don't know what this does, and Googleing it is not helping.
Can anyone please explain what this does, or what it is called, so i will be able to google it properly. Thank you.
It means that the function returns a type that can have the unary - (minus) operator applied to it.
int myFunction()
{
return 42;
}
...
int x = -myFunction();
The value of x will be -42 in the example above.

if then else in ocamlyacc

Can anyone brief how can I implement if then else in Ocamlyacc. I've defined tokens from lexical analyzer(ocamllex) namely IF, THEN, ELSE. For the if statement, I have defined tokens: GREATERTHAN, LESSERTHAN, EQUALTO for integers. I've searched many tutorials but to no avail!
UPDATE:
I want to interpret the result and return the value of the expression dictated by the if-else statement.
You have to define rules :
ifthenelse :
| IF condition THEN statement ELSE statement { IfThenElse($1,$2,$3) }
condition :
| INT EQUALTO INT { Cond(EqualTo,$1,$3) }
| INT LESSERTHAN INT { Cond(LesserThan,$1,$3) }
| INT GREATERTHAN INT { Cond(GeaterThan,$1,$3) }
Don't forget to define regular expression for int, in your lex fil
Perhaps you've seen it, but the OCaml manual gives a complete ocamllex/ocamlyacc example that calculates the values of expressions: Desk Calculator Example.
The example shows that you can calculate your result in the ocamlyacc actions if you want to. For a simple example, it's not at all hard to follow. In a more realistic case, you would probably want to construct an abstract syntax tree for later processing (such as evaluation). The code has a similar flavor except that the cases are given by the different constructors of your AST type rather than by the different grammar rules.

Replace if-else with ?: in c++

The original code is:
if (expression1) statement1;
else statement2;
is it possible to transform it into this?
expression1 ? statement1 : statement2
or it depends on the compiler? it seems that this differs within c++ standards?
Sometimes the case is if (expression1) statement1; and how can i transform that?
btw, it can be done in c.
Making the source code unreadable is exactly what i am trying to do
This is just one of the steps
error: second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'
This is what i got with g++ (TDM-2 mingw32) 4.4.1 when compile
#include <stdio.h>
void _(int __, int ___, int ____, int _____)
{
((___ / __) <= _____) ? _(__,___+_____,____,_____) : !(___ % __) ? _(__,___+_____,___ % __, _____) :
((___ % __)==(___ / __) && !____) ? (printf("%d\n",(___ / __)),
_(__,___+_____,____,_____)) : ((___ % __) > _____ && (___ % __) < (___ / __)) ?
_(__,___+_____,____,_____ + !((___ / __) % (___ % __))) : (___ < __ * __) ?
_(__,___+_____,____,_____) : 0;
}
int main() {
_(100,0,0,1);
return 0;
}
and if i replace the last 0 with throw 0, it will compile successfully.
expression1 ? statement1 : statement2 This is actually incorrect. The correct is this:
expression1 ? expression2 : expression3
Not any statement can be equivalently transformed into a single expression, so in general it is not always possible. For example:
if(expr)
{
for(int i = 0; i < 2; ++i) {std::cout << i; }
}
else
{
//something else
}
You can't transform this into ?: expression because for is a statement, not an expression.
Btw. It can't be done in standard C. What you are referring to is probably the statement expression which is a GCC extension.
You can transform the single if
if (expression1) expression2;
with a trick of the (void(0)) this ways
expression1 ? expression2 : (void(0)) ;
but I am not suggest to do it/use it !
the ? : can lead to spaghetti code and try to avoid it. Its better to have a clear and easy to read and understand code.
Also the code is the same on both ways and there is not special reason to make a hard to read code.
Making the source code unreadable is exactly what i am trying to do
Why? The only people you are going to affect are the maintainers of your code. If you are trying to keep hackers from understanding your code, don't bother. They know every trick in the book, and several that aren't in the book.
The code:
expression1 ? statement1 : statement2
works the same in C++ as it does in (GCC) C.
The code:
if (expression1) statement1;
Is already as simple as things go. Why would you even want to transform this?
In addition to Armen answer, it may be mentioned that C grammar defines statement as one of the following: labeled_statement, compound_statement, expression_statement, selection_statement, iteration_statement and jump_statement. Of all of them, only expression statement is, actually, an expression.
Not related to the ?: issue, but I don't think it is legal to define your own identifiers with double underscores, or identifiers beginning with underscores in the global namespace - see this question.