1

I need to count the number of occurance of word in the array from the field "MytextField". The array consist of more than 9000 lines and the text in field "MytextField" consist of more than 10000 lines. I am using the following code and it works fine but need so much time.

put the number of lines of (the keys of myArray) into myArrayL
   repeat with i = 0 to myArrayL
      put  myArray[i] into k 
      split k by colon
      put k[1] into searchStr
      put k[2] into replaceStr
      repeat for each line iword in Tex
         if  iword contains searchStr then
            add 1 to tmp
            put tmp & "                 " & searchStr & cr into sam
         end if
         --delete word iword of Tex
         if  iword contains replaceStr then
            add 1 to tmp1
            put tmp1 & "                 " & replaceStr & cr into sam1
         end if
         --delete word iword of Tex
      end repeat
      put sam after slu
      put 0 into tmp
      put "" into sam
      put sam1 after slu1
      put 0 into tmp1
      put "" into sam1
   end repeat
answer slu1
answer slu

Is it any way to decrease the time consumption? How to change this code with more speed

Zam
  • 367
  • 2
  • 17

2 Answers2

1

Try using offsets, which should be faster than examining every word:

put fld "MytextField" into tText
set the itemdel to colon
repeat for each element e in myArrayL
  put item 1 of e into searchStr
  put 0 into tSkip
  repeat
    get wordOffset(searchStr,tText,tSkip)
    if it = 0 then exit repeat
    add it to tSkip
    add 1 to tCountArray[searchStr]
  end repeat
end repeat

That counts the search words and puts the counts into an array. You can alter it a bit and run it again if you need to count the replacement words too. Don't count the search words and replacement words in the same repeat loop, you'll get incorrect results. I don't know if this will be much faster than your original script because the repeat loop has to run twice if you want both search and replacement word counts.

After the arrays are made, you can use the combine command to quickly show the counts.

Jacque
  • 416
  • 2
  • 7
0

Try the repeat for each element construct instead. It is generally faster in situations like this than repeat with i = x to y forms.

repeat for each element thisElement in myArray
   # your loop logic here
end repeat
Devin
  • 593
  • 1
  • 3
  • 8