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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Number of values in a String 1

Status
Not open for further replies.

McMint

IS-IT--Management
Apr 21, 2004
7
GB
Hi Guys,

I am using Crystal 8.5 and have a set of strings in data fields which contain data like

"212,212,212,212,212,120"
"216,216,216,216,216,216"

What I wish to do is count how many of each value is in the string ie 5 212's and 1 120 or 6 216's etc.

There can be up to 20 different values in the string although there are likely to only be 1 to 4 different values but I cannot guarantee this.

Any suggestions as to how I should do this?

Many thanks in advance.
 
I thought of lots of fun ways to do this, but this is the simplest I could come up with:

whileprintingrecords;
stringvar array MyArray:= split({table.field},",");
numbervar Counter1;
Stringvar array DistArray;
Stringvar CurrVal;
For Counter1 := 1 to ubound(MyArray) do(
DistArray:=filter(MyArray,MyArray[Counter1]);
if instr(Currval, "Val: "+MyArray[Counter1]) =0 then
Currval:=Currval+"Val: "+MyArray[counter1]+" = "+totext(ubound(filter(MyArray,MyArray[Counter1])))+chr(13);
);
Currval

-k
 
I just realized that I tested this in CR 9, if it doesn't work in CR 8.5, you'll need to create another loop to gen the distinct array as done using the filter function.

-k
 
Thanks very much. Filter works fine in 8.5. Your code is great. It took me a little while to work out just what you were doing. You had 3 functions in there I had never heard of! :-D

Your assistance is much appreciated.


 
Glad that it worked for you.

Most of the code is relatively common, the filter is unusual for many, and the instr() check was used to make sure that the value wasn't previously used by fully qualifying it using the "val =", rather than just searching for the value, which in theory could also be the quantity.

If you run into output limitations (CR 8.5 has 254 char max) create ample <254 formulas to hold the output and drop them all in a text box.

-k
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top