Recent

Author Topic: [FPC] IF IN statement for Arrays  (Read 8252 times)

Человек_Борща

  • New Member
  • *
  • Posts: 33
    • My little IT world.
[FPC] IF IN statement for Arrays
« on: January 13, 2015, 10:04:50 am »
Greetings!

It will be perfect, If FPC compliller wouldbe support this IF..IN statement.

For example:
Code: [Select]
var
  i:Integer;
  a:array of integer;
begin
  i := 1;
  if (i IN a) then
  //....
end;
Here we checks, if value of i exists in aray.

for objfpc, delphi modes.

Thanks.
« Last Edit: January 13, 2015, 12:42:29 pm by Человек_Борща »
With best regards, Alexandr.
_
OS: Windows 8.1 x64 / Ubuntu 14.02 x64
IDE: CodeTyphon 5.4 (win32-win64/win64) / FPC 3.1.1

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: [FPC] IF IN statement
« Reply #1 on: January 13, 2015, 10:08:37 am »
It does!

Человек_Борща

  • New Member
  • *
  • Posts: 33
    • My little IT world.
Re: [FPC] IF IN statement
« Reply #2 on: January 13, 2015, 11:18:17 am »
 It doesn't. In Lazarus 1.2.6, I've checked:
Code: [Select]
function IsKnownSignature(iVer: integer): boolean;
var
  i: integer;
begin
  Result := False;
    Result := (iVer in CRYPTO_KNOWN_SIGNATURES);   //uCrypto.pp(635,21) Error: Operator is not overloaded
  {for i := low(CRYPTO_KNOWN_SIGNATURES) to high(CRYPTO_KNOWN_SIGNATURES) do
  begin
    if (CRYPTO_KNOWN_SIGNATURES[i] = iVer) then
    begin
      Result := True;
      Break;
    end;
  end; }
end;

FPC 3.3.1 under CodeTyphon 5.2 said the same. Commented code is an analog.
« Last Edit: January 13, 2015, 11:20:18 am by Человек_Борща »
With best regards, Alexandr.
_
OS: Windows 8.1 x64 / Ubuntu 14.02 x64
IDE: CodeTyphon 5.4 (win32-win64/win64) / FPC 3.1.1

Basile B.

  • Guest
Re: [FPC] IF IN statement
« Reply #3 on: January 13, 2015, 11:26:31 am »
What is  CRYPTO_KNOWN_SIGNATURES type ?

Человек_Борща

  • New Member
  • *
  • Posts: 33
    • My little IT world.
Re: [FPC] IF IN statement
« Reply #4 on: January 13, 2015, 11:47:39 am »
Is an global constant:
Code: [Select]
  CRYPTO_MAX_SIGNS = 1;
  CRYPTO_SIGN_VER_1_NUM = 0;

  CRYPTO_KNOWN_SIGNATURES: array[1..CRYPTO_MAX_SIGNS] of integer =
    (CRYPTO_SIGN_VER_1_NUM); 
   
« Last Edit: January 13, 2015, 11:49:18 am by Человек_Борща »
With best regards, Alexandr.
_
OS: Windows 8.1 x64 / Ubuntu 14.02 x64
IDE: CodeTyphon 5.4 (win32-win64/win64) / FPC 3.1.1

Bart

  • Hero Member
  • *****
  • Posts: 3538
    • Bart en Mariska's Webstek
Re: [FPC] IF IN statement
« Reply #5 on: January 13, 2015, 11:58:24 am »
Your first example is a set, not an array.

Bart

Leledumbo

  • Hero Member
  • *****
  • Posts: 8111
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: [FPC] IF IN statement for Arrays
« Reply #6 on: January 13, 2015, 12:49:26 pm »
Just implement it yourself:
Code: [Select]
operator in(i: integer; a: array of integer) Result: Boolean;
var
  b: Integer;
begin
  Result := false;
  for b in a do begin
    Result := i = b;
    if Result then Break;
  end;
end;

begin
  WriteLn(1 in [1..3]);
  WriteLn(0 in [1..3]);
end.
NOTE: above is a damn naive implementation using linear search, feel free to improve it yourself.