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!

Trouble with a function

Status
Not open for further replies.

lebronletchev

Programmer
Oct 17, 2007
17
ES
Hi,

I have these function copyright by Borislav Borissov

**************example*****************
?GetTextBetween([be],;
[bat],;
[but she's a pretty girl who happens to be as blind as a bat])
**************************************

***********************************start of function*
FUNCTION GetTextBetween
LPARAMETERS lcVar1, lcVar2, lcText
lnVar1Pos = AT(lcVar1,lcText,1)
lnVar2Pos = AT(lcVar2,lcText,1)
IF lnVar1Pos > 0 AND lnVar2Pos > 0 AND lnVar1Pos < lnVar2Pos
RETURN SUBSTR(lcText,lnVar1Pos,(lnVar2Pos-lnVar1Pos))
ELSE
RETURN []
ENDIF
ENDFUNC

************************************ end of function*

TROUBLE
_______

Occurs when the function find words in reverse order!

for example:

?GetTextBetween([bats],;
[pretty],;
[but she's a pretty girl who likes bats, happens to be as blind as a bat]).

Here it consider the "bats" and "pretty" even if in inverted order. In the above example the text between
variables are wrong, but it show them as correct!

How I change the function for searching just in right order, in other words, lcVar1 must be before lcVar2 ALWAYS.

Thanks a lot

Lebron Letchev
 
Hmmm,
It is not mine. It is Ken Murphy's one. I just add one more test to it :)
Try this:
Code:
FUNCTION GetTextBetween
 LPARAMETERS lcVar1, lcVar2, lcText
 LOCAL lnVar1Pos, lnVar2Pos
 LOCAL lnPos1, lnPos2  

 lnVar1Pos = AT(lcVar1,lcText,1)
 lnVar2Pos = AT(lcVar2,lcText,1)
 IF lnVar1Pos > 0 AND lnVar2Pos > 0
    lnPos1 = MIN(lnVar1Pos, lnVar2Pos)
    lnPos2 = MAX(lnVar1Pos, lnVar2Pos)
    RETURN SUBSTR(lcText,lnPos1,lnPos2)
 ELSE
    RETURN []
 ENDIF
 ENDFUNC
not tested at all

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
Microsoft MVP VFP
 
Oops,
Code:
FUNCTION GetTextBetween
 LPARAMETERS lcVar1, lcVar2, lcText
 LOCAL lnVar1Pos, lnVar2Pos
 LOCAL lnPos1, lnPos2  

 lnVar1Pos = AT(lcVar1,lcText,1)
 lnVar2Pos = AT(lcVar2,lcText,1)
 IF lnVar1Pos > 0 AND lnVar2Pos > 0
    lnPos1 = MIN(lnVar1Pos, lnVar2Pos)
    lnPos2 = MAX(lnVar1Pos, lnVar2Pos)
    RETURN SUBSTR(lcText,lnPos1,(lnPos2-lnPos1))
 ELSE
    RETURN []
 ENDIF
 ENDFUNC

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
Microsoft MVP VFP
 
Borislav

Sorry. Copyright of Ken Murphy adjusted by Borislav B.

Unfortunately the trouble persists!

Again other example:

if the script lloking for "Greece" and "Spain" it take into account the following text as VALID, but in reality it is NOT VALID!

"We have seen Spain, Greece and other countries fare extremely well against our so-called best."

Valid will be just "Spain" and "Greece"

Thanks and my apologies by inconvenience.

Lebron
 
This for example?

x="We have seen Spain, Greece and other countries fare extremely well against our so-called best."

? STREXTRACT(x,"have","our",0,2)
? STREXTRACT(x,"Spain","Greece",0,2)

This not works!

Lebron
 
Why it is not valid?

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
Microsoft MVP VFP
 
Because if you are searching "Spain" and "Greece" in this order when the output it is a string where there are these two words BUT IN INVERSE ORDER! Greece and Spain, for example. In short words it don´t take into consideration ALWAYS lcVar1 MUST BE BEFORE lcVar2.

The function must force for lcVar1 be before lcVar2. ALWAYS!

Thanks

Lebron
 
Code:
lll = "We have seen Spain, Greece and other countries fare extremely well against our so-called best."
? GetTextBetween([Greece],[Spain] ,lll)
? GetTextBetween([Spain] ,[Greece],lll)

FUNCTION GetTextBetween
 LPARAMETERS lcVar1, lcVar2, lcText
 LOCAL lnVar1Pos, lnVar2Pos
 LOCAL lnPos1, lnPos2  

 lnVar1Pos = AT(lcVar1,lcText,1)
 lnVar2Pos = AT(lcVar2,lcText,1)
 IF lnVar1Pos > 0 AND lnVar2Pos > 0
    lnPos1 = MIN(lnVar1Pos, lnVar2Pos)
    lnPos2 = MAX(lnVar1Pos, lnVar2Pos)
    lnAdd  = IIF(lnPos1 # lnVar1Pos,LEN(lcVar2),LEN(lcVar1))
    RETURN SUBSTR(lcText,lnPos1,(lnPos2-lnPos1+lnAdd)+1)
 ELSE
    RETURN [aaa]
 ENDIF

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
Microsoft MVP VFP
 
Borislav,

I don´t understand.


It return .T. although lcvar1 = "Greece" and lcvar2 = "Spain" when would return .F.


lcvar1 = "Greece"
lcvar2 = "Spain"

lll = "We have seen Spain, Greece and other countries fare extremely well against our so-called best."
? GetTextBetween([lcvar1],[lcvar2] ,lll)


FUNCTION GetTextBetween
LPARAMETERS lcVar1, lcVar2, lcText
LOCAL lnVar1Pos, lnVar2Pos
LOCAL lnPos1, lnPos2

lnVar1Pos = AT(lcVar1,lcText,1)
lnVar2Pos = AT(lcVar2,lcText,1)
IF lnVar1Pos > 0 AND lnVar2Pos > 0
lnPos1 = MIN(lnVar1Pos, lnVar2Pos)
lnPos2 = MAX(lnVar1Pos, lnVar2Pos)
lnAdd = IIF(lnPos1 # lnVar1Pos,LEN(lcVar2),LEN(lcVar1))
RETURN SUBSTR(lcText,lnPos1,(lnPos2-lnPos1+lnAdd)+1)
* ELSE
* RETURN
ENDIF


Thanks again

Lebron
 
You pass lcVar1 and lcVar2 as strings not as variables. The function parameter lcVar1 is equal to [lvar1]
and because both strings are not contained in the string the function ends. and because you didn't have any return value in such case (you commented it) the function returns .t. (by default).
Try:
Code:
lcvar1 = "Greece"
lcvar2 = "Spain"

lll = "We have seen Spain, Greece and other countries fare extremely well against our so-called best."
? GetTextBetween(m.lcvar1,m.lcvar2 ,m.lll) && See the changes here
? GetTextBetween([lcvar1],[lcvar2] ,m.lll)

FUNCTION GetTextBetween
 LPARAMETERS lcVar1, lcVar2, lcText
 LOCAL lnVar1Pos, lnVar2Pos
 LOCAL lnPos1, lnPos2  

 lnVar1Pos = AT(lcVar1,lcText,1)
 lnVar2Pos = AT(lcVar2,lcText,1)
 IF lnVar1Pos > 0 AND lnVar2Pos > 0
    lnPos1 = MIN(lnVar1Pos, lnVar2Pos)
    lnPos2 = MAX(lnVar1Pos, lnVar2Pos)
    lnAdd  = IIF(lnPos1 # lnVar1Pos,LEN(lcVar2),LEN(lcVar1))
    RETURN SUBSTR(lcText,lnPos1,(lnPos2-lnPos1+lnAdd)+1)
ENDIF

RETURN [Nothing here] && Change this to what you want if some of the strings is not found.



Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
Microsoft MVP VFP
 
Borislav,

After a little change in script oit works as I want.

Thanks for all.

Lebron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top