Recent

Author Topic: <SOLVED> Syntax of IF x in Range  (Read 2989 times)

Bert_Plitt

  • Jr. Member
  • **
  • Posts: 59
<SOLVED> Syntax of IF x in Range
« on: January 10, 2022, 12:38:04 am »
Hello All -- It seems that this is a simple question, but I can't find the answer.  What is the fpc syntax to do the following pseudo code?  I get "Operator is not overloaded" error in the app I'm working on..

type
  TRange = '3'..'8';
var
  num: String;
function CountNums(S: String): Integer;
var
  k: Integer;
begin
  Result:=0;
  for k:=1 to length(S) do
  begin
    num:=S[k];
    if num in TRange then Result:=Result+1;
  end;
end;
« Last Edit: January 12, 2022, 05:25:56 pm by Bert_Plitt »
Windows 10, Lazarus 2.2.0 RC1, FPC 3.2.2

Bert_Plitt

  • Jr. Member
  • **
  • Posts: 59
Re: Syntax of IF x in Range
« Reply #1 on: January 10, 2022, 03:36:56 am »
Jamie -- Thanks for the quick reply, but your code gives me the error I'm getting.  I've attached a test project to demonstrate.  If you un-comment the line
"if num in '3'..'8' then Result:=Result+1;"  you'll see what I mean.  Can you see what I'm doing wrong?
Windows 10, Lazarus 2.2.0 RC1, FPC 3.2.2

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Syntax of IF x in Range
« Reply #2 on: January 10, 2022, 03:58:51 am »
Code: Pascal  [Select][+][-]
  1.  if num in '3'..'8' then

Needs brackets:
Code: Pascal  [Select][+][-]
  1.  if num in ['3'..'8'] then


AND

num should be of type char. Or use num[1] if you insist:
Code: Pascal  [Select][+][-]
  1.  if num[1] in ['3'..'8'] then

Thaddy

  • Hero Member
  • *****
  • Posts: 11517
Re: Syntax of IF x in Range
« Reply #3 on: January 10, 2022, 07:35:34 am »
You can do this:
Code: Pascal  [Select][+][-]
  1. program rangedemo;
  2. {$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
  3. type
  4.   TMyRange = 0..9;
  5. var
  6.    i:integer;  
  7.    r:TMyRange; // you can not always use TMyRange directly here.
  8.  begin
  9.    for i in [r] do; // something
  10.    for i in TMyrange do;// something.  This is also legal
  11.    // and
  12.    if i in [r] then;// do something
  13.    // this is not legal for comparisons, requires a var of TMyrange
  14.    // if i in TMyrange
  15.    // if i in[tmyrange]
  16.  end.
So in this case you can not always use the type directly, but you must declare a var of type for comparisons. Then it works as you expected. As opposed to iterating , where you can use the type directly.
« Last Edit: January 10, 2022, 08:24:17 am by Thaddy »
Путин преступник. Россияне дезинформированы.

Zoran

  • Hero Member
  • *****
  • Posts: 1726
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Syntax of IF x in Range
« Reply #4 on: January 10, 2022, 11:15:15 am »
Hello All -- It seems that this is a simple question, but I can't find the answer.  What is the fpc syntax to do the following pseudo code?  I get "Operator is not overloaded" error in the app I'm working on..

type
  TRange = '3'..'8';
var
  num: String;
function CountNums(S: String): Integer;
var
  k: Integer;
begin
  Result:=0;
  for k:=1 to length(S) do
  begin
    num:=S[k];
    if num in TRange then Result:=Result+1;
  end;
end;

This TRange you declared is not range of strings, but range of chars.
By the way, range of strings can not be used in pascal. Ranges can be defined with ordinal types only, and string is not an ordinal type.

So, it should be enough to change declaration of num from String to Char.
Or better, eliminate this auxiliary variable and use
Code: Pascal  [Select][+][-]
  1. if S[k] in TRange then Result := Result + 1;

The fact that char can be assigned to string does not help here. Here, it would obviously be better if compiler refused the line "num := S[k]". This only creates confusion.
S[k] is a char, not a string.

EDIT:
Sorry, no. You cannot use S[k] in TRange directly. You would need a set of TRange...
The simplest I can think of would be:
Code: Pascal  [Select][+][-]
  1. if (S[k] >= Low(TRange)) and (S[k] <= High(TRange)) then Result := Result + 1;
« Last Edit: January 10, 2022, 11:24:09 am by Zoran »

MarkMLl

  • Hero Member
  • *****
  • Posts: 4183
Re: Syntax of IF x in Range
« Reply #5 on: January 10, 2022, 11:32:48 am »
This TRange you declared is not range of strings, but range of chars.
By the way, range of strings can not be used in pascal. Ranges can be defined with ordinal types only, and string is not an ordinal type.

However, individual strings and ranges of characters /can/ be used in a case statement, and very often this leads to more readable code. Now I fully accept that putting as much stuff as possible into constants at the head of a unit or function is good practice "in case the value of Pi changes", but it can also be counterproductive if a maintainer trying to get an idea of the flow of a program has to constantly flip between declarations and implementation.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018