Recent

Author Topic: ExtractWordPos ?  (Read 828 times)

JLWest

  • Hero Member
  • *****
  • Posts: 612
ExtractWordPos ?
« on: March 20, 2019, 01:50:20 am »

function ExtractWordPos( N: Integer;
                                     const S: string;
                                    const WordDelims: TSysCharSet;
                                   var Pos: Integer) : string;

I think I would like to use this function but not sure how to call it.

Var
 Answer : String;
 aPos      : Integer;
 Item      : String = 'Now is the time for all good men';

begin
Answer := ExtractWordPos(4,Item,['  '] , aPos);

And wouldb't it return 4 because I specifying 4 in  "N: Integer";

It's obvious that I'm not reading this right;
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

lucamar

  • Hero Member
  • *****
  • Posts: 2147
Re: ExtractWordPos ?
« Reply #1 on: March 20, 2019, 02:11:45 am »
The function returns the Nth word as result and the character position in which it was found in Pos

With your example it should return "time" and "aPos = 12". Does it return something else?
« Last Edit: March 20, 2019, 02:15:05 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

BobDog

  • New Member
  • *
  • Posts: 40
Re: ExtractWordPos ?
« Reply #2 on: March 20, 2019, 02:51:13 am »

According to the help file
. . .
function ExtractWordPos(
  N: Integer;
  const S: string;
  const WordDelims: TSysCharSet;
  out Pos: Integer
):string;


N   Which word to extract
S    String to extract a word from.
WordDelims  Characters to use as word delimiters
Pos    On return, contains the position of the N-th word.
. . .
But if I make the deliminator more than one character I get an error.
So, just one space as deliminator.
Code: Pascal  [Select]
  1.    
  2.  
  3. program pos;
  4. uses strutils;
  5.  
  6.  
  7. Var
  8.  Answer : String;
  9.  aPos      : Integer;
  10.  Item      : String = 'Now is the time for all good men';
  11.  
  12. begin
  13. Answer := ExtractWordPos(4,Item,[' '] , aPos);
  14. writeln(answer,'  ',apos);
  15. writeln('Press return to end . . .');
  16.  
  17. readln;
  18. end.

result
time  12
Press return to end . . .
   

 
   


lucamar

  • Hero Member
  • *****
  • Posts: 2147
Re: ExtractWordPos ?
« Reply #3 on: March 20, 2019, 04:10:40 am »
But if I make the deliminator more than one character I get an error.
So, just one space as deliminator.

WordDelims is a TSysCharSet, i.e. a set of AnsiChar; which means you can use, say:
Code: [Select]
FoundWord := ExtractWordPos(N, SrcStr, [' ', ',', '.', ':', ';'], APos);but, of course, not something like:
Code: [Select]
FoundWord := ExtractWordPos(N, SrcStr, [':::', '=='], APos);
« Last Edit: March 20, 2019, 04:12:30 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 612
Re: ExtractWordPos ?
« Reply #4 on: March 20, 2019, 04:38:55 am »
Thank you;

FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB