Re: Best practices for creating and comparing lists of text?
Re: Best practices for creating and comparing lists of text?
- Subject: Re: Best practices for creating and comparing lists of text?
- From: kai <email@hidden>
- Date: Fri, 16 Dec 2005 16:07:53 +0000
On 15 Dec 2005, at 16:47, Brian Arnold wrote:
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.
Not too sure about Best Practices, Brian - but I'd probably start
with something like the efforts below:
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"}
-----------------------
to parse_lists from t
tell t starts with "#"
if (count t) is 0 or it and (count t's paragraphs) is 1 then return
{{}, {}}
if it then set t to t's text from paragraph 2 to end
end tell
script o
property m : t's paragraphs
property l : m's items
end script
set n to 1
set tid to text item delimiters
set text item delimiters to "="
tell t's text items to repeat with i from 1 to (count) - 1
set n to n - 1 + (count item i's paragraphs)
tell item n of o's m
set item n of o's m to text item 1
set item n of o's l to text item 2
end tell
end repeat
set text item delimiters to tid
o's {m, l}
end parse_lists
set sourceText to "# List of module names (and library names if they
differ)
Modulename1
Modulename2=Libname2
Modulename3
Modulename4=Libname4"
parse_lists from sourceText returning {Modulenames, Libnames}
--> {{"Modulename1", "Modulename2", "Modulename3", "Modulename4"},
{"Modulename1", "Libname2", "Modulename3", "Libname4"}}
-----------------------
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.)
-----------------------
on missing_strings from l1 against l2
if 0 is in {count l1, count l2} then return {l2, l1}
set m2 to {}
set tid to text item delimiters
set text item delimiters to return
set m1 to return & l2 & return
repeat with i in l1
set text item delimiters to return & i & return
tell m1's text items to if (count) is 1 then
set m2's end to i's contents
else
set AppleScript's text item delimiters to return
set m1 to beginning & ({""} & rest)
end if
end repeat
set text item delimiters to tid
if m1 is return then return {{}, m2}
{m1's paragraphs 2 thru -2, m2}
end missing_strings
set Modulenames to {"name1", "name1name2", "name2", "name2name3",
"name3", "name4", "name6"}
set Foldernames to {"name1", "name1name2", "name3", "name4",
"name4name5", "name5", "name6"}
missing_strings from Modulenames against Foldernames returning
{missingModules, missingFolders}
--> {{"name4name5", "name5"}, {"name2", "name2name3"}}
-----------------------
---
kai
_______________________________________________
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