Recent

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

ruk1n

  • New Member
  • *
  • Posts: 16
[SOLVED]looking for a string in an array of string
« on: June 28, 2017, 06:31:39 pm »
Hello,
I was used to writing programs in Python. Now I find it very hard to write programs in free pascal.
For instance, how would I write in free pascal the following example:
Code: Python  [Select][+][-]
  1. TT=['one', 'quatro', 'zwei']
  2. t='trois'
  3. if t in TT:
  4.         print "OK"
  5. else:
  6.         print "nope."
I'm afraid the code if t in TT: has no simple equivalent in free pascal.
« Last Edit: June 29, 2017, 08:58:12 am by ruk1n »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: looking for a string in an array of string
« Reply #1 on: June 28, 2017, 06:36:25 pm »
Yes. That is not in language in Pascal, and done by libraries.

The simplest container types (like TStringlist) are basically array wrappers for this purpose, study the examples in the documentatiion

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: looking for a string in an array of string
« Reply #2 on: June 28, 2017, 06:43:35 pm »

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: looking for a string in an array of string
« Reply #3 on: June 28, 2017, 06:51:57 pm »

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: looking for a string in an array of string
« Reply #4 on: June 28, 2017, 07:09:47 pm »
Basically, that Python code can be easily translated by implementing a simple operator.
Here's an example, but notice this will only work in trunk.
You see the similarities?
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$mode objfpc}
  3. // library
  4. operator in(const a:string;b:Array Of String):Boolean;inline;
  5. var i:integer;
  6. begin
  7.   Result := False;  
  8.   for i :=Low(b) to High(b) do
  9.     if a = b[i] then
  10.     begin
  11.       Result := True;
  12.       Break;
  13.     end;    
  14. end;
  15.  
  16. // main code
  17. var
  18.  TT:Array Of String;
  19.  T:String = 'trois';
  20. begin
  21.   TT:=['one', 'quatro', 'zwei'];
  22.   if T in TT then
  23.      writeln('Ok')
  24.   else
  25.     writeln('Nope');       
  26. end.

A slightly more conservative approach, along the lines that Marco indicates and also works in 3.0 or 2.6.4 looks like this:
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$mode objfpc}
  3. uses classes;
  4. // library code
  5. operator in(const a:string;const b:TStrings):Boolean;
  6. var i:integer;
  7. begin
  8.   Result := False;  
  9.   for i :=0 to Pred(b.count) do
  10.     if a = b[i] then
  11.     begin
  12.       Result := True;
  13.       Break;
  14.     end;    
  15. end;
  16.  
  17. // main
  18. var
  19.  T:String = 'zwei';
  20.  L:TStringlist;
  21. begin
  22.   L:=TStringlist.create;
  23.   try
  24.     L.AddStrings(['one','quatro','zwei']);
  25.     if T in L then
  26.      writeln('Ok')
  27.     else
  28.      writeln('Nope');      
  29.   finally
  30.     L.Free;
  31.   end;
  32. end.
« Last Edit: June 28, 2017, 07:37:53 pm by Thaddy »
Specialize a type, not a var.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: looking for a string in an array of string
« Reply #5 on: June 28, 2017, 07:41:57 pm »
 :D

Code: Pascal  [Select][+][-]
  1. PROGRAM StringsDings;
  2. {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4.  CONST
  5.   Str = 'one quatro zwei';
  6.  
  7.  VAR
  8.   T: String;
  9.  
  10. BEGIN
  11.  T:= 'one';
  12.  
  13.  If Pos(T, Str) <> 0
  14.  Then WriteLn('OK')
  15.  Else WriteLn('NOPE');
  16.  
  17.  ReadLn;
  18. END.
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: looking for a string in an array of string
« Reply #6 on: June 28, 2017, 07:55:48 pm »
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. program Project1;
  3.  
  4. uses StrUtils;
  5.  
  6. begin
  7.   if AnsiMatchStr('trois', ['one', 'quatro', 'zwei']) then
  8.     Writeln('OK')
  9.   else
  10.     Writeln('nope.');
  11. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: looking for a string in an array of string
« Reply #7 on: June 28, 2017, 08:09:05 pm »
Does not satisfy the question: array of string  >:(
ASerge's answer is much better, but the syntax is a bit alien for someone who comes from Python.  And it uses a function instead of an operator....Which he asked: in!
So that also does not satisfy the question  >:(
I tried to come close in syntax, since FPC supports it. In my view both my examples are candidates for inclusion in e.g. strutils. O:-)
« Last Edit: June 28, 2017, 08:35:28 pm by Thaddy »
Specialize a type, not a var.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: looking for a string in an array of string
« Reply #8 on: June 28, 2017, 08:11:55 pm »
Quote
Does not satisfy the question: array of string  >:(
True  :)

Now it's a little bit more like that...  :D
Code: Pascal  [Select][+][-]
  1. PROGRAM StringsDings;
  2. {$MODE OBJFPC}{$H+}{$J-}
  3.  
  4.  USES Classes;
  5.  
  6.  VAR
  7.   str: String;
  8.   sl : TStringlist;
  9.  
  10. BEGIN
  11.  str:= 'trois';
  12.  
  13.  sl:= TStringlist.Create;
  14.   Try
  15.    sl.Text:= 'one'   +sLineBreak+
  16.              'quatro'+sLineBreak+
  17.              'zwei';
  18.  
  19.    If Pos(str, sl.Text) <> 0
  20.    Then WriteLn('OK')
  21.    Else WriteLn('NOPE');
  22.   Finally
  23.    sl.Free;
  24.   End;
  25.  
  26.  ReadLn;
  27. END.
« Last Edit: June 28, 2017, 08:49:44 pm by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: looking for a string in an array of string
« Reply #9 on: June 28, 2017, 08:14:57 pm »
True  :)
Oh well, I am awaiting the first comments that it is all syntactic sugar  :D O:-) Hey, Marco?

Note ASerge's code is not fully equivalent to the Pyton code because the strings are const and the Python code explicitly declares  as variable.
Otherwise you would write in Python : if "trois" in
« Last Edit: June 28, 2017, 08:40:28 pm by Thaddy »
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: looking for a string in an array of string
« Reply #10 on: June 28, 2017, 08:30:15 pm »
I'm afraid the code if t in TT: has no simple equivalent in free pascal.
Yes, it has, but it involves a bit of library code to be written. If you have seen my examples, you will find it is pretty easy to do.
So yes, can be done in FPC, but that particular library code is at the moment missing in the rtl or any of the standard packages.
OTOH, I just wrote it for you, so you can re-use it if needed by putting it in a separate library unit.
« Last Edit: June 28, 2017, 08:32:34 pm by Thaddy »
Specialize a type, not a var.

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: looking for a string in an array of string
« Reply #11 on: June 28, 2017, 09:06:57 pm »
Now it's a little bit more like that...  :D
Like this?
Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2. uses sysutils;
  3. var
  4.  T:String = 'onequatrozwei';
  5. begin
  6.   if T.Contains('trois') then
  7.     writeln('Ok')
  8.   else writeln('Nope');
  9. end.

Or in your example:
Code: Pascal  [Select][+][-]
  1. If sl.Text.Contains(str) then
Nah, no array, no in operator.... %) :-[ :'(

Let's stick to the question. I hope you agree that I answered it with a best fit.
Both you and Aserge (and me) showed that there are other options that are more traditional Pascal and I am also a bit teasing you and Aserge and Marco with some of the latest syntactical possibilities of modern Object Pascal.. :D.
What he asked is simply possible. And even looks the same  O:-)
« Last Edit: June 28, 2017, 09:21:00 pm by Thaddy »
Specialize a type, not a var.

RAW

  • Hero Member
  • *****
  • Posts: 868
Re: looking for a string in an array of string
« Reply #12 on: June 28, 2017, 09:17:47 pm »

Or in your example:
Code: Pascal  [Select][+][-]
  1. If sl.Text.Contains(str) then
Nah, no array, no in operator.... %) :-[ :'(

Yeah.. nice, just for the sake of variety ...   :D

// All good things come in threes. [The third guy took the cake !!!] [Solved]  :D
« Last Edit: June 28, 2017, 09:35:51 pm by RAW »
Windows 7 Pro (x64 Sp1) & Windows XP Pro (x86 Sp3).

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: looking for a string in an array of string
« Reply #13 on: June 28, 2017, 09:32:13 pm »
Like this?
Code: Pascal  [Select][+][-]
  1. var
  2.  T:String = 'onequatrozwei';
  3. begin
  4.   if T.Contains('trois') then
  5.  
Then T.Contains('oz') will also be true and I don't think that's right.

Also... doesn't "in" in python check for whole strings. Both Pos and Contains won't do unless you use separating characters like:
Code: Pascal  [Select][+][-]
  1. var
  2.  T:String = '.one.quatro.zwei.';
  3. begin
  4.   if T.Contains('.trois.') then
  5.  

Thaddy

  • Hero Member
  • *****
  • Posts: 14197
  • Probably until I exterminate Putin.
Re: looking for a string in an array of string
« Reply #14 on: June 28, 2017, 09:38:50 pm »
@rvk
Hence my first example that satisfies all elements of the question. The rest was just toying around.
Back to the original question: array of string + in operator...
The overloaded operator can be part of a library, just like in Python.
Code: Pascal  [Select][+][-]
  1. var
  2.  TT:Array Of String;
  3.  T:String = 'trois';
  4. begin
  5.   TT:=['one', 'quatro', 'zwei'];
  6.   if T in TT then
  7.      writeln('Ok')
  8.   else
  9.     writeln('Nope');      
  10. end.
Now that looks very much like the Python syntax and is legal FPC Pascal code (in trunk).
Observe that with the same overloaded operator it can also be written like this for fixed length arrays (from fpc 2.6.4, probably earlier):
Code: Pascal  [Select][+][-]
  1. var
  2.  TT:Array[0..2] Of String = ('one', 'quatro', 'zwei');
  3.  T:String = 'trois';
  4. begin
  5.   if T in TT then
  6.      writeln('Ok')
  7.   else
  8.     writeln('Nope');      
  9. end.
« Last Edit: June 28, 2017, 10:11:19 pm by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018