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

extract part of a string 1

Status
Not open for further replies.

BettyJ

Programmer
Jul 25, 2012
269
US
I have a field with the following possible values
1)
-aaa- these are ...

2)
-aaa- these are ...
-bbb- these are ...

3)
-bbb- these are ...

4)
-aaa- these are ...
-bbb- these are ...
-ccc- these are ...

I just need to extract the portion within the hyphens and display it like:
1)
aaa

2)
aaa
bbb

3)
bbb

4)
aaa
bbb
ccc

Can anyone help me? Thanks in advance.

 
Here's how I would do it:

whileprintingrecords;
numbervar v_firsthyphen;
numbervar v_secondhyphen;

v_firsthyphen := instr({YourField},"-");
v_secondhyphen := instr(mid({YourField},v_firsthyphen + 1),"-");


mid({@Value},v_firsthyphen + 1, v_secondhyphen - 1)
 
Sorry, forgot one change:

Here's how I would do it:

whileprintingrecords;
numbervar v_firsthyphen;
numbervar v_secondhyphen;

v_firsthyphen := instr({YourField},"-");
v_secondhyphen := instr(mid({YourField},v_firsthyphen + 1),"-");


mid({YourField},v_firsthyphen + 1, v_secondhyphen - 1)
 
Thank you, Brian for your reply.

When I tried your formula, I got part of what I need. I got aaa in eg 2) of my post. How will I get bbb also?
 
Can you post what your raw data looks like? I thought that each value you posted was one record, and that the 1), 2), etc. were Crystal groups.
 
Thanks Brian.

What actually I have as data is conditions of patients. Patients can have multiple conditions. The condition field includes a description of the condition along with it as shown in the examples below. I need only the part of the string within hyphens as the output.

eg 1. patient has

-Hypertension- Elevated BP
-Diabetes Mellitus- Elevated Blood Sugar

output should be
Hypertension
Diabetes Mellitus
---------------------------------------
eg 2. patient has

-Hypertension- Elevated BP
-CVA- Stroke
-Diabetes Mellitus- Elevated Blood Sugar

output should be

Hypertension
CVA
Diabetes Mellitus
---------------------------------------

eg 3. patient has
-Hypertension- Elevated BP

output should be

Hypertension
-----------------------------------------

Hope you got what I am trying to do.
 
Try the following formula:

Code:
WhilePrintingRecords;

Local NumberVar i;
Local NumberVar S;
Local NumberVar L;

Global StringVar Result := '';

For i := 1 to UBound(Split({Table.Data}, CHR(10))) Do
(
    S := (Instr(Split({Table.Data}, CHR(10))[i], '-')) + 1;
    L := Instr((Instr(Split({Table.Data}, CHR(10))[i], '-'))+1,Split({Table.Data}, CHR(10))[i], '-') - S;
    Result := Result + Mid(Split({Table.Data}, CHR(10))[i], S, L) + CHR(10)
);

Result

This assumes that the character that forces the new lines in your data is a Line Feed, but it could be a Carriage Return or possibly a Vertical Tab which all do the same thing. If the code does not work, replace all occurrences of "CHR(10)" with "CHR(13)", and if that doesn't work "CHR(11)".

Hope this helps.

Cheers
Pete
 
Thank you Pete.
I tried your formula, but I am getting an error "String length is less than 0 or not an integer" in the line
Result := Result + Mid(Split({RPTOBS.OBSVALUE}, CHR(10)), S, L) + CHR(10)

When i replaced CHR(10) with CHR(13), I am getting only the first values within hyphens.

When I tried with chr(11), I am getting the first values and a square symbol.
--------------------------------------------------------

i was trying to do something similar using split function.
i was checking to see if i can just display the second part of the string I need.

whileprintingrecords;
stringvar array polyp_names:=split({RPTOBS.OBSVALUE},"-");
if ubound(polyp_names)>2 then polyp_names[4] else
polyp_names[2];

But I am getting this error.
"a subscript must be between 1 and the size of the array"
I believe it is because of the size of the string.

Can you provide some suggestions?
 
Thank you Pete.
I tried your formula, but I am getting an error "String length is less than 0 or not an integer" in the line
Result := Result + Mid(Split({RPTOBS.OBSVALUE}, CHR(10)), S, L) + CHR(10)

When i replaced CHR(10) with CHR(13), I am getting only the first values within hyphens.

When I tried with chr(11), I am getting the first values and a square symbol.
--------------------------------------------------------

i was trying to do something similar using split function.
i was checking to see if i can just display the second part of the string I need.

whileprintingrecords;
stringvar array polyp_names:=split({RPTOBS.OBSVALUE},"-");
if ubound(polyp_names)>2 then polyp_names[4] else
polyp_names[2];

But I am getting this error.
"a subscript must be between 1 and the size of the array"
I believe it is because of the size of the string.

------------------------------------------------------------
By the way, in the data, when there are multiple values, there is a blank line separating those.

Can you provide some suggestions?
 
Pete,
Just an update
When I take just one record that has multiple (2)values the following formula, it works perfect

whileprintingrecords;
stringvar array polyp_names:=split({RPTOBS.OBSVALUE},"-");
polyp_names[2] +chr(13)+polyp_names[4];

when it reads the next record with 1 value then I feel the size of the array has to be dynamically changed. How can this be done?

Thank you.
 
OK, it must be the CHR(10) that is creating the line breaks.

The additional line between records is crucial information that should have been included in your original post. You make it difficult to assist when you provide incomplete information.

Amend the formula as follows:

Code:
WhilePrintingRecords;

Local NumberVar i;
Local NumberVar S;
Local NumberVar L;
Local StringVar TEXT := Replace(Replace({RPTOBS.OBSVALUE}, Chr(10), '/'), '//', Chr(10));
Global StringVar Result := '';


For i := 1 to UBound(Split(TEXT, CHR(10))) Do
(
    S := (Instr(Split(TEXT, CHR(10))[i], '-')) + 1;
    L := Instr((Instr(Split(TEXT, CHR(10))[i], '-'))+1,Split(TEXT, CHR(10))[i], '-') - S;
    Result := Result + Mid(Split(TEXT, CHR(10))[i], S, L) + CHR(10)
);

Result

Pete
 
Thanks a lot. That worked perfect! I'm sorry for not mentioning that there is a blank line in the earlier posts.

Thanks again.
Betty

 
Hi Pete,
I am going to ask you another favor - could you please explain your code.
Today I was looking at the code.
Local StringVar TEXT := Replace(Replace({RPTOBS.OBSVALUE}, Chr(10), '/'), '//', Chr(10));
why is this needed.

Thank you.
Betty
 
Hi Betty

This is the code that deals with the blank line. We determined that the multiple lines separated by the blank line were caused by consecutive Chr(10) characters.

The code replaces all instances of Chr(10) with slashes ('/') and then replaces all instances of consecutive slashes ('//') with a Chr(10). Essentially it is just a way to replace consecutive Chr(10)s with a single Chr(10) because at the time I wasn't sure that looking for consecutive Chr(10)s would work.

I have since tested:

Code:
Local StringVar TEXT := Replace({Table.Data}, (Chr(10)+CHR(10)), CHR(10))

, which does also work.

Now I have confirmed that, it would also be possible to omit that step completely and go straight to using SPLIT to separate the multiple lines on the character sequence (Chr(10)+CHR(10)). That code would be:

Code:
WhilePrintingRecords;

Local NumberVar i;
Local NumberVar S;
Local NumberVar L;
Global StringVar Result := '';


For i := 1 to UBound(Split({RPTOBS.OBSVALUE}, (CHR(10)+Chr(10)))) Do
(
    S := (Instr(Split({RPTOBS.OBSVALUE}, (CHR(10)+Chr(10)))[i], '-')) + 1;
    L := Instr((Instr(Split({RPTOBS.OBSVALUE}, (CHR(10)+Chr(10)))[i], '-'))+1,Split({RPTOBS.OBSVALUE}, (CHR(10)+Chr(10)))[i], '-') - S;
    Result := Result + Mid(Split({RPTOBS.OBSVALUE}, (CHR(10)+Chr(10)))[i], S, L) + CHR(10)
);

Result

Personally, I find the original approach easier to read and comprehend, and I don't see there would be a significant impact on report efficiency, so I would probably stick with it. At least then in 6 or 12 months time when I need to make changes to the report I wouldn't need to spend time trying to understand the formula.

As with most challenges in CR here are multiple ways of tackling the problem. In this case there are many variations that would work, including the use of an Array. In this instance I opted for a String variable over n Array because there was no benefit in using an array. If the result was going to be used in subsequent formulas (ie, other than for simple display purposes), an Array would have been a better approach.

Hope this helps.

Cheers
Pete
 
Thank you very much, Pete. You are a great help for users of this forum.
 
Thanks for the kind words Betty. I have learned a lot from this forum over the past 10 years, so it is only fair I give back now that I am able to.

Pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top