Recent

Author Topic: [SOLVED]looking for a string in an array of string  (Read 22030 times)

ruk1n

  • New Member
  • *
  • Posts: 16
Re: looking for a string in an array of string
« Reply #15 on: June 29, 2017, 08:55:56 am »
Wow! What a forum! I didn't expect so many useful and pertinent answers !

Now I have some work to do to select the solution that fit my needs.  :)

A lot of thanks to all of you.
--
ruk1n

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: [SOLVED]looking for a string in an array of string
« Reply #16 on: June 29, 2017, 11:43:36 am »
I have submitted a patch for the strutils library that introduces the in operator for string in array of string.
https://bugs.freepascal.org/view.php?id=32076

If it will be applied, the syntax will be available in trunk.
It is not an invasive patch so I hope it will be applied.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: [SOLVED]looking for a string in an array of string
« Reply #17 on: June 29, 2017, 12:21:05 pm »
+1, elegant solution.

I did check if the in operator in python is case sensitive and it is, so it will be compatible likewise.

ASerge

  • Hero Member
  • *****
  • Posts: 2240
Re: looking for a string in an array of string
« Reply #18 on: July 01, 2017, 09:16:11 pm »
...I hope you agree that I answered it with a best fit.
Not agree.
The original question: code..has simple equivalent in free pascal.
Your answer gives equivalent, which visual similar to PHP, but not simple. My code gives a simple equivalent. In addition, your code adds a superfluous abstraction, which can further degrade the code, especially for native pascal developers. KISS :D

Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: [SOLVED]looking for a string in an array of string
« Reply #19 on: July 01, 2017, 09:36:21 pm »
@ASerge
Native Pascal developers developed the possibility to elegantly solve the problem in a manner familiar to OP more than a couple of years ago..
Therefor I have also provided a patch.
Object Pascal is a living language. But be an Ostrich and stick your head in the sand if you do not agree, I don't mind.

I already showed your solution was wrong lacking (but working) in multiple ways.
« Last Edit: July 01, 2017, 09:40:24 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

ASerge

  • Hero Member
  • *****
  • Posts: 2240
Re: [SOLVED]looking for a string in an array of string
« Reply #20 on: July 01, 2017, 11:59:40 pm »
I already showed your solution was wrong lacking (but working) in multiple ways.
Thaddy go down to earth and do not hide behind beautiful phrases. You just like the wrap, which is essentially superfluous. Compare your code:
Code: Pascal  [Select][+][-]
  1. operator in(const a:string;b:Array Of String):Boolean;inline;
  2. var i:integer;
  3. begin
  4.   Result := False;
  5.   for i :=Low(b) to High(b) do
  6.     if a = b[i] then
  7.     begin
  8.       Result := True;
  9.       Break;
  10.     end;
  11. end;
  12.  
  13. var
  14.   TT:Array Of String;
  15.   T: String = 'trois';
  16. begin
  17.   //  TT := ['one', 'quatro', 'zwei']; ERROR not compiled code!
  18.   SetLength(TT, 3);
  19.   TT[0] := 'one';
  20.   TT[1] := 'quatro';
  21.   TT[2] := 'zwei';
  22.   if T in TT then
  23.     writeln('Ok')
  24.   else
  25.     writeln('Nope');
  26. end.
and my
Code: Pascal  [Select][+][-]
  1. uses StrUtils;
  2.  
  3. var
  4.   T: string = 'trois';
  5. begin
  6.   if AnsiMatchStr(T, ['one', 'quatro', 'zwei']) then
  7.     Writeln('OK')
  8.   else
  9.     Writeln('nope.');
  10. end.
And show me an example where "I already showed your solution was wrong".
Incidentally, the inlining does not work with open arrays and to "b: array of string" it is better to add a const.

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: [SOLVED]looking for a string in an array of string
« Reply #21 on: July 02, 2017, 12:24:02 am »
The suggested bugtracker entry is much more elegant.
And when excepted the operation part will be in strutils internally so not needed in program.

This is much closer to what OP asked.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses StrUtils;
  3.  
  4. // this is the suggested change
  5. Operator in (const AText:string;const AValues: array of string):Boolean;
  6. begin
  7.   Result := AnsiIndexStr(AText,AValues) <> -1;
  8. end;
  9.  
  10. var
  11.   TT: array[0..2] of string = ('one', 'quatro', 'zwei');
  12.   T: string = 'trois';
  13. begin
  14.   if T in TT then
  15.     writeln('Ok')
  16.   else
  17.     writeln('Nope');
  18. end.

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: [SOLVED]looking for a string in an array of string
« Reply #22 on: December 10, 2020, 02:19:02 pm »
Sorry to open this old thread, just looking for the same solution I'm using trunk and now using the strutils in operator.

But why this is not widely used across FPC? Or is it? There is a reason there is not a language default without including units? As well why is not included as well for array of integer? Or is it in some other unit?

Thanks.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11446
  • FPC developer.
Re: [SOLVED]looking for a string in an array of string
« Reply #23 on: December 10, 2020, 02:21:47 pm »
Sorry to open this old thread, just looking for the same solution I'm using trunk and now using the strutils in operator.

But why this is not widely used across FPC? Or is it? There is a reason there is not a language default without including units? As well why is not included as well for array of integer? Or is it in some other unit?

It's limited. Just e.g. case sensitive vs insensitivity

jamie

  • Hero Member
  • *****
  • Posts: 6129
Re: [SOLVED]looking for a string in an array of string
« Reply #24 on: December 10, 2020, 02:35:58 pm »
Sorry to open this old thread, just looking for the same solution I'm using trunk and now using the strutils in operator.

But why this is not widely used across FPC? Or is it? There is a reason there is not a language default without including units? As well why is not included as well for array of integer? Or is it in some other unit?

Thanks.

Its called LARD..

 Its like the LARD I have around my waist, I don't need it and I don't want it but I still need to carry it with me and it slows me down, forces me to eat more to provide the energy to carry it.

It guess that was simply enough..  :)

I suppose and it could already exist, a feature in the IDE to automatically include your added units to each New unit started.


The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018