Recent

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

bdexterholland

  • Jr. Member
  • **
  • Posts: 65
  • uh?
Re: Set of strings (ordinal expression expected
« Reply #15 on: February 09, 2017, 04:33:49 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.

Lotta code for a simple problem.

Anyway, thanks for your help
[sleep .....]

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Set of strings (ordinal expression expected
« Reply #16 on: February 09, 2017, 04:38:05 pm »
It is a generic solution that you can re-use. So write once applies ;) Not a lot of code for that... at all...

Thing is: set operators on strings do not exist, but you can overload all set operators to create that feature yourself ;)
Put them in a unit, and hey presto! we have the behavior that you expected.
« Last Edit: February 09, 2017, 04:44:45 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

bdexterholland

  • Jr. Member
  • **
  • Posts: 65
  • uh?
Re: Set of strings (ordinal expression expected
« Reply #17 on: February 18, 2017, 02:27:43 pm »
well yeah, that's a good hint!

so, why i doesn't came onboard in fpc lib?
[sleep .....]

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Set of strings (ordinal expression expected
« Reply #18 on: February 18, 2017, 02:38:57 pm »
Because I didn't submit it?
It's trivial. And boring to code all of them. Once I need all of it, trust me, I will submit it  O:-)

There's also the small matter of interpretation... But, oh, well...

For example: do you want it to work like a set or like a tuple?
« Last Edit: February 18, 2017, 03:01:03 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018