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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting strings in memo fields 1

Status
Not open for further replies.

virtualranger

Technical User
Sep 14, 2001
115
0
0
GB
I have two different strings stored in variables. Var1 only occurs once in the memo field, var2 can occur more than once and could be postioned both before and after var1.

I need to count how many times var2 occurs AFTER the occurance of var1 in the memo.

I can use ATLINE() to return the line number of var1 but I'm not sure how to count the occurences of var2 thereafter. As far as I know ATLINE() only looks for the first instance of a string so I'm not sure how to procede or even if it's possible.

Any help greatly appreciated.

Thanks,
Jamie
 
Try something like this:
Code:
function yourcount
parameters var1, var2, string
private var2hits, firstvar1, newstring
var2hits  = 0
firstvar1 = at(var1, string)
if firstvar1 > 0
   newstring = substr(string, firstvar1 + 1)
   do while at(var2, newstring) > 0
      var2hits  = var2hits + 1
      newstring = substr(newstring, ;
                  at(var2, newstring)+1)
   enddo
endif
return(var2hits)

hope it helps.
 
Thanks for the reply.

Ah ok, I see what you're doing there. You're creating a new string each time you search for var2, starting from the last point the variable was found. The only problem is that this will need to be performed on 1000's of clients in our db and some of them have memo fields approaching the fox pro size limit. It may be a bit slow but it looks like something similar will work. I'll give it a go and see how I get on.

Thanks once again.

Thanks,
Jamie
 
If you look at Foxpro's Help listing for the AT() command you will see that it can apply to any number of occurances.

Depending on the size of the Memo field content and as long as the value for var2 does not change, maybe something like:

mcMemoText = ALLTRIM(MyTable.MemoFld)

FOR n = 1 TO 100
mnLoc = AT(var2, mcMemoText, n)
IF mnLoc > 0
* --- n th occurance found ---
<do something>
ELSE
* --- n th occurance not found ---
<do something>
ENDIF
ENDFOR

Good Luck,


JRB-Bldr
VisionQuest Consulting
Business Analyst & CIO Consulting Services
CIOServices@yahoo.com
 
Ah , that looks more like it. I didnt realise you could return more than one instance with AT. Thank you.

Thanks,
Jamie
 
Rather than create a substring for each iteration, it may be a little faster (since you have a lot of parsing to do) to make just one substring and count occurenses in that. Less function calls:
Code:
FUNCTION HowMany
STORE AT(var1) TO nStart
STORE 1 TO nCounter
STORE 0 TO nTotal

IF nStart > 0
   STORE SUBSTR(MyTable.Memo1, nStart) TO cString
   DO WHILE AT(var2, nCounter) > 0
      nCounter = nCounter + 1
      nTotal = nTotal + 1
   ENDDO
ELSE
   WAIT WINDOW 'var1 not found'   &&... or whatever
   RETURN
ENDIF
RETURN nTotal


-Dave S.-
[cheers]
Even more Fox stuff at:
 
In the previous post the line:
DO WHILE AT(var2, nCounter) > 0

should be:
DO WHILE AT(var2, cString, nCounter) > 0

I know you know, but just in case.

I like JRB-Bldr's and Dave's posts more than mine, it is good to learn better ways to do the same thing.
 
mlv1055,
Thanks for pointing that out. I just typed away and didn't do any testing. But, there's another bug.
Code:
   WAIT WINDOW 'var1 not found'   &&... or whatever
   RETURN
-should have been-
   WAIT WINDOW 'var1 not found'   &&... or whatever
   RETURN
nTotal

Here is a corrected version:

Code:
FUNCTION HowMany
STORE AT(var1) TO nStart
STORE 1 TO nCounter
STORE 0 TO nTotal

IF nStart > 0
   STORE SUBSTR(MyTable.Memo1, nStart) TO cString
   DO WHILE AT(var2, cString, nCounter) > 0
      nCounter = nCounter + 1
      nTotal = nTotal + 1
   ENDDO
   *... The ELSE portion can be eliminated if you don't 
   *... want any message displayed
ELSE
   WAIT WINDOW 'var1 not found'   &&... or whatever
   RETURN nTotal 
ENDIF
RETURN nTotal



-Dave S.-
[cheers]
Even more Fox stuff at:
 
Dave,

While we are at it, there is another bug in line two, and if we consider nTotal is always equal to nCounter minus one, then it could be a little faster like:

FUNCTION HowMany
STORE AT(var1, cString) TO nStart
STORE 1 TO nCounter

IF nStart > 0
STORE SUBSTR(MyTable.Memo1, nStart) TO cString
DO WHILE AT(var2, cString, nCounter) > 0
nCounter = nCounter + 1
ENDDO
ENDIF
RETURN nCounter - 1

Something I like about this forum is that I have not asked any question, yet I have learned a lot.
 
Well, we're both wrong. At that point cString hasn't been initialized. We need to look for var1 in the memo field:

STORE AT(var1, MyTable.Memo) TO nStart

But I do agree that having 1 counter may be a little faster.


-Dave S.-
[cheers]
Even more Fox stuff at:
 
Oops![blush]
Code:
FUNCTION HowMany
STORE AT(var1, MyTable.Memo1) TO nStart
STORE 1 TO nCounter
IF nStart > 0
   STORE SUBSTR(MyTable.Memo1, nStart) TO cString
   DO WHILE AT(var2, cString, nCounter) > 0
      nCounter = nCounter + 1
   ENDDO
ENDIF
RETURN nCounter - 1
I tested and it's ok now.
Happy Thanksgiving to everyone!
 
Hi virtualranger ,

Try this ONE LINE CODE
? occu(var2,subs(MyTable.Memo1,at(var1,MyTable.Memo1)))

Example..

sele details as Memo1 from \fpd26\foxhelp into cursor Mytable where recn()=16
var1='Most of the configuration'
var2='Configuring'
? occu(var2,subs(MyTable.Memo1,at(var1,MyTable.Memo1)))
 
Wow. Thanks for all the input guys. I'll let you know how I get on.


Thanks,
Jamie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top