I figured you guys may find these following results interesting.
The text file I ran tests against contained only the following 4 lines… Over and over again
[format]Here is a script that will find all instances but it uses a repeat loop.
My test string contained 4096 paragraphs and every fourth paragraph matched the substring.
Your script is ingenious but I wondered how it would fare with a large string.
I am trying to figure out a way to find the paragraph number of a text file that contains 100658.[/format]
Test.txt with 4400 paragraphs and 100658 in every 4th paragraph
MacBookPro15,1, macOS Version 12.5.1 (Build 21G83), 100 iterations
First Run Total Time Average
First 0.014 1.350 0.014 ← AppleScript using “do shell script” - wch1zpink
Second 0.045 4.631 0.046 ← AppleScript using “script objects” - robertfern
Ratio (excluding first run): 1:3.43
Test.txt with 57148 paragraphs and 100658 in every 4th paragraph
MacBookPro15,1, macOS Version 12.5.1 (Build 21G83), 100 iterations
First Run Total Time Average
First 0.119 8.933 0.089 ← AppleScript using “do shell script” - wch1zpink
Second 0.748 67.303 0.673 ← AppleScript using “script objects” - robertfern
Ratio (excluding first run): 1:7.53
It seems to me as if the larger the list, the more inefficient AppleScript using “script objects” becomes.
These were the two scripts I ran the tests on:
set myFile to quoted form of POSIX path of (path to desktop as text) & "Test.txt"
set myID to quoted form of "100658"
set paragraphNumber to do shell script "grep -no " & myID & ¬
" " & myFile & " |sed -E -e 's/" & ":" & myID & "//g'"
set text item delimiters to ", "
log words of paragraphNumber as text
AND
script M
property myCounts : missing value
property textList : missing value
end script
set M's myCounts to {}
set myfile to ((path to desktop as text) & "Test.txt")
set theText to (read file myfile)
set myID to "100658"
set text item delimiters to myID
set M's textList to text items of theText
set mycount to 0
repeat with i from 1 to (count M's textList) - 1
set mycount to mycount + (count (paragraphs of (item i of M's textList)))
set end of M's myCounts to mycount
set mycount to mycount - 1
end repeat
set text item delimiters to ", "
log M's myCounts as text