Recent

Author Topic: Need a smart way to control assignment conditions  (Read 1925 times)

whiffee

  • New Member
  • *
  • Posts: 18
Need a smart way to control assignment conditions
« on: February 27, 2018, 07:08:20 pm »
Hi All,

I'm trying to write a block that adapts to entries of
differing lengths:

Code: Pascal  [Select][+][-]
  1. j := 0;
  2. REPEAT
  3.     if GetToken(ptblobs[e], ' ', j) <> '' then counter := counter +1;
  4.     j := j +1;
  5.     until GetToken(ptblobs[e], ' ', j) = '';

But this does not work to ward off trying to assign entries longer than
counter. With counter = 8 for example:
   
Code: Pascal  [Select][+][-]
  1. if counter > 8 then
  2. eb09 := eightblobsf[StrtoInt(GetToken(ptblobs[e], ' ', 10))];      
throws a ' "" is an invalid integer ' error.  Can someone suggest
a strategy to lock out the unwanted assignment attempts?

My Laz is 1.6

Josh

  • Hero Member
  • *****
  • Posts: 1454
Re: Need a smart way to control assignment conditions
« Reply #1 on: February 27, 2018, 07:46:11 pm »
Hi

StrToInt will throw an error if the string is not a number.

You could try strtointdef; which allows it to return a default value if the string is invalid ie

Code: Pascal  [Select][+][-]
  1. if counter > 8 then
  2. begin
  3.    Check_Value:=StrtoIntDef(GetToken(ptblobs[e], ' ', 10),-999); // -999 is used as a rogue value for checking against;
  4.    If Check_Value=-999 then
  5.    begin
  6.        // Token contains invalid data
  7.       // So handle the situation;
  8.    end
  9.    else
  10.    begin
  11.      // value should now be a number
  12.      eb09 := eightblobsf[Check_Value];
  13.    end;
  14.  
« Last Edit: February 27, 2018, 07:47:44 pm by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Thaddy

  • Hero Member
  • *****
  • Posts: 18792
  • Glad to be alive.
Re: Need a smart way to control assignment conditions
« Reply #2 on: February 27, 2018, 08:42:31 pm »
StrToIntDef will give you a default.
Recovered from removal of tumor in tongue following tongue reconstruction with a part from my leg.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12718
  • FPC developer.
Re: Need a smart way to control assignment conditions
« Reply #3 on: February 27, 2018, 08:52:36 pm »
Or use trystrtoint. This avoids "impossible" values like -999 in Josh' example.

whiffee

  • New Member
  • *
  • Posts: 18
Re: Need a smart way to control assignment conditions [SOLVED]
« Reply #4 on: February 27, 2018, 10:22:23 pm »
I should have included the function header probably:
Code: Pascal  [Select][+][-]
  1. function TForm1.FindShortestEdge(e : Integer) : Double;

The test value was e = 0, and ptblobs[0] = ' 0 3 4 5 6 7 8 9 10'

However, it doesn't matter because InttoStrDef is a perfect answer, I can work with that.

Thanks much for the help.

 

TinyPortal © 2005-2018