Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count Characters

Status
Not open for further replies.

lynchdog

MIS
Jan 30, 2002
16
US
I have a string "YYNNYYYXXNYN", from which I need to be able to count the number of X(s), Y(s), and N(s). I am unable to locate a command or think of a why to programatcally do this...any help would be appreciated.

Josh
 
If you are using CR v8 and above.

I'm assuming you want three different fields - one for each letter. The following is for "N".

stringvar textn:={yourfield};
numbervar N_count;
numbervar loopn;

for loopn:= 1 to len(textn) do(
if textn[loopn]="N" then N_count:=N_count + 1);
N_count


Change the variable names for each formula field. E.g change textn to textx for the "X" formula.
Mike

Before: After
[morning] [bugeyed]
 
Create a formula such as to count all of them (or separate formulas if you're going to just display the values):

whileprintingrecords;
global numbervar Ns:=0;
global numbervar Xs:=0;
global numbervar Ys:=0;
global numbervar SomethingElse:=0;
numbervar X; // used as the counter

for x := 1 to length({AtsAddrs.chvAddr1}) do(
if mid({AtsAddrs.chvAddr1},X) = "N" then
Ns:=Ns+1
else
if mid({AtsAddrs.chvAddr1},X) = "X" then
Xs:=Xs+1
else
if mid({AtsAddrs.chvAddr1},X) = "Y" then
Ys:=Ys+1
else
SomethingElse := SomethingElse+1
);

Now you can us the results in other formulas, such as:

whileprintingrecords;
global numbervar Ns;
If Ns > 0 then
"I found " + totext(Ns,0) + " of the value N"
else
"There are no N's"

-k kai@informeddatadecisions.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top