Search for specific words in a string...

i have a string...example:

Code:
test1234: 12345957827

and i have a list with words:

"test,progress"

how can i test if the string contains one of the words?


thank you for your help and sorry for my bad english
 

StuartT

Member
Code:
def var w-i as int.
def var w-instring as logical init false.
 
do w-i = 1 to num-entries(<list>):
  if lookup(entry(w-i,<list>),<string>) <> 0 then
  do:
    assign w-instring = true.
    leave.
  end.
end.
 

mobrien

New Member
You could use the following.

DEFINE VARIABLE iLoop AS INTEGER NO-UNDO.
DEFINE VARIABLE lExists AS LOGICAL NO-UNDO.
DEFINE VARIABLE cString AS CHARACTER NO-UNDO.
DEFINE VARIABLE cKeyWords AS CHARACTER NO-UNDO.

ASSIGN cString = "test1234: 12345957827"
cKeyWords = "test,progress".

ASSIGN lExists = FALSE.
DO iLoop = 1 TO NUM-ENTRIES(cKeyWords):

IF INDEX(cString,ENTRY(iLoop,cKeyWords)) <> 0 THEN
DO:
ASSIGN lExists = TRUE.
LEAVE.
END.
END.

Thanks.
 

StuartT

Member
The most concise way IMO is:

IF CAN-DO("test,progress","123456789").

that will not work as far as I can see. I tried out the following:
Code:
 IF CAN-DO("test,progress","thisisatestok") then
            display "ok".
          else
            display "no".
now as you can see the string contains the word "test" which is one of the items in the list, however the can-do returns "no" when it is quite clearly in the string. It would be the most concise if that was the way that "CAN-DO" worked.

Just checked out my first solution and it doesn't work either:blush1:.
Mobrien's way does work however.
 

schaapie

Member
The question is do you want searching for "test" in "test123: 123456" to give a true of false?
Do you want "complete entries" using , or : as seperators?
Code:
index("test123: 123456","test") = 1
index("test123: 123456","test123") = 1
index("test123: 123456","123") = 5 
index("test123: 123456","123", 6) = 10 /* start-positition, second 123 */
 
lookup("test",   "test123: 123456") = 0
lookup("123",    "test123: 123456") = 0
lookup("test123","test123: 123456") = 0 /* default seperator , */
lookup("test123","test123: 123456",":") = 1 /* seperator */
 
can-do("test123, 123456","test" ) = no
can-do("test123, 123456","test123") = yes
can-do("test123, 123456","123" ) = no

can-do roughly acts the same as the first lookup here only parameters are other way around and can't handle the seperator. Then you will have to extract the entries from "test123: 123456" using entry() with a seperator.
 

TomBascom

Curmudgeon
CAN-DO() should not be used as a string search function. It is a security function that was perverted to string operations way back in the old days before LOOKUP(), INDEX(), ENTRY() et al existed.

When used to search strings CAN-DO() has a number of major deficiencies:

1) It forces query resolution to the client when used in a WHERE clause.

2) There are "magic" characters that impact the results in very unexpected ways. Especially if you do not realize that this is a security function being misused. Read the "notes" section of the documentation very, very carefully.

3) Behavior varies depending on who runs the code -- it acts differently if "root" is the user.

4) It is misleading -- it looks like you're doing a security thing when, in fact, you're doing something quite different.

This perverted usage has stuck around and been perpetrated by legacy applications and code bases for far too long. It should be stamped out and unrepentant practitioners should be burned at the stake. ;)
 

KrisM

Member
LOL
I can still remember very old development guidelines saying you MUST use can-do before lookup because it was faster.
 

Daniel Vanzo

New Member
An option:

DEFINE VARIABLE cString AS CHARACTER NO-UNDO.
DEFINE VARIABLE cWords AS CHARACTER NO-UNDO.
DEFINE VARIABLE i AS INTEGER NO-UNDO.
ASSIGN
cString = "test1234: 12345957827"
cWords = "test,progress,578".
REPEAT i = 1 TO NUM-ENTRIES(cWords):
DISPLAY ENTRY(i,cWords) cString MATCHES ("*" + ENTRY(i,cWords) + "*")
WITH STREAM-IO.
END.

As you can see, it found "test" and "578" aswell, so it depends about exactly what do you need...

Regards,

Daniel
 
Top