如何计算livecode中的出现?

时间:2015-06-02 13:03:54

标签: livecode

我需要从字段" MytextField"中计算数组中单词的出现次数。该数组包含超过9000行和文本字段" MytextField"由超过10000行组成。我使用以下代码,它工作正常,但需要这么多时间。

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

是否可以减少时间消耗? 如何以更快的速度更改此代码

2 个答案:

答案 0 :(得分:1)

尝试使用偏移,这应该比检查每个单词更快:

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

计算搜索词并将计数放入数组中。如果您还需要计算替换单词,可以稍微改变它并再次运行它。不要在同一个重复循环中计算搜索词和替换词,否则会得到不正确的结果。我不知道这是否比原始脚本快得多,因为如果你想要搜索和替换字数,重复循环必须运行两次。

制作数组后,您可以使用combine命令快速显示计数。

答案 1 :(得分:0)

请尝试使用repeat for each element构造。在这种情况下,它通常比repeat with i = x to y形式更快。<​​/ p>

repeat for each element thisElement in myArray
   # your loop logic here
end repeat