Reputation: 17
I'm new to applescript and was wondering whether you see a problem with my below code as it is painfully slow:
set myPath to ((path to desktop as text) & "a.txt") as alias
set myFile to read myPath
set myText to paragraphs of myFile
set rowCounter to 0
repeat with nextline in myText
set rowCounter to rowCounter + 1
end repeat
rowCounter
The textfile i am opening is huge, ie about 1.1m records. The same code in VBA opens it and counts the rows in about 7seconds, it took the better part of 10 minutes with applescript.
Am i doing something wrong??
Thanks, Alex
Upvotes: 0
Views: 967
Reputation: 11238
You can also try something like this:
set myPath to POSIX path of ((path to desktop as text) & "a.txt")
set lineCount to (do shell script "wc -l " & quoted form of myPath & " | sed -E 's/^ *([[:digit:]]+).*/\\1/'") as number
Upvotes: 0
Reputation: 6932
The repeat loop as you have is most likely taking forever to process And is not necessary to just count the items in the list with a repeat loop.
You have already have a item list of all the paragraphs.
So you should be able to just count the number of items in the list.
Set rowcounter to number of items in myText
Or
Set rowcounter to count of myText
Also as an aside.
Your repeat loop is not using the index variable 'nextline'
If you wanted to iterate over each item you would use something like :
Repeat with nextline from 1 to count of myText
Set thisItem to nextline of myText
....
Upvotes: 1