Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Count number of occurrences of a string Post Reply Post New Topic
Author Message
saoco77
Senior Member
Senior Member


Joined: 26 Jun 2007
Online Status: Offline
Posts: 104
Quote saoco77 Replybullet Topic: Count number of occurrences of a string
    Posted: 02 Sep 2009 at 6:27am
I'm trying to count the number of times the word "Title" occurs within a memo field (500-2000 chars).

Is this possible to do within Crystal??

Thanks

Edited by saoco77 - 02 Sep 2009 at 6:29am
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet Posted: 02 Sep 2009 at 11:01am
you would use a loop:
local numbervar iIndex := instr({table.field}, "Title");
local numbervar iCount:=0;
 
 
while iIndex <> 0 do
(
 if iIndex <> 0 then iCount := iCount + 1;
 iIndex = instr(iIndex + 1, {table.field}, "Title")
)
 
iCount
 
IP IP Logged
saoco77
Senior Member
Senior Member


Joined: 26 Jun 2007
Online Status: Offline
Posts: 104
Quote saoco77 Replybullet Posted: 02 Sep 2009 at 1:28pm
I'm receiving the error message

A loop was evaluated more than the maximum number of times allowed.

If the word Title is not contained within the field the formula works fine.

IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet Posted: 02 Sep 2009 at 4:57pm
oops,
should have typed:
iIndex := instr(iIndex + 1, {table.field}, "Title")
 
need to assign the variable, not compare.
 
there should be a ; after the the last ) as well.


Edited by lockwelle - 02 Sep 2009 at 4:59pm
IP IP Logged
saoco77
Senior Member
Senior Member


Joined: 26 Jun 2007
Online Status: Offline
Posts: 104
Quote saoco77 Replybullet Posted: 03 Sep 2009 at 7:57am
I still couldn't get your formula to work. I think there may be an issue with the stop mechanism on the loop.

Listed below is the solution I finally came up with which breaks the process into two formulas.

@formula1

stringvar array SplitText := split({table.field}, " ");
stringvar array FilterText := filter(SplitText, "Letter");
stringvar iJoin;

iJoin := Join(FilterText,"");
iJoin


@formula2

evaluateafter({@formula1});

if isnull({@formula1}) then 0 else
(length({@formula1})/6)

Thank you again for your assistance, it is appreciated.

Edited by saoco77 - 03 Sep 2009 at 7:57am
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4372
Quote lockwelle Replybullet Posted: 04 Sep 2009 at 6:19am

looks like a nice solution, but I thought you were looking for "Title"?

I have never seen the filter function before, but it looks like it would do the trick.
IP IP Logged
Aliqux
Newbie
Newbie
Avatar

Joined: 28 Aug 2009
Online Status: Offline
Posts: 3
Quote Aliqux Replybullet Posted: 26 May 2016 at 5:12am
I know this is an older post but I thought this might help for other people looking for this. The code above helped me come up with this solution.

stringvar array textcheck := split({table.field}, "TEXT");
count(textcheck)-1;

Change the TEXT to what your looking for and the table will be split by that text. Each item in the array will then have the text your looking for. Subtract by one because the first split will be the first section without the word.
I ended up coming to this because there were some fields that the users were using commas instead of spaces to sperate the items

Edited by Aliqux - 26 May 2016 at 5:14am
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.016 seconds.