Best practices for creating and comparing lists of text?
Best practices for creating and comparing lists of text?
- Subject: Best practices for creating and comparing lists of text?
- From: Brian Arnold <email@hidden>
- Date: Thu, 15 Dec 2005 11:47:57 -0500
- Thread-topic: Best practices for creating and comparing lists of text?
Title: Best practices for creating and comparing lists of text?
Hi,
I am working on two AppleScript scripting problems with text and lists that seem to be related. They also seem general enough that other people probably have invented the wheel already, and could suggest Best Practices for me.
1) Extract text from a file whose format is like this:
# List of module names (and library names if they differ)
Modulename1
Modulename2=Libname2
Modulename3
Modulename4=Libname4
And convert the text to AppleScript lists that look something like this:
Modulenames = {"Modulename1","Modulename2","Modulename3","Modulename4"}
Libnames = {"Modulename1","Libname2","Modulename3","Libname4"}
Also, get the name of every folder for a specified folder (for the second problem).
2) Compare lists of folder names with module names, and identify which are in one list, and not the other (two sets: A not in B, and B not in A), as efficiently as possible. (Insidious detail: some dirnames are concatentations of other dirnames.)
Here is some sample code for both problems. In the example below, srcPath is a string of a Unix-delimited (/) path to a folder, and the function getModulesList opens a file in that folder, reads its entire contents into a text variable, and returns it. In theory, the file will contain names of all of the directories in that folder. The purpose of this script is to detect when either the file or the folders contained in the parent folder have fallen out of sync, so it can repair the problem. I have other problems to solve in a similar fashion, for example, syncrhonizing an Xcode project that contains groups with the same names as the modules (and if you’re following, library targets with the same names as the libraries).
tell application "Finder"
set srcFolders to every folder of folder (srcPath as POSIX file)
set srcNames to name of every folder of folder (srcPath as POSIX file)
end tell
set savedTextItemDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return}
set modules to getModulesList(srcPath) as list
set AppleScript's text item delimiters to savedTextItemDelimiters
set moduleNames to {}
set libraryNames to {}
repeat with i from 1 to the number of items of modules
set currentLine to item i of modules
if first character of currentLine is not "#" then
if currentLine contains "=" then
set AppleScript's text item delimiters to {"="}
set currentLine to currentLine as list
set AppleScript's text item delimiters to savedTextItemDelimiters
set moduleName to item 1 of currentLine-- DOESN’T WORK
set libraryName to item 2 of currentLine-- DOESN’T WORK
set moduleNames to moduleNames & {moduleName}
set libraryNames to libraryNames & {libraryName}
else
set moduleName to currentLine
set moduleNames to moduleNames & {currentLine}
set libraryNames to libraryNames & {currentLine}
end if
-- check if the module has a real directory
if moduleName is not in srcNames then
-- take some corrective action
end if
end repeat
set AppleScript's text item delimiters to savedTextItemDelimiters
-- check that all corresponding directories are listed in the modules list
set missingNames to {}
repeat with i from 1 to the number of items of srcNames
if item i of srcNames is not in moduleNames then
set missingNames to missingNames & {item i of srcNames}
end if
end repeat
if (missingNames is not {}) then
-- take some corrective action
end if
Where it’s failing is the part that extracts the module and library names from the lines of the modules file (inside the if contains “=” part). I don’t know if the rest is working, because of that problem. If you have any suggestions for me to try, or if you can point me to sample code, I would appreciate such pointers and tips. Thank you!
Brian
--
Brian Arnold, Mac Developer
The MathWorks, Inc.
(508) 647-8006
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden