Focal Point
[SOLVED] Find specific word in a string

This topic can be found at:
https://forums.informationbuilders.com/eve/forums/a/tpc/f/7971057331/m/3747007495

December 03, 2010, 02:04 AM
ChristianP
[SOLVED] Find specific word in a string
Hello,
is there a function to find a specific word in a string and to count it. i need this for a flexible
html-form. in the form i want to make a drilldown on a specific coloum, but the coloumnumber is flexible.

thanks a lot

This message has been edited. Last edited by: Kerry,


WF Production Version: 7.7.02M
WF Test Version: 7.7.02M
Developer Studio: 7.7.02
HTML, EXL2K, FLEX, PDF,PPT
December 03, 2010, 03:36 AM
GamP
quote:
is there a function to find a specific word in a string
Look up the POSIT function, it does just that.
quote:
and to count it
Now that's a different thing. WebFocus does not have any standard procedure that can do that for you. So you'll have to think up some workaround to get the count. Search this forum for that, I'm sure there is some thread that can give you a hint on how to do that.


GamP

- Using AS 8.2.01 on Windows 10 - IE11.
in Focus since 1988
December 03, 2010, 04:15 PM
T.Peters
quote:
Originally posted by GamP:
quote:
and to count it
Now that's a different thing. WebFocus does not have any standard procedure that can do that for you. So you'll have to think up some workaround to get the count. Search this forum for that, I'm sure there is some thread that can give you a hint on how to do that.


Are we talking a count of the number of letters or count of the number of occurences of whatever word youre looking for?


WebFOCUS: 7702
O/S : Windows
Data Migrator: 7702
December 04, 2010, 08:07 AM
Dan Satchell
Here is a possible solution. I used Dialogue Manager but with some minor changes this should work in a DEFINE, too. First replace the search string ('car' in this example) with a single character that won't ever appear in your text string (I used the tilde ~ here). Next remove that character from the second string. The difference between the lengths of the second and third strings will give you the number of occurrences of the search string in the original string. The search string 'car' appears four times in the original string.

-SET &TEXT1     = 'this car that car those cars all have "scars"!';
-SET &TEXT2     = STRREP(&TEXT1.LENGTH,&TEXT1,3,'car',1,'~',&TEXT1.LENGTH,'A&TEXT1.LENGTH');
-SET &TEXT3     = STRIP(&TEXT2.LENGTH,&TEXT2,'~','A&TEXT2.LENGTH');
-SET &TEXT2_LEN = ARGLEN(&TEXT2.LENGTH,&TEXT2,'I3');
-SET &TEXT3_LEN = ARGLEN(&TEXT3.LENGTH,&TEXT3,'I3');
-SET &COUNTX    = &TEXT2_LEN - &TEXT3_LEN ;
-TYPE &TEXT1
-TYPE &TEXT2
-TYPE &TEXT3
-TYPE &TEXT2_LEN
-TYPE &TEXT3_LEN
-TYPE &COUNTX


If you need to find whole words, then put spaces around the search string (i.e., ' car '):

-SET &TEXT2 = STRREP(&TEXT1.LENGTH,&TEXT1,5,' car ',1,'~',&TEXT1.LENGTH,'A&TEXT1.LENGTH');



WebFOCUS 7.7.05
December 04, 2010, 09:01 AM
Ram Prasad E
quote:
-SET &TEXT2 = STRREP(&TEXT1.LENGTH,&TEXT1,3,'car',1,'~',&TEXT1.LENGTH,'A&TEXT1.LENGTH');
-SET &TEXT3 = STRIP(&TEXT2.LENGTH,&TEXT2,'~','A&TEXT2.LENGTH');


Dan,
This is a good one. But I would prefer to replace with nothing instead of '~'. If my input string already contains '~', then here you go.

-SET &TEXT1      = 'this ~car that car ~those cars all have "scar~s"!';
-SET &SEARCH_STR = 'car';

-SET &TEXT2      = STRREP(&TEXT1.LENGTH,&TEXT1,&SEARCH_STR.LENGTH,&SEARCH_STR,0,'~',&TEXT1.LENGTH,'A&TEXT1.LENGTH');
-SET &TEXT2      = TRUNCATE(&TEXT2);
-SET &TEXT1_LEN  = ARGLEN(&TEXT1.LENGTH,&TEXT1,'I3');
-SET &TEXT2_LEN  = ARGLEN(&TEXT2.LENGTH,&TEXT2,'I3');

-SET &SEARCH_LEN = ARGLEN(&SEARCH_STR.LENGTH,&SEARCH_STR,'I3');

-SET &COUNTX     = (&TEXT1_LEN - &TEXT2_LEN) / &SEARCH_LEN ;

-TYPE &TEXT1
-TYPE &SEARCH_STR 
-TYPE &TEXT2
-TYPE &TEXT1_LEN
-TYPE &SEARCH_LEN
-TYPE &TEXT2_LEN
-TYPE &COUNTX



WebFOCUS 8.1.05
Windows
http://ibiwebfocus.wordpress.com
https://www.facebook.com/groups/ibi.webfocus/
December 04, 2010, 01:45 PM
Dan Satchell
You're absolutely right Ram. I tried this:

-SET &TEXT2 = STRREP(&TEXT1.LENGTH,&TEXT1,3,'car',0,'',&TEXT1.LENGTH,'A&TEXT1.LENGTH');

But it gave me an error message. It never occurred to me to just throw any character between the single quotes for the replacement string:

-SET &TEXT2 = STRREP(&TEXT1.LENGTH,&TEXT1,3,'car',0,'x',&TEXT1.LENGTH,'A&TEXT1.LENGTH');

But it works because the replacement string length is set to zero. Great job finding a "trick" that works.


WebFOCUS 7.7.05
December 05, 2010, 03:23 PM
Waz
Dan, that not good, its brilliant.

Next stop Mensa ?




Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!

December 05, 2010, 04:08 PM
Dan Satchell
Thanks Waz. We all have our brilliant moments and our "what the Focus was I thinking" moments. But actually I think there was a similar post in the past year. I couldn't find it but I seemed to remembered the basic concept from that post. Cheers.


WebFOCUS 7.7.05
December 05, 2010, 04:24 PM
Waz
Well, no matter who had the idea, it is a brilliant piece of left field thinking.


Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!

December 05, 2010, 04:36 PM
Francis Mariani
How to get multiple string variables from a parameter? [SOLVED] has an example of a DEFINE FUNCTION...


Francis


Give me code, or give me retirement. In FOCUS since 1991

Production: WF 7.7.05M, Dev Studio, BID, MRE, WebSphere, DB2 / Test: WF 8.1.05M, App Studio, BI Portal, Report Caster, jQuery, HighCharts, Apache Tomcat, MS SQL Server
December 05, 2010, 04:47 PM
Waz
Oh dear, I had forgotten about that one, even posted against it.

Another senior moment!

As my son says, senior moment, another 5 minutes off your life.

Frowner


Waz...

Prod:WebFOCUS 7.6.10/8.1.04Upgrade:WebFOCUS 8.2.07OS:LinuxOutputs:HTML, PDF, Excel, PPT
In Focus since 1984
Pity the lost knowledge of an old programmer!

December 06, 2010, 04:14 AM
ChristianP
Thanks to all!!
i will try the solutions. i build one by using the posit function.
-SET &SORTBY1=STRIP(60, &Sortfeld2, ' ', 'A60');
-SET &SORTBY=POSIT(&SORTBY1,60,'BY',2, I2);
-SET &ROUND1 = IF SORTBY1 GT '0' THEN '1' ELSE '0';
-SET &SORTBY2 = SUBSTR(60, &SORTBY1, &SORTBY + 1, 60, 50, A60);
-SET &SORTBY_1=POSIT(&SORTBY2,60,'BY',2, I2);
-SET &ROUND2 = IF &SORTBY_1 GT '0' THEN '1' ELSE '0';
-SET &SORTBY3 = SUBSTR(60, &SORTBY2, &SORTBY_1 + 1, 60, 50, A60);
-SET &SORTBY_2=POSIT(&SORTBY3,60,'BY',2, I2);
-SET &ROUND3 = IF &SORTBY_2 GT '0' THEN '1' ELSE '0';
-SET &SORTBY4 = SUBSTR(60, &SORTBY3, &SORTBY_2 + 1, 60, 50, A60);
at the end i counted the &ronds.
i think your solutions are much smarter but im just a beginner!!!

greetings

christian


WF Production Version: 7.7.02M
WF Test Version: 7.7.02M
Developer Studio: 7.7.02
HTML, EXL2K, FLEX, PDF,PPT
December 06, 2010, 09:43 AM
GaryB
Can you use CONTAINS in a DEFINE?

DATA/A1 = IF str1 CONTAINS 'XYZ' THEN 'Y' ELSE 'N';