Is there a way to simplify this further? - boolean-logic

I feel like I could simplify this more but I'm not able to.
If A ^ B Then
C = True
Else
C = False
End If
If A = True Then
D = True
Else
D = False
End If
Edit: I oversimplified my problem, it is my mistake. C and D are not boolean values but real values to assign to variables in a function.
Edit2: The function looks more like this:
If hasFoo() And hasBar() Then
C = getValue1()
Else
C = getValue2()
End If
If hasFoo() Then
D = getValue3()
Else
D = getValue4()
End If

C = (A ^ B)
if A is a boolean type in your language:
D = A
or
D = (A = True)

You could certainly do
If A ^ B Then
C = True
Else
C = False
End If
D = A

Related

Convert LUA table/json to KEY=VAL

I need to convert a lua table to KEY=VAL
e.g:
local t1 = {
t0 = "valt0",
t1 = "valt1",
tN = {
t0key = "t0var",
t1key = "t1var",
}
}
to be
t0="valt0", t1="valt1", tN_t0key="t0var", tN_t1key="t1var"
somebody have suggestions?
Make a function that loops through the table, checking if the value is table, in which case feed that table back into the same function. for anything that isn't a table, write it out to your new flattened table. Depending on how likely it is to happen, you might want to check there are no cyclic reference by tracking tables you've already iterated through.
local t1 = {
t0 = "valt0",
t1 = "valt1",
t2 = 42,
tN = {t0key = "t0var",
t1key = "t1var"}
}
local function convert(t)
local arr = {}
local cyclic = {}
local function convert_subtable(t, prefix)
assert(not cyclic[t], 'Cannot convert: cyclic reference detected')
cyclic[t] = true
for k, v in pairs(t) do
k = prefix..tostring(k)
if type(v) == 'number' or type(v) == 'boolean' then
table.insert(arr, k..'='..tostring(v))
elseif type(v) == 'table' then
convert_subtable(v, prefix..k..'_')
else
table.insert(arr, k..'='..string.format('%q', tostring(v)))
end
end
cyclic[t] = nil
end
convert_subtable(t, '')
table.sort(arr)
return table.concat(arr, ', ')
end
print(convert(t1)) --> t0="valt0", t1="valt1", t2=42, tN_t0key="t0var", tN_t1key="t1var"

MYSQL: select where / and / or with two tables

I am trying to get the following SQl working:
SELECT * FROM tc_appointment as tcAppointment, tc_message as tcMessage
WHERE tcAppointment.counsellor_id = 502
AND
(
(tcAppointment.tc_message_id = tcMessage.id AND tcMessage.marked_read = false AND tcMessage.sender_id != 502)
OR
(tcAppointment.cancelled = true AND tcAppointment.cancelled_acknowledged = false)
OR
(tcAppointment.confirmed = false)
);
The tcAppointment.tc_message_id is null in the only tc_appointment entry in the table. I am trying to get it to return as the counsellor_id = 502 and the second OR statment is true
I seem to be getting stuck because of the tc_message as tcMessage clause and the first AND / OR statement but I'm not sure why. Can anyone point me in the right direction please?
Try Joining the 2 tables and use the 'Where':
SELECT * FROM tc_appointment as tcAppointment
LEFT JOIN tc_message as tcMessage
ON
((tcAppointment.tc_message_id = tcMessage.id AND tcMessage.marked_read = false AND tcMessage.sender_id != 502)
OR
(tcAppointment.cancelled = true AND tcAppointment.cancelled_acknowledged = false)
OR
(tcAppointment.confirmed = false)
)
WHERE tcAppointment.counsellor_id = 502

about field update using function with case

I'm trying to update some field value depending two string field using a function with If and Case, function below:
Private Function checkDATI(tipotransazione As String, tipovendita As String) As String
Dim r As String
r = ""
If tipotransazione = "VENDITA" Then
Select Case tipovendita
Case "ARMI"
If Me.txtMatricola = "" Then r = "Matricola"
If Me.txtModello = "" Then r = "Modello"
If Me.txtCalibro = "" Then r = "Calibro"
If Me.txtTipoArma = "" Then r = "Tipo Arma"
If Me.txtFabrica = "" Then r = "Fabbrica"
Case "VENDITA ARMI/MUNIZIONI"
Case "MUNIZIONI"
End Select
End If
If tipotransazione = "ACQUISTO" Then
Select Case tipovendita
Case "ARMI"
If Me.txtMatricola = "" Then r = "Matricola"
If Me.txtModello = "" Then r = "Modello"
If Me.txtCalibro = "" Then r = "Calibro"
If Me.txtTipoArma = "" Then r = "Tipo Arma"
If Me.txtFabrica = "" Then r = "Fabbrica"
Case "VENDITA ARMI/MUNIZIONI"
Case "MUNIZIONI"
End Select
End If
checkDATI = r
End Function
And then when I call this function in a command button event as:
MsgBox (checkDATI(Me.CausaleMov, Me.txt_tipomov))
It's not updating that field. What is wrong?
When you say:
If Me.txtMatricola = "" Then r = "Matricola"
What you are doing is assigning the value: "Matricola" to the variable:r. Instead what I think you want is this:
If Me.txtMatricola = "" Then Me.txtMatricola = "Matricola"
This actually sets the value of the field: txtMatricola to "Matricola". This goes for all of the fields that are in your Case statements.

VBA nested if inquiry

I am trying to write write a nested if statement in VBA. In cobol, I would typically use the evaluate clause. But what do I use in VBA so as to avoid a long loop.
Example.
if cmbfield = "green" then
me.frame1.enable = true
else
me.frame2.enable = false
me.frame3.enable = false
end if
if cmbfield = "red" then
me.frame2.enable = true
else
me.frame1.enable = false
me.frame3.enable = false
end if
if cmbfield = "white" then
me.frame3.enable = true
else
me.frame1.enable = false
me.frame2.enable = false
end if
In the example you gave I'd use a switch command:
http://www.techonthenet.com/excel/formulas/case.php
Select Case test_expression
Case condition_1
result_1
Case condition_2
result_2
...
Case condition_n
result_n
Case Else
result_else
End Select
You could also do if ... elseif ... end if
http://www.techonthenet.com/excel/formulas/if_then.php
If condition_1 Then
result_1
ElseIf condition_2 Then
result_2
...
ElseIf condition_n Then
result_n
Else
result_else
End If
I do it this way:
Me.frame1.enabled = (cmbfield = "green")
Me.frame2.enabled = (cmbfield = "red")
Me.frame3.enabled = (cmbfield = "white")
Dim isGreen as Boolean, isRed as Boolean, isWhite as Boolean
isGreen = (cmbfield = "green")
isRed = (cmbfield = "red")
isWhite = (cmbfield = "white")
me.frame1.enabled = isGreen
me.frame2.enabled = isRed
me.frame3.enabled = isWhite
This is a shorter way to write the same code. Should work modulo syntax; hope this helps.

Using linebreak to break IF statement in Access VBA

I have a VBA conditional script as below:
If COMORBIDITY_CANCER = -1 Or COMORBIDITY_CHRONIC_SKIN_LESIONS = -1 Or COMORBIDITY_CHRONIC_LUNG_DISEASE = -1 Or COMORBIDITY_CARDIOVASCULAR_DISEASE = -1 Or COMORBIDITY_DM = -1 Or COMORBIDITY_IMMUNODEFICIENCY_OR_CHEMOTHERAPY = -1 Or COMORBIDITY_ADL = -1 Or COMORBIDITY_HEPATIC_DISEASE = -1 Or COMORBIDITY_RENAL_IMPAIRMENT = -1 Then
RS_REPORT.AddNew
RS_REPORT!REPORT_UID = REPORT_UID
RS_REPORT!CATEGORY1_ID = 2
RS_REPORT!CATEGORY2_ID = 1
RS_REPORT!CATEGORY3_ID = 1
RS_REPORT.Update
End If
I think the first line of it is too long and I would like to chop it using ENTER, like what can be done in R, but seems it is invalid in VBA, I tried
& _
but it also fails.
Any suggestions? Thanks!
Try this:
If COMORBIDITY_CANCER = -1 Or COMORBIDITY_CHRONIC_SKIN_LESIONS = -1 Or _
COMORBIDITY_CHRONIC_LUNG_DISEASE = -1 Or COMORBIDITY_CARDIOVASCULAR_DISEASE = -1 Or _
COMORBIDITY_DM = -1 Or COMORBIDITY_IMMUNODEFICIENCY_OR_CHEMOTHERAPY = -1 Or _
COMORBIDITY_ADL = -1 Or COMORBIDITY_HEPATIC_DISEASE = -1 Or _
COMORBIDITY_RENAL_IMPAIRMENT = -1 Then
You could try something such as:
Boolean b = false
If condition1 b = true
If condition2 b = true
. . .
If b ...