Is there a way to prevent invalid row range in powerbuilder - exception

Is there a way to prevent invalid row range in powerbuilder.
IF dw_lista_campanias.GetSelectedRow(0) > 0 AND dw_lista_campanias.object.est_camp[dw_lista_campanias.GetRow()] = 'EO020' THEN
when dw_lista_campanias.object.est_camp index is 0 an exception is throw.
Invalid row range at line 193 in ue_opcion4 event of object w_os0210_mantenimiento_campanya.

You could put this statement in a TRY/CATCH block, but I'd think it'd be easier just to capture GetRow() into a variable and test it for 0 (which is a fairly normal state) before using it to access data.
Good luck.

Change your codes as below:
IF dw_lista_campanias.ROWCOUNT() > 0 THEN
IF dw_lista_campanias.GetSelectedRow(0) > 0 AND dw_lista_campanias.object.est_camp[dw_lista_campanias.GetRow()] = 'EO020' THEN
//PUT YOUR CODE HERE
END IF
END IF
Happy coding ( from pb developer :) )

I will assume there is a retrieve. E.g. ll_rowsrtn = this.retrieve().
If ll_rowsrtn > 0 then
//the getselectedrow script
End if
So the command will not execute unless datawindow has greater than 0 rows

Related

Can I include conditional logic in VS Code snippets?

I would like to write a snippet in VS Code that writes a "switch" expression (in Javascript), but one where I can define the number of cases.
Currently there is a snippet that produces the outline of a switch expression with 1 case, and allows you to tab into the condition, case name, and the code contained within.
I want to be able to type "switch5" ("5" being any number) and a switch with 5 cases to be created, where I can tab through the relevant code within.
I know the snippets are written in a JSON file, can I include such conditional logic in this, or is it not possible?
Thanks!
The short answer is that you cannot do that kind of thing in a standard vscode snippet because it cannot dynamically evaluate any input outside of its designated variables with some limited workarounds like I'll mention next.
You might - I and others have written answers on SO about his - type your various case values first and then trigger a snippet tat would transform them into a switch statement. It is sort of doing it backwords but it might be possible.
There are extensions, however, that do allow you to evaluate javascript right in a snippet or setting and output the result. macro-commander is one such extension. I'll show another simpler extension doing what you want: HyperSnips.
In your javascript.hsnips:
snippet `switch(\d)` "add number of cases to a switch statement" A
``
let numCases = Number(m[1]) // 'm' is an array of regex capture groups
let caseString = ''
if (numCases) { // if not 'switch0'
let tabStopNum = 1
caseString = `switch (\${${tabStopNum++}:key}) {\n`
for (let index = 0; index < m[1]; index++) {
caseString += `\tcase \${${tabStopNum++}:value}:\n\t\t\$${tabStopNum++}\n`
caseString += '\t\tbreak;\n\n'
}
caseString += '\tdefault:\n'
caseString += '\t\tbreak;\n}\n'
}
rv = `${caseString}` // return value
``
endsnippet
The trickiest part was getting the unknown number of tabstops to work correctly. This is how I did it:
\${${tabStopNum++}:key}
which will resolve to ${n:defaultValue} where n gets incremented every time a tabstop is inserted. And :defaultValue is an optional default value to that tabstop. If you don't need a defaultValue just use \$${tabStopNum++} there.
See https://stackoverflow.com/a/62562886/836330 for more info on how to set up HyperSnips.

word() function returns the different results depending on gnuplot version 5.06 and 5.22

I recently updated gnuplot latest version, 5.22 and my code didn't work properly. I debugged and found the reasons.
str="1 2"
print word(str,3)+0
In the previous version, 5.06 or older, the print shows 0 values without error.
But the latest version got error, "Non-numeric string found where a numeric expression was expected"
Without +0, both results are the same, blank (no output), but the latest version treats it as string I think.
My code has lots of routine related to word(), so how do I resolve this problem in the new version?
Your code seems to make two potentially dangerous assumptions:
that requesting the third element from a list of two elements returns an empty string, rather than causing an error, and
that converting that empty string to a number will yield 0.
Assumption 1 seems to still hold in gnuplot 5.2.2, but assumption 2 does not. If you really wanted that then you could create a wrapper
f(x) = (x eq "" ? 0 : x)
and use f(word(str,3)) instead of word(str,3). However, there might be a better way to deal with non-existing elements.
Use words to check the index:
w2num(list, i) = (i > 0 && i <= words(list)) ? word(list, i)+0 : 0
Example:
w2num(list, i) = (i > 0 && i <= words(list)) ? word(list, i)+0 : 0
l = "10 20"
do for [i=-1:3] { print w2num(l, i) }
prints
0
0
10
20
0

How do I set a function to a variable in MATLAB

As a homework assignment, I'm writing a code that uses the bisection method to calculate the root of a function with one variable within a range. I created a user function that does the calculations, but one of the inputs of the function is supposed to be "fun" which is supposed to be set equal to the function.
Here is my code, before I go on:
function [ Ts ] = BisectionRoot( fun,a,b,TolMax )
%This function finds the value of Ts by finding the root of a given function within a given range to a given
%tolerance, using the Bisection Method.
Fa = fun(a);
Fb = fun(b);
if Fa * Fb > 0
disp('Error: The function has no roots in between the given bounds')
else
xNS = (a + b)/2;
toli = abs((b-a)/2);
FxNS = fun(xns);
if FxNS == 0
Ts = xNS;
break
end
if toli , TolMax
Ts = xNS;
break
end
if fun(a) * FxNS < 0
b = xNS;
else
a = xNS;
end
end
Ts
end
The input arguments are defined by our teacher, so I can't mess with them. We're supposed to set those variables in the command window before running the function. That way, we can use the program later on for other things. (Even though I think fzero() can be used to do this)
My problem is that I'm not sure how to set fun to something, and then use that in a way that I can do fun(a) or fun(b). In our book they do something they call defining f(x) as an anonymous function. They do this for an example problem:
F = # (x) 8-4.5*(x-sin(x))
But when I try doing that, I get the error, Error: Unexpected MATLAB operator.
If you guys want to try running the program to test your solutions before posting (hopefully my program works!) you can use these variables from an example in the book:
fun = 8 - 4.5*(x - sin(x))
a = 2
b = 3
TolMax = .001
The answer the get in the book for using those is 2.430664.
I'm sure the answer to this is incredibly easy and straightforward, but for some reason, I can't find a way to do it! Thank you for your help.
To get you going, it looks like your example is missing some syntax. Instead of either of these (from your question):
fun = 8 - 4.5*(x - sin(x)) % Missing function handle declaration symbol "#"
F = # (x) 8-4.5*(x-sin9(x)) %Unless you have defined it, there is no function "sin9"
Use
fun = #(x) 8 - 4.5*(x - sin(x))
Then you would call your function like this:
fun = #(x) 8 - 4.5*(x - sin(x));
a = 2;
b = 3;
TolMax = .001;
root = BisectionRoot( fun,a,b,TolMax );
To debug (which you will need to do), use the debugger.
The command dbstop if error stops execution and opens the file at the point of the problem, letting you examine the variable values and function stack.
Clicking on the "-" marks in the editor creates a break point, forcing the function to pause execution at that point, again so that you can examine the contents. Note that you can step through the code line by line using the debug buttons at the top of the editor.
dbquit quits debug mode
dbclear all clears all break points

Flex Number.toFixed not giving expected result

I have a function that gets a numeric value (as Object) and returns a well formatted representation of that number. Because we can get very small numbers, in the process we use the Number object of flex. this is part of the code:
var numericValue:Number = Number(value.toString());
var fixed:String = numericValue.toFixed(precision);
This is the problem: there are situations that the numeric value is in the form of
5.684341886080802e-14
because we want to represent these numbers as 0 we use the above code. In this specific case, where precision is 0 we get an odd result
Initial Values:
value = 5.684341886080802e-14
percision = 0
Operation on values:
var numericValue:Number = Number(value.toString());
var fixed:String = numericValue.toFixed(precision);
Result:
fix = "1."
Why is this?
(BTW - on other numbers in the representataion of X.XXXXXXe-YY with percision bigger than 0 we get the correct result of 0)
This is a bug in Flash Player (FP-5141). It has been around for quite a while. The bug report says it is fixed, but it is not as of Flash Player 11.5.

Passing variables into a function in Lua

I'm new to Lua, so (naturally) I got stuck at the first thing I tried to program. I'm working with an example script provided with the Corona Developer package. Here's a simplified version of the function (irrelevant material removed) I'm trying to call:
function new( imageSet, slideBackground, top, bottom )
function g:jumpToImage(num)
print(num)
local i = 0
print("jumpToImage")
print("#images", #images)
for i = 1, #images do
if i < num then
images[i].x = -screenW*.5;
elseif i > num then
images[i].x = screenW*1.5 + pad
else
images[i].x = screenW*.5 - pad
end
end
imgNum = num
initImage(imgNum)
end
end
If I try to call that function like this:
local test = slideView.new( myImages )
test.jumpToImage(2)
I get this error:
attempt to compare number with nil
at line 225. It would seem that "num" is not getting passed into the function. Why is this?
Where are you declaring g? You're adding a method to g, which doesn't exist (as a local). Then you're never returning g either. But most likely those were just copying errors or something. The real error is probably the notation that you're using to call test:jumpToImage.
You declare g:jumpToImage(num). That colon there means that the first argument should be treated as self. So really, your function is g.jumpToImage(self, num)
Later, you call it as test.jumpToImage(2). That makes the actual arguments of self be 2 and num be nil. What you want to do is test:jumpToImage(2). The colon there makes the expression expand to test.jumpToImage(test, 2)
Take a look at this page for an explanation of Lua's : syntax.