2015-06-05 265 views
0

如何通过此代码缩短时间? TE是包含我的文本的变量(包含超过20000行) 和s是我的搜索词。在文本文件中搜索单词

repeat with x = 0 to the number of lines in TE 
     if line x of TE contains s then 
     put line x-1 of TE & cr & line x of TE & cr & line x+1 of TE & cr & cr after dataarray 
     end if 
    end repeat 

此代码工作正常,但它需要太多的时间。 如何减少时间?

回答

0

尽量避免使用repeat with语法。而是使用repeat for each语法:

put 0 into myLineNr 
repeat for each line myLine in myData 
    // update counter 
    add 1 to myLineNr 
    if myLine contains mySearchString then 
    // store data in new variable 
    put line myLineNr - 1 of myData & cr & myLine & cr & line myLineNr + 1 of myData & cr & cr after myNewData 
    end if 
end repeat 

我已经更改了脚本以允许重复的行。这需要一个计数器,但使用计数器的速度仍然快于使用控制结构。

+2

如果myData中存在重复的行,lineOffset函数将仅查找第一个行,除非使用可选的第三个参数linesToSkip。 – Devin

+1

是的,这是真的德文。当我有一点时我会更新代码。 – Mark

+1

脚本现在已经更新 – Mark

相关问题