NEED A FASTER TEXT PARSING SOLUTION
This example script takes entirely too long to process larger text files.
What I need it to do is:
read a text file,
find a string,
then return the first word of its paragraph line.
But my files are 2000 lines long!! in this example I just have a short snippet of the text file working.
Any ideas?
[code]
set findme to "8701"
set findme to SaR(findme, " ", "")
set the_file to (((path to desktop) as string) & "etch_rules.txt")
set rulez to my read from_file(thefile) --- this file is normally 2500 lines
set rulez to "715, 716, 717, 718, 719, 720, 721, 722, 723, 724, 727, 728, 729
736, 744, 745, 749, 742, 99, 864, 865, 661, 664, 660, 663, 662, 665, 659, 782
4484, 4479, 4476
770, 771, 772, 774, 776, 778, 779, 780, 781, 783, 785, 786, 787, 773, 282, 7702
2070, 2875, 2079, 2082, 2084, 2075
1884, 1870, 1875, 1879, 1882, 1884
8700, 8701
791, 795, 792, 796" -----substitute for larger text file
set rulez to SaR(rulez, " ", "")
set cnt to count paragraphs of rulez
repeat with x from 1 to cnt
set this_par to paragraph x of rulez
set cnt2 to count text items of this_par
set isit to the offset of findme in this_par
if isit ≠ 0 then
repeat with y from 1 to cnt2
set this_word to text item y of this_par
if this_word = findme then set findme to text item 1 of this_par
end repeat
end if
end repeat
display dialog findme
on read from_file(thefile)
set the_data to "FILE APPARENTLY DOES NOT EXISTS"
try
set the_file to the_file as file specification
set the_data to read the_file
end try
return the_data
end read fromfile
on SaR(sourceText, findText, replaceText)
set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, findText}
set tempText to text items of sourceText
set AppleScript's text item delimiters to replaceText
set sourceText to tempText as string
set AppleScript's text item delimiters to atid
return sourceText
end SaR
imac, Mac OS X (10.5.8), my computer is fast