I have the following expression in a Derived Column transformation.
((DT_STR,1,1252)outGender == "M" || (DT_I1)outGender == 1) ? 1 : (((DT_STR,1,1252)outGender == "F" || (DT_I1)outGender == 2) ? 2 : 3
When I run the package, if the "outGender" column contains a "1" or "M", the GenderId column becomes 1. If "outGender" is "2", the GenderId column is "2". So far, this is correct. If, however, "outGender" contains "F" then GenderId will be NULL. This is wrong.
Why does this happen and how do I fix it? If I switch the condition strings, placing the "F" conditions in the first part of the expression and "M" conditions in the second part, then NULL appears in the GenderId column for rows when the "outGender" column contains "M" values.
So this transform works fine without casting your values as integers:
((DT_STR,1,1252)outgender == "M" || (DT_STR,1,1252)outgender == "1") ? 1 : (((DT_STR,1,1252)outgender == "F" || (DT_STR,1,1252)outgender == "2") ? 2 : 3)
And if outGender is already a DT_STR type you can simplify it and just use
(outgender == "M" || outgender == "1") ? 1 : ((outgender == "F" || outgender == "2") ? 2 : 3)
Related
I have the following script that I use for part of my count in payment_status from Mysql. I have seen some examples of this online, but I can't seem to figure out how to make this work WITH a Null instance. Any insight would be appreciated.
Note: The way I have it now doesn't work when Null is in the database. I'm guessing because that isn't an inserted word?
Objective: To add $row['payment_status'] == "Null" to this with the rest of the script.
if($row['payment_status'] == "Completed" || $row['payment_status'] == "Free" || $row['payment_status'] == "Cancelled" || $row['payment_status'] == "Null")
if payment_status column has default value NULL, Then try below change
if($row['payment_status'] == "Completed" || $row['payment_status'] == "Free" || $row['payment_status'] == "Cancelled" || $row['payment_status'] == null)
Without thinking in C# I tried to compare three objects. It failed, and explained why [since (typeof("A == B") == bool) and (typeof(C) != bool) it was an invalid comparison]. Do any languages support short circuiting logic like this?
There are several languages that let you do multiple conditionals like this.
But the first I could think of was Python
Example:
a = 5
b = 5
c = 5
if(a == b == c):
print "yes"
else:
print "no"
Will print "yes" in the console.
It works with other types as well, like this:
a = ["A",1]
b = ["A",1]
c = ["A",1]
d = ["A",1]
if(a == b == c == d):
print "YES"
else:
print "NO"
Now the reason for C# (And other C like languages) doesn't support this is that they evaluate comparison expressions down to a true / false, so what your compiler sees when you do (5 == 5 == 5) is ((5 == 5) == 5) which yields: (true == 5) which invalid since you cannot compare a Boolean to an integer, you could actually write (a == b == c) if c is a Boolean, so (5 == 5 == true) would work.
I have this expression:
!(1 && !(0 || 1))
The output returns 1 true. And that's ok. When I read the expression I came to the same conclusion before checking the output. But I would really appreciate if someone can explain to me why the returning value is true, that way, I will have a better understanding of boolean logic and how to implement better evaluators in my code.
Key observation here: ! is not, && is the "And" operator, and || is the "Inclusive Or" Operator.
What are you really asking when you say "why it's true?".
0 = false
1 = true
AND && table
0 0 -> 0
0 1 -> 0
1 0 -> 0
1 1 -> 1
OR || table
0 0 -> 0
0 1 -> 1
1 0 -> 1
1 1 -> 1
NOT ! table
0 -> 1
1 -> 0
With parentheses implying "do this first", the statement reduces using the tables above:
!(1 && !(0 || 1))
!(1 && !1)
!(1 && 0)
!0
1
But I don't know "why" it's true. Because that's what an AND operation is, what an OR operation is, and what a NOT operation is, and how reducing a statement works. With those definitions, it can't be another answer, so it's that answer. But you already know that, because you did it yourself and got the same answer ... so what does the question mean?
The innermost expression (0 || 1) is always true.
So !(0 || 1) is always false.
That leaves 1 && 0, which is always false.
So !(false) is always true.
Please forgive my freely intermixing 0/false and 1/true.
The human evaluator (:-).
Working through the expression, following order of operation:
!(1 && !(0 || 1))
= !(1 && !(1))
= !(1 && 0)
= !(0)
= 1
Step by step explanation:
1 = true
0 = false
Starting point: !(1 && !(0 || 1))
Lets start with the inner most expression: !(0 || 1)
Var1 || Var2 =
Var1 or Var2 =
If Var1 or Var2 is 1 or both are 1, the result is 1.
(0 || 1) = 0 or 1 -> the second variable is 1 so the expression is 1.
Insert the result (0 || 1) = 1 into Startingpoint: !(1 && !(1))
! = not (inverts the value of what is behinde)
!1 = 0
!0 = 1
!(0 || 1) = !(1) = 0
Insert the result !(1) = 0 into Startingpoint: !(1 && 0)
So we have !(1 && 0)
Var1 && Var2 = And =
the opossite of or =
If Var1 AND Var2 are both 1, the result is 1. Else it is 0 =
If Var1 or Var2 is 0, the result is zero
1 && 1 = 1
1 && 0 = 0
everything else: 0
So this is left: !(0)
Reminder: ! = not = inverts the expression behind it. So !0 = 1 (and !1 = 0)
This is 1. Or in your case: true
A good book for Beginner C programmers and people who want to learn about programming and logic in an easy, understandable way:
C for Dummies by Dan Godkins
!(1 && !(0 || 1))
Since, you have used parenthesis, evaluation takes place according to them.
First, evaluate innermost parenthesis.
0 || 1 => always true.
!(0 || 1) => !(true) => always false.
1 && !(0 || 1) => 1 && false => always false.
!(1 && !(0 || 1)) => !false => always true.
I am fairly new to Haskell and am working on an assignment simulating checkers currently. I am having a bit of difficulty determining the proper method of conditionally checking an expression and updating the values of a tuple. I have a function called getPos that will return the Char at a specific location on the board to determine its state.
onemove :: (Int,[Char],[[Char]],(Int,Int)) -> (Int,[Char],[[Char]])
onemove (a,b,c,(d,e))
| e <= 0 =(a-30,b,c)
| e > 50 =(a-30,b,c)
| (((posTo == 'r') || (posTo == 'i')) &&((posFrom == 'w')||(posFrom == 'k'))) == 'true' =(a-20,b,c)
| (((posTo == 'w')||(posTo == 'k')) && ((posFrom == 'r') || (posFrom == 'i')))== 'true' =(a-20,b,c)
| otherwise = (1000,b,c)
where posFrom = getPos (d, c)
posTo = getPos (e,c)
Is it correct to use a function to define a variable within my where clause? I receive the following error on my last line:
parse error on input `='
Your immediate problem is mostly just caused by indentation. Guards need to be indented w.r.t the definition they're associated with.
onemove :: (Int,[Char],[[Char]],(Int,Int)) -> (Int,[Char],[[Char]])
onemove (a,b,c,(d,e))
| e <= 0 =(a-30,b,c)
| e > 50 =(a-30,b,c)
| (((posTo == 'r') || (posTo == 'i')) &&((posFrom == 'w')||(posFrom == 'k'))) =(a-20,b,c)
| (((posTo == 'w')||(posTo == 'k')) && ((posFrom == 'r') || (posFrom == 'i'))) =(a-20,b,c)
| otherwise = (1000,b,c)
where posFrom = getPos (d, c)
posTo = getPos (e,c)
Notice I also removed the == 'true' in your original code. That was wrong for three separate reasons.
Single quotes denote a Char. Double quotes for String.
You can't compare a Boolean value to a String just because that String
happens to say "true". You would have to say == True.
There's no reason to ever write bool == True, because that's
exactly the same as just writing bool.
Also, a, b, c, and (d,e) should probably all be separate arguments, not a single tuple. You lose all the advantages of currying that way.
I have asked this question before and I believed to have got the correct answer but i didnt.
What I am trying to achieve is Column 5:
Column 1 Columns 2 Column 3 Column 4 Column 5
A B NULL D A|B|D
B C B|C
NULL D NULL NULL D
I used a derived column:
(DT_STR,50,1252)((Column1 == "" ? "" : Column1 + "|") + (Column2 == "" ? "" : Column2 + "|") + (Column3 == "" ? "" : Column3 + "|") + (Column4 == "" ? "" : Column4))
But I ended with:
Column 1 Columns 2 Column 3 Column 4 Column 5
A NULL NULL D NULL
If I get NULL the overall answer is NULL
Try this:
(DT_STR,50,1252)((Column1 == "" || isnull(Column1) ? "" : Column1 + "|") +
(Column2 == "" || isnull(Column2) ? "" : Column2 + "|") +
(Column3 == "" || isnull(Column3) ? "" : Column3 + "|") +
(Column4 == "" || isnull(Column4) ? "" : Column4))
===
Hi Try using
ISNULL(Value) ? " " : Value
for each column
Mario