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!

CR 11 formula to know if data is whole word 2

Status
Not open for further replies.

taterday

Programmer
Jan 28, 2009
183
US
I know my subject does not make sense. I want to trunicate a field that is 50 characters to 20 chars. I do not want to split a word. I want to check at the 20 chars.
There will always be data.
//this removes the comma if there is one in 19th or 20th char
if mid({cr_x.HDR},18,2) = ", " then
hdr_len :=left({cr_x.HDR},18) &&local variable set

IF Length(trim({cr_x.HDR})) > 20 then && have 20+ chars

I what to know how to check if the next char is a "A-Z" so I can -1 from the 20 until a space is found and I can extract from there and not split a word.

If someone knows how to check if there was a whole word, that would be wonderful.

Also is there a way to do a loop to check each letter as the code -1 until a space occurs?

Thank you in advance.
 
Hi,
To aid in finding a solution yourself ( very satisfying, if you have the time), remember that any character field can be thought of as an Array, so look into the various array functions that CR provides, for instance, to start try something like:
Code:
StringVar Array MyField := {Table50charfield};
NumberVar Cntr := 20;
If UBound(MyField) > 20 then
While MyField[Cntr] <> ' ' 
   Cntr = Cntr - 1
Wend
Left(MyField,Cntr);


[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
What am I doing stupid? Your code works great! I had to remove the WEnd. It was not right. Maybe because I use Crystal syntax.
I still need to remove the comma if if falls in the last chars.

This gives a "string is require here" error at the "If unBound"


Local StringVar hdr_len ;
hdr_len := {cr_x.HDR};
StringVar Array MyField := {cr_x.HDR};
NumberVar Cntr := 20;

if mid({cr_x.HDR},19,2) = ", " then
hdr_len :=left({cr_x.HDR},18)
else
If UBound(MyField) > 20 then
While MyField[Cntr] <> " " do
Cntr := Cntr - 1
hdr_len :=Left({cr_x.HDR},Cntr)
 
Hi,
Try:
Code:
StringVar Array MyField := {cr_x.HDR};
NumberVar Cntr := 20;
If UBound(MyField) > 20 then
While MyField[Cntr] <> " "  do   
Cntr := Cntr - 1;

If MyField[Cntr] = ',' then
Left(MyField),Cntr-1)
else
Left(MyField,Cntr)

Let me konw if the UBound error still appears.





[profile]

To Paraphrase:"The Help you get is proportional to the Help you give.."
 
Hi,
I received an error on this
Left(MyField),Cntr-1) needed an array[]
so I changed it to Left(MyField[cntr],Cntr-1)

I even tried putting my Left(cr_x.hdr,Cntr-1).

It compiled/saved clean either way. When I tried to run it I get a subscript must be 1 and the size of the array. The first code I posted worked separately. The else would not work.

By the way, thank you for your help. Sorry I haven't said this sooner.
 
I bleieve this will do what you want... not sure what you want to do with the words after the 19th character though. You mentioned truncate so I assume you wont need them

stringvar array in := split({yourfield}," ");
stringvar out;
numbervar b;
for b := 1 to ubound(mydata) Do
(
if len(out & " " & in)<=20 then
out := out & " " & in
);
out

_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
my apologies for not addressing the comma

stringvar mydata1 := left({yourfield},18)&replace(mid({yourfield},19,2),",","")&mid({yourfield},21,30);
stringvar array in:= split(mydata1," ");

stringvar out;
numbervar b;
for b := 1 to ubound(mydata) Do
(
if len(out & " " & in)<=20 then
out := out & " " & in
);
out



_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
sorry I was in a hurry

replace
for b := 1 to ubound(mydata) Do

with

for b := 1 to ubound(in) Do

_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
Hi,
I get a variable name is expected here at "in"="
stringvar array in:= split(mydata1," ");

Thank you,
 
Hi, same error. on "in" Please see if my code is typed right.

stringvar mydata1 := left({cr_x.HDR},18)&replace(mid({cr_x.HDR},19,2),",","")&mid({cr_x.HDR},21,30);
stringvar array in := split(mydata1," ");
stringvar out;
numbervar b;
for b := 1 to ubound(in) Do
(if len(out & " " & in)<=20 then
out := out & " " & in);
out

 
my fault .. in is a reserved word.... I had replaced that with what I used to make it read better when I posted it .. HEre is my EXACT formula that works. Replace {@wholeword1} with your field

stringvar mydata1 := left({@wholeword1},18)&replace(mid({@wholeword1},19,2),",","")&mid({@wholeword1},21,30);
stringvar array mydata := split(mydata1," ");

stringvar out;
numbervar b;
for b := 1 to ubound(mydata) Do
(
if len(out & " " & mydata)<=20 then
out := out & " " & mydata
);
out



_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
Hi,

I don't seem to be getting the next data in when printing my report. The first answer stays. Should I have while printing ??

??
 
I dont understand? Is the field you are using this a database field? Are you placing the formula in the details section?

_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
Yes. It is a database field. Yes, it is in the details section.

Let me work on this. I don't know enough on the replace, arrays to get help. Give me a day and check back on this, please.

Thank you,

 
Ok .. let me know how it works out as I am very interested. One suggestion to make sure your are looking at things correctly. Place both the database field and that formula field next to each other in the detail section and run your report. There was nothing global about the formula I sent you so I cant see why it would not work for each record.

By the way... the replace you mentioned above was nothing more than looking at character positions 19 and 20 and if there was a comma replacing it with a "".

If you want an explanation on the array piece let me know.

Good Luck!

_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
Hi,

I got the code working. I encountered a problem with the statement.

(if len(out & " " & mydata)<=20 then out := out & " " & mydata); It was skipping over a word to get to a word that matched the IF and wasn't storing the value to "out". I had to put the store to "out" before the evaluation.

I removed all commas in the data.
I learned alot about arrays and replace and for loops.

I couldn't have done it without you. Thank you so much. I thought you might like to see the code, I used.

StringVar check1 ;
StringVar check2 ;
stringvar out;
numbervar b;
check1 := {cr_x.HDR};
check2 :=replace(check1,", "," ",1);
stringvar array check3:= split(check2);
out :=check3[1];
for b := 2 to ubound(check3) Do
(
out := (out & " " & check3) ;
if len(out) >= 20 then
b:= ubound(check3)
);
out

Do you see anything I should be aware of?

Again thank you. You deserve a star.
 
The only potential problem i see is if {cr_x.HDR} is ever 20 characters long without a space. If this happens your for statement may cause you problems. (for b = 2 to 1 Do)

_____________________________________
Crystal Reports XI Developer Version
Intersystems Cache 5.X ODBC connection

 
Thank you. I will be a check in the code before going into the for code.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top