Recent

Author Topic: Set of strings (ordinal expression expected  (Read 10787 times)

bdexterholland

  • Jr. Member
  • **
  • Posts: 65
  • uh?
Set of strings (ordinal expression expected
« on: January 27, 2017, 03:45:46 pm »
Hi guys!

Too much time far away from programing with pascal, last months i was a bit close from php and magento.

Now, with a microsystem to dev, I forgot how to work with sets, set of strings

i have this code:
Code: Pascal  [Select][+][-]
  1. if not(UpperCase(AValue) in ['AC','AL','AP','AM','BA','CE','DF','ES','GO','MA','MT','MS','MG','PA','PB','PR','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO']) then
(Brazillian states)

Since AValue is a string, fpc raises 'Error: Ordinal expression expected'.

FPC doesn't handle set of strings, only chars and other enumerated types?

Thanks !
« Last Edit: February 09, 2017, 04:30:09 pm by bdexterholland »
[sleep .....]

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Set of strings (ordinal expression expected
« Reply #1 on: January 27, 2017, 04:04:06 pm »
No need to use such large font! It is like shouting.

Since AValue is a string, fpc raises 'Error: Ordinal expression expected'.

FPC doesn't handle set of strings, only chars and other enumerated types?

Sets are for ordinal types only, as the errormessage says.

Bart

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Set of strings (ordinal expression expected
« Reply #2 on: January 27, 2017, 04:22:51 pm »
Best compare performance:
Code: Pascal  [Select][+][-]
  1.   case AValue of
  2.    'AC','AL','AP','AM','BA','CE','DF','ES','GO','MA','MT','MS','MG','PA','PB','PR','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO':
  3.      writeln('É do Brasil-sil-sil-sil!!!');
  4.   else
  5.     writeln('Gol da alemanha!');
  6.   end;
  7.  

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Set of strings (ordinal expression expected
« Reply #3 on: January 27, 2017, 04:36:29 pm »
Code: Pascal  [Select][+][-]
  1.   case AValue of
  2.   else
  3.     writeln('Gol da alemanha!');
  4.   end;
  5.  

ROFL !!!!

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Set of strings (ordinal expression expected
« Reply #4 on: January 27, 2017, 05:38:03 pm »
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode objfpc}{$H+}{$endif}
  2. uses
  3.   classes,sysutils;
  4. const
  5.   mystrings:array [0..26] of string = ('AC','AL','AP','AM','BA','CE',
  6.     'DF','ES','GO','MA','MT','MS','MG','PA','PB','PR','PE','PI','RJ',
  7.     'RN','RS','RO','RR','SC','SP','SE','TO');
  8.  
  9.  operator in (const AVAlue:string;const A:array of string)c:Boolean;
  10.  var L:TStringList;
  11.  begin
  12.    L := TStringList.Create;
  13.    C := False;
  14.    try
  15.      L.AddStrings(A);
  16.      C := L.IndexOf(AValue) <> -1;
  17.    finally
  18.      L.Free;
  19.    end;
  20.  end;
  21.  
  22. var
  23.  AValue:string;
  24. begin
  25.   AVAlue := 'SP';
  26.   if AValue in mystrings then
  27.     writeln('Found')
  28.   else
  29.     writeln('not found');
  30. end.
Can be more efficient, but you get the idea.
« Last Edit: January 27, 2017, 05:55:59 pm by Thaddy »
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Set of strings (ordinal expression expected
« Reply #5 on: January 27, 2017, 07:04:47 pm »
Code: Pascal  [Select][+][-]
  1. if not(UpperCase(AValue) in ['AC','AL','AP','AM','BA','CE','DF','ES','GO','MA','MT','MS','MG','PA','PB','PR','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO']) then
Code: Pascal  [Select][+][-]
  1. if not StrUtils.AnsiMatchText(AValue, ['AC',...]) then

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Set of strings (ordinal expression expected
« Reply #6 on: January 27, 2017, 07:06:19 pm »
Yeah, but that's not in is it?  O:-)

Will make the operator overload shorter, though  :-* :o
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Set of strings (ordinal expression expected
« Reply #7 on: January 27, 2017, 09:38:20 pm »
Yeah, but that's not in is it?  O:-)
I agree. Then this :P
Code: Pascal  [Select][+][-]
  1. operator in (const AValue: string; const A: array of string): Boolean;
  2. begin
  3.   Result := AnsiMatchText(AValue, A);
  4. end;

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Set of strings (ordinal expression expected
« Reply #8 on: January 28, 2017, 12:13:03 am »
It does not use sets or "in", but a more efficient routine would be this:

Code: Pascal  [Select][+][-]
  1. function IsBrazilianState(const aString: string): boolean;
  2. var
  3.   s: string;
  4. begin
  5.   if Length(aString) <> 2 then Exit(False);
  6.   s:=UpperCase(aString);
  7.   case s[1] of
  8.     'A': case s[2] of
  9.            'C','L','M','P': Exit(True);
  10.            else Exit(False);
  11.          end;
  12.     'B': Exit(s[2]='A');
  13.     'C': Exit(s[2]='E');
  14.     'D': Exit(s[2]='F');
  15.     'E': Exit(s[2]='S');
  16.     'G': Exit(s[2]='O');
  17.     'M': case s[2] of
  18.            'A','G','S','T': Exit(True);
  19.            else Exit(False);
  20.          end;
  21.     'P': case s[2] of
  22.            'A','B','E','I','R': Exit(True);
  23.            else Exit(False);
  24.          end;
  25.     'R': case s[2] of
  26.            'J','N','O','R','S': Exit(True);
  27.            else Exit(False);
  28.          end;
  29.     'S': case s[2] of
  30.            'C','E','P': Exit(True);
  31.          else Exit(False);
  32.          end;
  33.     'T': Exit(s[2]='O');
  34.     else Exit(False);
  35.   end;
  36. end;  

creaothceann

  • Full Member
  • ***
  • Posts: 117
Re: Set of strings (ordinal expression expected
« Reply #9 on: January 28, 2017, 10:51:16 am »
UpCase might a slightly faster...

Code: Pascal  [Select][+][-]
  1. function IsBrazilianState(s : string) : boolean;
  2. begin
  3. if (Length(s) <> 2) then exit(False);
  4. case UpCase(s[1]) of
  5.         'A':  exit(UpCase(s[2]) in ['C', 'L', 'M', 'P'     ]);
  6.         'B':  exit(UpCase(s[2]) =   'A'                     );
  7.         'C':  exit(UpCase(s[2]) =   'E'                     );
  8.         'D':  exit(UpCase(s[2]) =   'F'                     );
  9.         'E':  exit(UpCase(s[2]) =   'S'                     );
  10.         'G':  exit(UpCase(s[2]) =   'O'                     );
  11.         'M':  exit(UpCase(s[2]) in ['A', 'G', 'S', 'T'     ]);
  12.         'P':  exit(UpCase(s[2]) in ['A', 'B', 'E', 'I', 'R']);
  13.         'R':  exit(UpCase(s[2]) in ['J', 'N', 'O', 'R', 'S']);
  14.         'S':  exit(UpCase(s[2]) in ['C', 'E', 'P'          ]);
  15.         'T':  exit(UpCase(s[2]) =   'O'                     );
  16.         else  exit(False);
  17. end;
  18. end;
  19.  

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Set of strings (ordinal expression expected
« Reply #10 on: January 28, 2017, 12:02:22 pm »
@creaothceann: you're definitely correct about the advantage of UpCase. In my testing using a "case of" construct is slightly faster than "in [aSet]".
So the following routine marginally outperforms the earlier ones:

Code: Pascal  [Select][+][-]
  1. function IsBrazilianState(const s: string): boolean;
  2. begin
  3.   if (Length(s) <> 2) then Exit(False);
  4.   case UpCase(s[1]) of
  5.     'A':  case UpCase(s[2]) of 'C', 'L', 'M', 'P': Exit(True); else Exit(False) end;
  6.     'B':  Exit(UpCase(s[2]) = 'A');
  7.     'C':  Exit(UpCase(s[2]) = 'E');
  8.     'D':  Exit(UpCase(s[2]) = 'F');
  9.     'E':  Exit(UpCase(s[2]) = 'S');
  10.     'G':  Exit(UpCase(s[2]) = 'O');
  11.     'M':  case UpCase(s[2]) of 'A', 'G', 'S', 'T': Exit(True); else Exit(False); end;
  12.     'P':  case UpCase(s[2]) of 'A', 'B', 'E', 'I', 'R': Exit(True); else Exit(False); end;
  13.     'R':  case UpCase(s[2]) of 'J', 'N', 'O', 'R', 'S': Exit(True); else Exit(False); end;
  14.     'S':  case UpCase(s[2]) of 'C', 'E', 'P': Exit(True); else Exit(False); end;
  15.     'T':  Exit(UpCase(s[2]) = 'O');
  16.     else  Exit(False);
  17.   end;
  18. end;  

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Set of strings (ordinal expression expected
« Reply #11 on: January 28, 2017, 01:17:57 pm »
Best compare performance:
Code: Pascal  [Select][+][-]
  1.   case AValue of
  2.    'AC','AL','AP','AM','BA','CE','DF','ES','GO','MA','MT','MS','MG','PA','PB','PR','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO':
  3.      writeln('É do Brasil-sil-sil-sil!!!');
  4.   else
  5.     writeln('Gol da alemanha!');
  6.   end;
  7.  

Still rubbing it in! A World Cup semi-final has nothing to do with the question.
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

bylaardt

  • Sr. Member
  • ****
  • Posts: 309
Re: Set of strings (ordinal expression expected
« Reply #12 on: January 28, 2017, 02:52:32 pm »
Still rubbing it in! A World Cup semi-final has nothing to do with the question.
Here in Brazil, is a local slang (of course after 2014) when you solve it in a fast way but the result isn't what you want. Have no literal translation, and sound like:
- This was fast!
- what??
- No, no... This was faster!

BTW: This Remind me an old joke when a terrorist says he want kill a thousand americans and only ONE squirrel...

Focus on solution
Code: Pascal  [Select][+][-]
  1. case AValue of
  2.    'AC','AL','AP','AM','BA','CE','DF','ES','GO','MA','MT','MS','MG','PA','PB','PR','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO':
  3.  
no on squirrel.

bdexterholland

  • Jr. Member
  • **
  • Posts: 65
  • uh?
Re: Set of strings (ordinal expression expected
« Reply #13 on: February 09, 2017, 04:28:57 pm »
No need to use such large font! It is like shouting.
Sorry,

as the age came, i think it could be better to read.
« Last Edit: February 09, 2017, 05:05:56 pm by bdexterholland »
[sleep .....]

bdexterholland

  • Jr. Member
  • **
  • Posts: 65
  • uh?
Re: Set of strings (ordinal expression expected
« Reply #14 on: February 09, 2017, 04:32:06 pm »
Best compare performance:
Code: Pascal  [Select][+][-]
  1.   case AValue of
  2.    'AC','AL','AP','AM','BA','CE','DF','ES','GO','MA','MT','MS','MG','PA','PB','PR','PE','PI','RJ','RN','RS','RO','RR','SC','SP','SE','TO':
  3.      writeln('É do Brasil-sil-sil-sil!!!');
  4.   else
  5.     writeln('Gol da alemanha!');
  6.   end;
  7.  
ainda bem que sou movido a gasolina.

Thanks for the hint!!!
[sleep .....]

 

TinyPortal © 2005-2018