SRL Archive - OSR Help

Dealing with NaNs


HT BaaFly

2012-03-05 13:10:44

Hello, everyone
I've had this issue for a while and I can't seem to fix it. My script reads integers off the screen and later processes them but sometimes, it fails to read the screen properly and instead returns nothing, or "" as simba likes to put it. Is there a way to check if the variable = nothing and redoing the test until the variable = integer?

I've tried If variable = "" then blah blah blah….. but that didn't work

Here's the function if anyone's interested:


function GetNumerals(const s: string): string; //Credit to Echo_
var
i, j: Integer;
numArray: array of string;
begin
numArray := ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
for i := 1 to High(s) do
for j := 0 to High(numArray) do
begin
if not (s[i] = numArray[j]) then Continue;
Result := Result + s[i];
end;
end;

function GetMidPrice: integer;
begin
writeln('GetMidPrice is: ' + GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpCharsEx'));
result := StrToInt(GetNumerals(GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpCharsEx')));
end;

Error:
Error: Exception: "" is an invalid integer at line blah blah blah

masterBB

2012-03-05 13:59:07

Couldn't reproduce. Also I don't know which line it's talking about. Tried:
'a string' result = ''
'4 str1ng' result = '41'
'' result = ''
'324' result = 324

nielsie95

2012-03-05 14:12:54

You could catch the exception using a try..exception block or you could use StrToIntDef('string', defaultInteger) which will return the default if the string is not a valid number.

HT BaaFly

2012-03-05 14:29:22

From: masterBB
Couldn't reproduce. Also I don't know which line it's talking about. Tried:
'a string' result = ''
'4 str1ng' result = '41'
'' result = ''
'324' result = 324


This error does not happen frequently and only occurs within the rest of my script in the smart client. It's kinda hard to reproduce because it happens infrequently but i'm not trying to alter the code in anyway, i'm just trying to create an if statement or something similar that will rerun the procedure or function if the value returned is nothing or not a number

From: nielsie95
You could catch the exception using a try..exception block or you could use StrToIntDef('string', defaultInteger) which will return the default if the string is not a valid number.


Thanks for the post but can you elaborate more? I've never used a try..exception block or the function StrToIntDef('string', defaultInteger)

Edit: Just read on try…exception…finally and it seems to make a lot of sense :) just implemented it and going to try it asap!

bg5

2012-03-06 06:21:36

I think problem is here
function GetMidPrice: integer;
begin
writeln('GetMidPrice is: ' + GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpCharsEx'));
result := StrToInt(GetNumerals(GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpCharsEx')));
end;

if GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpCharsEx') returns empty string or string without numbers then you got this exception.

Fix:

function GetMidPrice: integer;
var text :string;
begin
writeln('GetMidPrice is: ' + GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpCharsEx'));
text := GetNumerals(GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpCharsEx'))
if High(text)>0 then
result := StrToInt(text);
end;

Brandon

2012-03-06 06:44:19


function GetMidPrice: Integer;
var
Text: String;
begin
Text:= GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpCharsEx');
writeln(Text); //easier than calling the func again..
Result:= StrToIntDef(GetNumbers(Text), -1);
end;

masterBB

2012-03-06 07:22:28

Maybe I also should place the High(x) in a var to reduce function calls.

function GetNumerals(const s: string): string;  //Credit to Echo_
var
i, j: Integer;
numArray: string; //Could also be Array of char
begin
numArray := '0123456789';
for i := 1 to High(s) do
for j := 1 to High(numArray) do
if (s[i] = numArray[j]) then
Result := Result + s[i];
end;

function GetMidPrice: Integer;
var
Text: String;
begin
Text:= GetTextAtExWrap(296, 186, 440, 199, 0, 10, 1, 6400255, 5, 'UpCharsEx');
writeln('GetMidPrice is: ' + Text); //easier than calling the func again..
Result:= StrToIntDef(GetNumerals(Text), -1);
end;

DemiseScythe

2012-03-06 07:56:35

Nope, the problem is that he is getting something that isnt a number

Anyways, dont use getnumerals, use smart functions like ExtractFromStr

ExtractFromStr('s', Numbers);

Then Int := StrToInt(ExtractFromStr('s', Numbers));