Results 1 to 8 of 8

Thread: Dealing with NaNs

  1. #1
    Join Date
    Oct 2008
    Posts
    196
    Mentioned
    1 Post(s)
    Quoted
    20 Post(s)

    Default Dealing with NaNs

    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:

    Simba Code:
    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:
    Simba Code:
    Error: Exception: "" is an invalid integer at line blah blah blah

  2. #2
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    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
    Working on: Tithe Farmer

  3. #3
    Join Date
    Sep 2006
    Posts
    6,089
    Mentioned
    77 Post(s)
    Quoted
    43 Post(s)

    Default

    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.
    Hup Holland Hup!

  4. #4
    Join Date
    Oct 2008
    Posts
    196
    Mentioned
    1 Post(s)
    Quoted
    20 Post(s)

    Default

    Quote Originally Posted by masterBB View Post
    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

    Quote Originally Posted by nielsie95 View Post
    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!
    Last edited by HT BaaFly; 03-05-2012 at 07:34 PM.

  5. #5
    Join Date
    Oct 2011
    Posts
    805
    Mentioned
    21 Post(s)
    Quoted
    152 Post(s)

    Default

    I think problem is here
    Simba Code:
    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:

    Simba Code:
    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;
    Last edited by bg5; 03-06-2012 at 11:25 AM.

  6. #6
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Simba Code:
    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;
    I am Ggzz..
    Hackintosher

  7. #7
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

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

    Simba Code:
    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;
    Working on: Tithe Farmer

  8. #8
    Join Date
    Nov 2011
    Posts
    1,268
    Mentioned
    17 Post(s)
    Quoted
    217 Post(s)

    Default

    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));
    Last edited by DemiseScythe; 03-06-2012 at 01:00 PM.
    GLH Tutorial ~ OpenGL Scripting
    http://villavu.com/forum/showthread.php?p=1292150

    GLH Scripts ~ Abyssal Scripts
    http://villavu.com/forum/showthread.php?p=1293187
    Current Projects:
    A) DemiseSlayer Pro (Released/100%).
    B) Demise Power Miner(Released/100%).
    C) Demise Pyramid Plunder(Planning Stage/0%).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •