Link to home
Start Free TrialLog in
Avatar of LBarrett
LBarrett

asked on

Crystal Reports, ver 8.5, extract portion of text field as number

Hi,
I need to extract a portion of a text field (not memo) as a number.  Example:

Dust all rooms, 6 minutes per day, 5 times per week.
I want the 6 only

Dishwashing, 20 minutes per meal per day
I want the 20

Errand.  Pick up clients prescriptions from pharmacy. 15 minutes per week.
I want the 15 only

I will need to total these numbers as minutes.  The phrases can have more than one number, it can be preceded by a comma, period or whatever.  The only thing consistent is the word "minutes" in the field.  The field name is task.des.  Also not every field has minutes in it so I received an error unless I tested the field first like:

if {task.des} like "* minutes *"then...

and return blank or zero if there are no "minutes" if instr() is used.

Tried this from this forum:

stringvar sentence := {PwHcTelRptTasks.TskDes};
stringvar array words := split(sentence," ");
numbervar i;
stringvar result := "";

for i := 1 to ubound(words)
do
(
    if (left(words[i],1) = "(" and right(words[i],1) = ")") then
        (i := i + 1;
         "")
    else
        if isnumeric(words[i]) then
            result := words[i]
);
result;

it worked fine until the first example above of "6 minutes per day, 5 times per week" in this case the 5 was returned not the 6.

Any help would be appreciated!



Open in new window

Avatar of Mike McCracken
Mike McCracken

What do you want returned if MINUTES is not in the sentence?

I assume you want the number that is before the word minutes

Try the formula below

mlmcc

Local StringVar TestField := {task.des};
Local StringVar array Words;
Local NumberVar i := 0;
Local NumberVar index := -1;
 
If (InStr(UpperCase(TestField),'MINUTES')) > 0 then
(
    Words := Split(TestField,' ');
    for i := 2 to UBound(Words) do
        If UpperCase(Words[i]) = 'minutes' then
            If IsNumeric(Words[i-1]) then
                index := i-1;
);
if index > -1 then
    Words[index]
else
    ' '

Open in new window

Avatar of LBarrett

ASKER

Thanks for the quick reply.  Returned the correct value but how to total this?  When I try to sum the fields I get the "this field must be numeric.." error.  Any suggestions?
If minutes is not in phrase then blank or zero doesn't matter as long as I can total the field.  And yes then value I need is the number of minutes.
If you need to total it then

if index > -1 then
    Val(Words[index])
else
    0

mlmcc
99% there. The formula works as does the totals, the one exception I have found so far is if the word "minutes" is followed by a period.  "15 minutes."  The formula returns zero.
Now that this is mostly working I was able to find another anomaly.  Often the word "minutes" is preceded by the word "extra":  "30 extra minutes" also the "number" of minutes can be the first word in the field such as: "20 extra minutes needed per day for...".  Also found this in a few instances: " 120 minutes for shopping and 30 extra minutes for accompanying."  I assume the formula would return the last value only.  If it could return both either as a total or even 2 separate values that would be great, if not then I can make do without it.

Since problem has increased in complexity I am raising the point value.
FOr multiple like  120 minutes for shopping and 30 extra minutes for accompanying."  
It should return the first one.  What do you need returned?

To handle the . you can use

If (InStr(UpperCase(TestField),'MINUTES')) > 0 then
(
    Words := Split(TestField,' ');
    for i := 2 to UBound(Words) do
        If UpperCase(Words[i]) IN ['MINUTES','MINUTES.']  then
            If IsNumeric(Words[i-1]) then
                index := i-1;
);
if index > -1 then
    Words[index]
else
    ' '

COuld there be a , or other puncuation after the minutes?

mlmcc
I added 'MINUTES,' to the IN statement and found a few fields like this.   Still can't get the fields with just a single value and the word "extra" in front like  "add 30 extra minutes..." to return a value other than 0.  I am getting correct value for the minutes with the period now, thanks.  For the case of multiples like "120 for... and 30 extra minutes" ideal would be to return both values either separately or added together.
You can add EXTRA MINUTES to the list above and it should catch some of them if they are alone.  Too get both will reuire a different solution.  Let me think about it and I'll post my idea.

mlmcc
Added extra minutes like shown below: and it did not catch the minutes in the string "20 extra minutes needed for ....".  The field began with the "20" and did not include any other numbers.

Local StringVar TestField := {PwHcTelRptTasks.TskDes};
Local StringVar array Words;
Local NumberVar i := 0;
Local NumberVar index := -1;

if ({PwHcTelRptTasks.TskSts} like "Completed" )then
 
(If (InStr(UpperCase(TestField),'MINUTES')) > 0 then
(
    Words := Split(TestField,' ');
    for i := 2 to UBound(Words) do
        If UpperCase(Words[i]) in  ['minutes','minutes.','minutes,','extra minutes'] then
            If IsNumeric(Words[i-1]) then
                index := i-1;
);

if index > -1 then
    Val(Words[index])
else
    0)

    else
    0
If you are going to UpperCase the text from the field the list should also be in uppercase.

To get all the values you will need to modify the loop


Local StringVar TestField := {task.des};
Local StringVar array Words;
Local NumberVar i := 0;
Local NumberVar index := -1;
Local NumberVar MinutesValue := 0;
 
If (InStr(UpperCase(TestField),'MINUTES')) > 0 then
(
    Words := Split(TestField,' ');
    for i := 2 to UBound(Words) do
        If UpperCase(Words[i]) = 'minutes' then
            If IsNumeric(Words[i-1]) then
                MinutesValue := MinutesValue + Val(Words[i-1];
);
MinutesValue 
 
This will print 0 if the number isn't there
 
If you want it to print nothing then
 
If MinutesValue >0 then
   ToText(MinutesValue,0,'')
else
  ' '
 
mlmcc

Open in new window

Unsure about your reference above if I "UpperCase the text from the field..." and what that has to do with the formula retrieving the 20 from the string "20 extra minutes...".

Did change the formula as shown but still did not get any values returned when the value is followed by the word "extra".
ASKER CERTIFIED SOLUTION
Avatar of Mike McCracken
Mike McCracken

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for the delay was out of town.  This formula works great so the only outstanding part is the sentence with 2 values in it like " 120 minutes for shopping and 30 extra minutes for accompanying.." I was hoping to get both values returned separately or added together.  If this should be a new question that's fine.  I will award points for this question and open a new one later.

Thanks for the help.

Points to mlmcc

LBarrett
Thanks for the help. I am calling the solution complete since the question kept expanding beyond what I orginally asked.  Hopefully I will be able to get the rest of it figured out or ask a new question if needed.  Thank you again you were a great help.