Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss.
 From: Tony Vignone
  Where is Tony Vignone?
 St Augustine
 Florida - United States
 Tony Vignone
 To: Samir Ibrahim
  Where is Samir Ibrahim?
 Chekka
 Lebanon
 Samir Ibrahim
 Tags
Subject: RE: Name of a Variable
Thread ID: 227055 Message ID: 228673 # Views: 1 # Ratings: 0
Version: Visual FoxPro 9 Category: General VFP Topics
Date: Wednesday, May 20, 2009 6:34:39 PM         
   


> >
> > If you are curious and have the spare time, write your version of the function that will yield the following result (I won't show you mine yet so you have a clear mind):
>
> Something like this maybe?

Your solution is for this specific predicate. It won't work for a general one, like...

i = searchfor(@atext,1,[;
   i<alen(atext) and ("$" $ atext(i+1) or "#" $ atext(i+1)) and "enemy" $ atext(i);
   ])


>
> Clear 
> local i
> local array atext(3)
> atext(1) = "Now is the time for all to come to the aid of their country."
> atext(2) = "We has met the enemy, and he is us!"
> atext(3) = "You now have $12,456,89"
> 
> i = searchfor(@atext,1,[i<alen(atext) and "$" $ atext(i+1) and "enemy" $ atext(i)])
> IF i <= alen(atext) THEN
>    ?atext(i)
> ELSE
>    ?"NOT FOUND"
> ENDIF
> 
> Function SearchFor(atext,nStartat,cPredicate)
> 	On Error p=0
> 	nCreteria = ALines(aCreteria,cPredicate,1,"and","or")
> 	For I = 1 to Alen(aText)
> 		nAnswer = 0
> 		For loop = 1 to nCreteria
> 			If &aCreteria(loop) then 
> 				nAnswer = nAnswer + 1
> 			Else
> 				nAnswer = 0
> 			EndIf 
> 		Next loop
> 		If nAnswer = 3 then 
> 			Return I
> 		EndIf 
> 	Next I
        <B>Return I</B>
> 	On error
> EndFunc 
> 


Here's mine. It searches for the names of the array and index and replaces them with the names of the function parameter.

FUNCTION searchFor(aArray,nStartat,cPredicate)
	local arrIndex,j,p,q,r,u,v
	#DEFINE alpha "ABCDEFHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	q = pos(cPredicate,"(")				&& search for innermost (
	do while pos(cPredicate,"(",q+1) > 0
		q = pos(cPredicate,"(",q+1)	&& 1st occurance of ( at or after q+1
	enddo	 
	r = cpos(cPredicate,")+_/*",q+1)	&& 1st occurance of )+_/* at or after q+1
	p = ncposr(cPredicate,alpha,q-1)	&& 1st occurance not alpha starting from q-1 in reverse
	arrName  = substr(cPredicate,p+1,q-p-1)			&& array name
	arrIndex = substr(cPredicate,q+1,r-q-1)			&& array index nname
	v = posr(lower(cPredicate),"alen("+arrName)		&& search for alen(arrName in reverse
	do while v > 0
		u  = cposr(cPredicate,"=<>",v-1)	&& 1st occurance of =<> starting from v-1 in reverse
		u = posr(cPredicate,arrIndex,u-1)&& 1st occurance of arrIndex starting from u-1 in reverse
		if u > 0 then
			cPredicate = stuff(cPredicate,u,len(arrIndex),"j")
		endif	
		v = posr(lower(cPredicate),"alen("+arrName,u-1)	&& search for next alen(arrName from u-1 in reverse
	enddo	
	cPredicate = strtran(cPredicate,arrName+"("+arrIndex,"aArray"+"(j")
	cPredicate = strtran(cPredicate,"alen("+arrname,"alen(aArray")
	j = nStartat
	do while j<=alen(aArray) and not eval(cPredicate)
		j = j+1
	enddo
RETURN j
*-------------------



You will notice my own parsing function pos,posr,cpos,cposr and ncposr which are imbedded in my DLL utility. If you want to run the code you will need these...


function pos            && 1st occurance of wd at or after p
   lparameter ph,wd,p
   local i
   if pcount() = 2 then
      p = 1
   endif   
   if p <= 0 then
      p = 1
   endif
   i = at(wd,substr(ph,p))
   if i > 0 then
      i = i+p-1
   endif
return i    
*---------------
function posr            && 1st occurance of wd starting from the end (or p)
   lparameter ph,wd,p
   if pcount() = 2 then
      p = len(ph)
   endif   
   if p >= len(ph) then
      p = len(ph)
   endif   
return rat(wd,left(ph,p))
*---------------
function cpos           && 1st occurance of any dels at or after p
   lparameter ph,dels,p
   local i,q,r
   if pcount() = 2 then
      p = 1
   endif   
   if p <= 0 then
      p = 1
   endif   
   for i = p to len(ph)
      if at(substr(ph,i,1),dels,1) > 0 then
         return i
      endif   
   next
return 0
*---------------
function ncpos            && 1st occurance not any dels at or after p
   lparameter ph,dels,p
   local i,q,r
   if pcount() = 2 then
      p = 1
   endif   
   if p <= 0 then
      p = 1
   endif   
   for i = p to len(ph)
      if at(substr(ph,i,1),dels,1) = 0 then
         return i
      endif   
   next
return 0
*---------------
function cposr          && 1st occurance of any dels starting from the end
   lparameter ph,dels,p
   local i,q,r
   if pcount() = 2 then
      p = len(ph)
   endif   
   if p <= 0 then
      return 0
   endif   
   for i = p to 1 step -1
      if at(substr(ph,i,1),dels,1) > 0 then
         return i
      endif   
   next
return 0
*---------------
*
function ncposr          && 1st occurance not any dels starting from the end
   lparameter ph,dels,p
   local i,q,r
   if pcount() = 2 then
      p = len(ph)
   endif   
   if p <= 0 then
      return 0
   endif   
   for i = p to 1 step -1
      if at(substr(ph,i,1),dels,1) = 0 then
         return i
      endif   
   next
return 0
*---------------


ENTIRE THREAD

Name of a Variable Posted by Tony Vignone @ 5/6/2009 4:05:35 PM
RE: Name of a Variable Posted by Tom Saddul @ 5/6/2009 4:22:19 PM
RE: Name of a Variable Posted by Tony Vignone @ 5/6/2009 4:57:45 PM
RE: Name of a Variable Posted by Tom Saddul @ 5/6/2009 5:37:24 PM
RE: Name of a Variable Posted by Tony Vignone @ 5/6/2009 5:43:49 PM
RE: Name of a Variable Posted by Tony Vignone @ 5/6/2009 5:58:58 PM
RE: Name of a Variable Posted by Tom Saddul @ 5/6/2009 6:19:08 PM
RE: Name of a Variable Posted by Tony Vignone @ 5/14/2009 3:19:11 PM
RE: Name of a Variable Posted by Tom Saddul @ 5/17/2009 10:09:28 AM
RE: Name of a Variable Posted by Tony Vignone @ 5/17/2009 7:31:52 PM
RE: Name of a Variable Posted by Tom Saddul @ 5/18/2009 8:09:44 AM
RE: Name of a Variable Posted by Tony Vignone @ 5/18/2009 3:52:23 PM
RE: Name of a Variable Posted by Tom Saddul @ 5/19/2009 3:41:37 PM
RE: Name of a Variable Posted by Tony Vignone @ 5/19/2009 4:02:11 PM
RE: Name of a Variable Posted by Tony Vignone @ 5/18/2009 6:20:29 PM
RE: Name of a Variable Posted by Samir Ibrahim @ 5/19/2009 6:56:28 PM
RE: Name of a Variable Posted by Tony Vignone @ 5/19/2009 9:10:40 PM
RE: Name of a Variable Posted by Samir Ibrahim @ 5/20/2009 10:21:14 AM
RE: Name of a Variable Posted by Tony Vignone @ 5/20/2009 6:34:39 PM
RE: Name of a Variable Posted by Tom Saddul @ 5/21/2009 1:52:57 AM
RE: Name of a Variable Posted by Tony Vignone @ 5/21/2009 7:33:39 PM
RE: Name of a Variable Posted by Samir Ibrahim @ 5/21/2009 10:35:02 AM
RE: Name of a Variable Posted by Tony Vignone @ 5/21/2009 7:58:52 PM
RE: Name of a Variable Posted by Samir Ibrahim @ 5/22/2009 11:04:13 AM
RE: Name of a Variable Posted by Tony Vignone @ 5/22/2009 3:07:38 PM
RE: Name of a Variable Posted by tushar @ 5/6/2009 6:10:53 PM