BASIC IF THEN using Like - oracle

I am currently trying to write an IF/THEN statement in BASIC but none of the 6 relational Operators seem to help me. I want to compare a variable (srName) and if it contains the letters TS I want the relevant statements to play out.
Just to add the bit of the code below for anyone that wants to see it. I have currently left LIKE in there to show what I want it to do. I know it won't work as is with that condition. The only line that is broke is the first one.
IF srName LIKE '%TS%' THEN
status = ChangeSpec(result, 'T')
return newDate
ELSEIF srName LIKE '%SD%' THEN
status = ChangeSpec(result, 'F')
return newDate
ELSE
return ""
ENDIF
As the code is I get an error stating i am missing an ENDIF which I clearly am not. And the reason i know the problem is specifically with the part
LIKE '%xx%'
is because when i remove that with a simple
IF (srName = TSA245) THEN
which for testing I have forced my variable to match then the code works fine

OK I have managed to solve it. LIKE won't work in any configuration but instead i used the InStr function like below. Hope this helps others.
IF (InStr(srName, "TSA") = 1) THEN
status = ChangeSpec(result, 'T')
return newDate
ELSEIF (InStr(srName, "SDA") = 1) THEN
status = ChangeSpec(result, 'F')
return newDate
ELSE
return ""
ENDIF
Note: This works because i know my variable srName will begin with TSA or SDA. If you don't know where in the string it will be use > 0 instead of = 1

Related

Expressing "equals" in pseudocode

I was just wondering if there is a special way of saying when something equals something. For example in python, if you declare something equals 2, you say something = 2, whereas when you check if something equals something else, you would say:
if something == somethingelse:
So my question is in pseudocode for algorithms if I'm checking to see if a entered password equals a stored password in an IF THEN ELSE ENDIF loop, would I use one or two equal signs:
WHILE attempts < 3
Get EnteredPassword
**IF EnteredPassword = StoredPassword THEN**
Validated = TRUE
ELSE
attempts = attempts + 1
ENDIF
ENDWHILE
Usually, pseudocode is very broad and every author has their own way of expressing it. As
Aziz has noted, usually x <- 1 is used for an assignment and x := x + 1 for an update. Read ':=' as 'becomes' instead of 'equals', however, they are interchangeably used. As for your question, both = and == are accepted answers, as long as it is clear to your reader what your intention is.
To express equals you use the equal mark symbol once, unlike in python where you use the symbol twice to compare two values (eg if variable == 'one'). An example syntax is:
variable = 'one'
WHILE variable = 'one' DO
SEND "hi" TO DISPLAY

Where did I go wrong in this M-Code function for power-query?

I'm trying to write an M-Code function that I can use to filter out text values that aren't of the format "A0000" or "B0000" or "C0000" etc.
I tried my hand at writing a custom function in Power-Query, but the nuances of this language seem to escape me and the documentation and the syntax error warnings are pretty much useless in troubleshooting.
Here's what I have so far. I'm getting an error "Token Identifier Expected" which highlights the very first if line when I click the show error button.
Does anyone know where I've gone wrong here, and what I might try to fix it?
let IsNewPN= (Argument) =>
let
/*M code to evaluate in the function goes here*/
a= Text.Start(Argument,1),
b= Text.End(Argument, Text.Length(Argument) - 1),
if a="A" or a="B" or a="C" or a="D" or a="E" or a="F" or a="H" or a="M" or a="Q" or a="W"
then x=1
else x=0
if x=0
then Result = "FALSE"
else Result=try a & Text.InferNumberType(b) otherwise "FALSE"
in
Result
in
IsNewPN
Each line needs to be of the form value = expression.
Try changing your code to this:
let IsNewPN= (Argument) =>
let
/*M code to evaluate in the function goes here*/
a= Text.Start(Argument,1),
b= Text.End(Argument, Text.Length(Argument) - 1),
x =
if a="A" or a="B" or a="C" or a="D" or a="E" or a="F" or a="H" or a="M" or a="Q" or a="W"
then 1
else 0,
Return =
if x=0
then "FALSE"
else try a & Text.InferNumberType(b) otherwise "FALSE"
in
Result
in
IsNewPN
In this code, x and Return are the 'token identifiers` for their respective lines.
Side note: You can simplify your big or expression using this instead:
Text.Contains("ABCDEFHMQW",a)

Write an SQR condition statement

I'm new in SQR. I need help to write a variable and use it for a condition statement. my pseudo code goes
declare $head_print
let $head_print = (select * from PS_DTR_RPT_ACCT
where RPT_TREE_NODE = 'REAL_ESTATE_EXP'
or TREE_NODE_NUM between 4600000 and 4699999)
if(head_print contain REAL_ESTATE_EXP or Account between 46000000 and 4699999)
then head_print = "REAL ESTATE";
else head_print = "Capital ESTATE";
It's not quite clear what you want so I'm making an assumption.
It seems it is if a certain value is in table PS_DTR_RPT_ACCT, then you want it to say "REAL ESTATE" otherwise say "CAPITAL ESTATE"
Now with SQR, you have to put your SQL in a begin-select block - rules are very strict - field names must be in column 1 - code underneath NOT in column 1. In the following routine, I've tried to code your pseudo code in real SQR, however, I could not test it so you may get errors - plus I don't know your field names since it just says "select *".
Begin-Report
do GetData
End-Report
Begin-Procedure GetData
! Initialize value - if no data found, query this later and set it to the "ELSE"
Let $Head_print = ''
Begin-Select
Head_Print
! Override the value from the table but only if you need to
Let $Head_Print = 'REAL ESTATE'
from PS_DTR_RPT_ACCT
Where RPT_TREE_NODE = 'REAL_ESTATE_EXP'
or TREE_NODE_NUM between 4600000 and 4699999)
End-Select
! If $Head_print is blank, then no value was found with the sql - do the ELSE part
If $Head_Print = ''
Let $Head_Print = 'Capital Estate'
End-If
End-Procedure
SQR is quite a nice finite language to learn - syntax somewhat strict, but simple as Basic with SQL. I do recommend reading the reference manual - it's downloadable from Oracle.
Feel free to ask any other questions about SQR - I get alerts if you do - sorry it took this long to answer

Is there a better way to write the following VB6 snippet?

I work at $COMPANY and I'm helping maintain $LEGACY_APPLICATION. It's written in visual basic 6.
I was faced with doing an unpleasantly elaborate nested if statement due to the lack of VB6's ability to perform short circuit evaluations in if statements (which would simplify this a lot). I've tried AndAlso, but to no avail. Must be a feature added after VB6.
Some genius on SO somewhere pointed out that you can trick a select case statement into working like a short-circuiting if statement if you have the patience, so I tried that, and here's what I came up with:
Select Case (True) ' pretend this is an if-else statement
Case (item Is Nothing): Exit Sub ' we got a non-element
Case ((item Is Not Nothing) And (lastSelected Is Nothing)): Set lastSelected = item ' we got our first good element
Case (item = lastSelected): Exit Sub ' we already had what we got
Case (Not item = lastSelected): Set lastSelected = item ' we got something new
End Select
It's definitely a little unusual, and I had to make use of my fantastic whiteboard (which, by the way, is pretty much the most useful programming resource besides a computer) to make sure I had mapped all of the statements correctly.
Here's what's going on there: I have an expensive operation which I would like to avoid repeating if possible. lastSelected is a persistent reference to the value most recently passed to this calculation. item is the parameter that was just received from the GUI. If there has never been a call to the program before, lastSelected starts out as Nothing. item can be Nothing too. Additionally, if both lastSelected and item are the same something, skip the calculation.
If I were writing this in C++, I would write:
if (item == NULL || (lastSelected != NULL && item->operator==(*lastSelected))) return;
else lastSelected = item;
However, I'm not.
Question
How can I rewrite this to look better and make more sense? Upvotes will be awarded to answers that say either "YES and here's why: X, Y, Z" or "NO, and here's why not: X, Y, Z".
Edits
Fixed the C++ statement to match the VB6 one (they were supposed to be equivalent)
This is shorter and 100x more readable.
EDIT Wug edited the code in MarkJ's original answer, into this:
If (item Is Nothing)
Then Exit Sub ' we got a non-element
ElseIf (lastSelected Is Nothing) Then
Set lastSelected = item ' we got our first go
ElseIf (item = lastSelected) Then
Exit Sub ' we already had what we got
End If
Set lastSelected = item ' we got something new
Here's MarkJ's edit in response. One nested if, but only one Set. Seems neater to me.
If (item Is Nothing) Then
Exit Sub ' we got a non-element
ElseIf Not (lastSelected Is Nothing) Then ' not our first go
If (item = lastSelected) Then
Exit Sub ' we already had what we got
End If
End If
Set lastSelected = item ' we got something new
' does stuff here? #Wug is that true?
To compare reference equality in VB6 use item Is LastSelected. Because item = lastSelected will probably evaluate the default properties in the objects and compare those instead!
Since brevity appears to be a goal, consider this. If you Exit Sub when condition X is True, you don't need to check X again later. It is False! Unless it changes its value in between evaluations (e.g. X is a function that checks the system clock). You were checking whether item was lastSelected, then whether it wasn't. And if item Is Nothing is False, do not bother to check whether item Is Not Nothing is True!
VB6 does not short circuit for backwards compatibility with ancient versions of Basic
Stop worrying that VB6 is not some other language and relax!
YES
I translated it from your case statement. I find it easier to read, personally.
If Item Is Nothing Then
Exit Sub ' we got a non-element
ElseIf LastSelected Is Nothing Then
Set LastSelected = Item ' we got our first good element
ElseIf Item = LastSelectedItem Then
Exit Sub ' we already had what we got
Else
Set LastSelected = Item ' we got something new
End If
You asked for explanation. I tried not to have to give much (by re-using your own code comments).
But here it is anyway :-)
Firstly if there is no item, just exit. Easy.
Otherwise if LastSelected Is Nothing then we know, because the first if condition failed, that Item exists, and it's safe to mark that value as having been Last Selected. As you say, we got our first good element. The sub continues on.
However if we have existing values for Item and LastSelected, then either they are equal or not. If they are equal, just quit.
If they aren't equal, then update LastSelected. As you say, we got something new.
You can use a helper function like this:
Private Function pvGetItemData(oItem As ListItem) As Variant
If Not oItem Is Nothing Then
pvGetItemData = oItem.Tag
Else
pvGetItemData = -1
End If
End Function
and then
If pvGetItemData(Item) = pvGetItemData(LastSelected) Then
' cache hit
Else
' do calc
Set LastSelected = Item
End If
YES
I'd make that simpler:
If item Is Nothing Then
Exit Sub ' we got a non-element
Else
Set lastSelected = item ' we got something to assign
End If
Unless there are side effects assigning lastItem (it can be property with invalid assignment code), then code logic is essentially same.
If you are not required to exit the sub (snippet is at the end of sub or something), then next is even simpler:
If Not (item Is Nothing) Then Set lastSelected = item
BTW, your Select Case (True) looks really odd to VB programmer :)

Problem with LINQ Where Clause

var results = from formNumber in context.DetailTM
join c in context.ClaimPeriodTM on formNumber.ClaimPeriod equals c.Cid
where formNumber.FormNumber.StartsWith(fNumber)
&& formNumber.RegistrationNumber != registrationNumber
select new { RegNo = formNumber.RegistrationNumber,
CP = c.ClaimPeriod, FormNo = formNumber.FormNumber };
The AND CLAUSE with .StartsWith Doesn't work. If I use == operator the query works fine. I tried adding brackets to the where clause but it didn't help. Any idea what is missing. Thank you in Advance.
Try something very basic like "&& true == false" and see if that fails. Also reverse the order. If you reverse the order, is it still the second clause that fails?
There's nothing about StartsWith that should cause this behavior. There's something else going on here. Trim and move elements until you isolate the real bug.
What is the database type for FormNumber? If its char (and not varchar), then you may have to trim one or both sides of the comparison.
where
formNumber.FormNumber.Trim().StartsWith(fNumber.Trim())
&& formNumber.RegistrationNumber != registrationNumber
Same as jrummells answer, just targetted by the additional comments.
formNumber.RegistrationNumber != registrationNumber
Check the database types here. If they are fixed length (char or nchar), you may need to do some trimming.

Resources