comparison of variables string and number in robotframework - robotframework

Run Keyword If "${numRows1}"=='None' Set Variable 0 ELSE ${numRows1}=>0 Set Variable ${numRows1}
Log ${numRows1}
how can I compare the values? and retrieve the result?

... ELSE ${numRows1}=>0
This part here looks like another comparison, thus you probably meant ELSE IF.

Related

How to have a SQLite query with a variable part?

Hi I am trying to create a query for SQLite which has a variable part in it. By variable I mean that a certain part within the string can possibly contain a variable but also an empy value
I tried this but I am not sure whether this works.
SELECT * FROM table WHERE attr LIKE 'ABC% %DEF'
Adding onto my comment, check the below code to test your values.
SELECT CASE WHEN 'ABC G DEF' LIKE 'ABC%DEF'
THEN 1
ELSE 0 END as test_space,
CASE WHEN 'ABCGGGDEF' LIKE 'ABC%DEF'
THEN 1
ELSE 0 END AS test_all

How can I dynamically change the where conditions of a for each loop?

I have a table of records that has two logical flags, holdn and holdl. I want to loop through this table with 3 different criteria.
Either flag is TRUE - We want to see everything that is on hold
Flag holdl is TRUE - We only want see items that are on hold for this one reason
Flag holdn is TRUE - We only want to see items that are on hold for this other reason.
I cannot figure out how to dynamically change the for each loop based on this. What I have tried so far is to set the value of a variable based on these conditions and then use the content of the variable as one of the where parameters. This does not work as Progress complains that there is a data mismatch. The variable is a string, the flags are logical, so that does make sense. See sample code below. This is a snippet of the actual code with the the table name changed. The which-hold, order-from, etc variables are defined and set in a different module which calls this one.
DEFINE VARIABLE which-hold# AS CHARACTER FORMAT "x(30)" NO-UNDO.
CASE which-hold:
WHEN "B" THEN which-hold# = "(widget.holdn or widget.holdl)".
WHEN "L" THEN which-hold# = "widget.holdl".
WHEN "N" THEN which-hold# = "widget.holdn".
END CASE.
for each widget where which-hold# and
widget.order-no >= order-from and widget.order-no <= order-thru and
widget.line-no >= line-from and widget.line-no <= line-thru and
widget.joint-no >= joint-from and widget.joint-no <= joint-thru
no-lock:
A bunch of code to make a nice report with the retrieved records...
end.
Self taught Progress programmer here, who has inherited a huge, poorly documented application. Please be gentle.
If you would prefer not to deal with handles a semi-dynamic approach is also possible:
define variable i as integer no-undo.
define query q for customer.
do while true:
update i.
case i:
when 0 then quit.
when 1 then open query q for each customer no-lock where custNum >= 1000.
when 2 then open query q for each customer no-lock where state = "nh".
otherwise open query q for each customer no-lock where name begins "u".
end.
do while true with frame a:
get next q.
if not available customer then leave.
display custNum name state with frame a 10 down.
down with frame a.
end.
close query q.
end.
What you want is actually a dynamic query. I'll get to it at the end, but first I'd like to explain why you won't be able to try and substitute the field name in the which-hold# variable: because the query is evaluated at compile time. And this is what it reads (supposing which-hold# has a value of widget.holdn
FOR EACH widget where "widget-holdn" (...)
And that does not evaluate to TRUE or FALSE. So what, you ask? Well, that is the key here. Every condition needs to evaluate to true or false, so you'd be more in luck if you try
for each widget where (if widget-hold# = 'widget.holdn' then widget.holdn = true else TRUE) (...)
Again, notice the condition will exist if widget-hold# has the value I want, otherwise it doesn't filter on this at all.
So you can just code the way I showed (for each of the conditions you have) and it should work fine.
BUT let me suggest a dynamic query instead.
You need to have:
DEFINE VARIABLE hQuery AS HANDLE NO-UNDO.
CREATE QUERY hQuery.
hQuery:SET-BUFFERS(BUFFER widget:HANDLE).
hQuery:QUERY-PREPARE('<THIS IS THE CORE>').
hQuery:QUERY-OPEN().
DO WHILE hQuery:GET-NEXT():
A bunch of code to make a nice report with the retrieved records...
END.
So in the core you have a string that corresponds to your for each the way you want it to look. So it should be for example (store this in a variable, or assemble it inside the query prepare, it doesn't matter):
'FOR EACH widget NO-LOCK WHERE ' +
(if which-hold = 'B' then 'widget.holdn = true and widget.holdl = true'
else if which-hold = 'L' then 'widget-holdl = true'
else /* N */ 'widget-holdn = true').
Remember I said your query is evaluated at compile time? Well, just so you know, dynamic queries on the other end are evaluated at run time, so be prepared for errors to pop up only when you run. Another thing I should mention is dynamic queries are slower than static ones, so please evaluate and choose your poison :)
This should be what you need. Please let me know if it's helpful or any questions remain.

R if statement with condition >1 length error

I'm trying to run an if statement, where I want to run something IF any of 23 values is below a certain value.
test.df<-as.data.frame(c(1:50))
if (test.df[,c(27:50)] <30){ print("hi")}
I get the error that the condition has length > 1 and only the first element will be used. Which is true... Does anyone know how I can test this if statement for 23 values, whithout having to type them one by one?
Thanks!
See the functions all or any, isTRUE and in newer versions of R, isFALSE, where the latter two to some degree takes care of fringe cases of NULLs and NAs.
For your example:
if (all(test.df[,c(27:50)] <30)) {
print("hi")
}
you can try this:
if (any(test.df[,c(27:50)] <30)){ print("hi")}

How to check if decimal is valid value in Teradata

How to check if the given decimal is valid. I usually do a case statement like below to check if column is invalid or NULL then set it to 0 else take it as it is:
case when decimal_column is NULL or decimal_column NOT BETWEEN -999999999999 AND 999999999999 then 0 else decimal_column end
Can anyone please let me know if the above query looks correct
Thanks
In Teradata 14.10 or greater, the TO_NUMBER() function can be used.
SELECT TO_NUMBER({decimal column})
FROM {table};
If the conversion to number failed, TO_NUMBER() returns NULL. See the SQL Functions,Operators, Expressions and Predicates manual for more details.
You can also use trycast. Something like ..:
trycast(trim(col1)) as DECIMAL (XX,Y))
From TD-Doku: TRYCAST takes a string and tries to cast it to a data type specified after the AS keyword (similar to CAST). If the conversion fails, TRYCAST returns a NULL instead of failing.

R how to check each element then return a boolean

I want to check a list named "answer,if it contains FALSE element,then return a boolean (F),if there is no any FALSE element,in other words all TRUE,return a boolean (T)
here is the code(not good too many lines,but I don't know the easier one)
>answer=c(TRUE,FALSE,FALSE)
>l=length(answer)
>ind <- 1
>t=0
>f=0
>while(ind<(length(answer)+1)){
>ifelse(answer[ind]==TRUE,t<-t+1,f<-f+1)
>ind<-ind+1
>}
>ifelse(f>0,print("False"),print("True"))
This part code could give me right result.BUT it give me twice!!
like this:
[1] "True"
[1] "True"
WHY WHY give me twice...please help me I don't want to use this function so many lines
The reason you get the print twice is that ifelse(f>0,print("False"),print("True")) will first evaluate f>0 (which is TRUE), and then evaluate print("False") (which will result in "False" being printed to the console), and finally the ifelse will return the value of print("False") ("False") which will then be auto-printed to the console (the second print).
To get around the double print you could replace your last line with
> ifelse(f>0,"False","True")
Note however that neither this code or your code is returning booleans, both of them are returning character-strings. If you want a boolean you should instead use
> ifelse(f>0,FALSE,TRUE)
There are however a number of things that could be done to improve this code. You could use a for-loop to iterate directly over the answer vector (eliminating the need for the ind variable. Even better, you could use the fact that R is allowing you to add the value of two booleans (TRUE=1, and FALSE=0), so sum(answer) will give you the number of TRUE-values in the vector.
The easiest way to solve this is doing what baptiste is hinting at all(answer) will evaluate to TRUE if answer only contains TRUE-values, which seems to be what you want.

Resources