I am still learning the ropes of AppleScript and cannot wrap my head around how to accomplish what I am trying to do with AppleScript. I have a list of two items name and gender (this is for demo purposes only). I would like the output to be something along the lines of:
If anyone knows of any examples online, it would be most appreciated. Below is the code I currently have:
set x to "Michael Boy
Eric Boy
Rebecca Girl
Stacey Girl
Pat Unknown
Jesse
Candy Girl"
-- This will just seperate the text into a tab delimited list
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
set mylist to {}
set myparagraphs to every paragraph of x
repeat with eachparagraph in myparagraphs
set mysublist to every text item of eachparagraph as list
set end of mylist to mysublist
end repeat
set AppleScript's text item delimiters to tid
-- Removes any blank values
set | List No Spaces | to {}
repeat with i from 1 to count of every item in mylist
set _name to ""
set _gender to ""
set _name to item 1 of (item i of mylist)
set _gender to item 2 of (item i of mylist)
if _gender is equal to "" then
-- DO NOTHING
else
set end of | List No Spaces | to {_name, _gender}
end if
end repeat
There are multiple ways to skin a cat, but in this example I actually generated 3 lists boys, girls, unknowns. If you really want it formated as you specified it’s doable, but this would seem to have more applications IMO.
set people to "Michael Boy
Eric Boy
Rebecca Girl
Stacey Girl
Pat Unknown
Candy Girl"
set {tid, text item delimiters, boys, girls, unknowns} to {text item delimiters, space, {}, {}, {}}
repeat with aPerson in paragraphs of people
set {theName, gender} to {text item 1, text item -1} of aPerson
if gender = "Boy" then
set end of boys to theName
else if gender = "girl" then
set end of girls to theName
else
set end of unknowns to theName
end if
end repeat
set text item delimiters to tid
Hi,
Thanks for the quick reply, I may not have explained what I was trying to do well enough. This script is going to run though thousands of common variables (not just boy, girl, unknown) to look for matches. So I cannot really specify a specific string variable match as in your example. The SQL equivalent of what I am trying to do would be a join between two tables based on a commonly shared variable.
Please forgive me if I don’t see how to achieve this with the code you have supplied me.
Hi Craig,
Below is a portion of the data I will be using (it is tab delimited):
ATM atm
ArA aurora
ArB aurora
ArC aurora
GRK2 grk
GRK3 grk
BCK1 bck
BCK2 bck
BLK blk
This is only 10 lines of 1200, but you can get the idea. The above left side is for products, the right side is for keywords. I would need to combine like so:
atm ATM
aurora ArA, ArB, ArC
grk GRK2, GRK3
bck BCK1, BCK2
blk BLK
set theData to paragraphs of (read (choose file))
set keyList to {}
set valueList to {}
repeat with aRecord in theData
set {theValue, theKey} to {word 1, word 2} of aRecord
if keyList does not contain theKey then
set end of keyList to theKey
set end of valueList to {theValue}
else
repeat with i from 1 to count keyList
if item i of keyList = theKey then
set end of item i of valueList to theValue
end if
end repeat
end if
end repeat
So you basically end up with two value lists keyList and valueList. Each index of keyList corresponds to a index in valueList which itself if a list of items that had that key.
Here is some sample data:
Michael Boy
Eric Boy
Rebecca Girl
Stacey Girl
Pat Unknown
Candy Girl
Honda Car
Advil Medicine
aspirin Medicine
Thanks, that is just what I needed. I modified your code slightly to product the results that I wanted, but thanks a lot, I really appreciate it. Below is the simply addition that I made to group the values together:
set keyList to {}
set valueList to {}
set comboList to {}
repeat with aRecord in theData
set {theValue, theKey} to {word 1, word 2} of aRecord
if keyList does not contain theKey then
set end of keyList to theKey
set end of valueList to {theValue}
else
repeat with i from 1 to count keyList
if item i of keyList = theKey then
set end of item i of valueList to theValue
end if
end repeat
end if
end repeat
repeat with i from 1 to count keyList
set end of comboList to {item i of keyList, item i of valueList}
end repeat
No problem, I just did the same thing And preserved the coma you need ( or at least listed )
set theData to paragraphs of (read (choose file))
set {keyList, valueList, tid, text item delimiters} to {{}, {}, text item delimiters, tab}
repeat with aRecord in theData
set {theValue, theKey} to {text item 1, text item 2} of aRecord
if keyList does not contain theKey then
set end of keyList to theKey
set end of valueList to {theValue}
else
repeat with i from 1 to count keyList
if item i of keyList = theKey then
set end of item i of valueList to theValue
end if
end repeat
end if
end repeat
-- Combine the lists
set text item delimiters to ", "
set masterList to {}
repeat with i from 1 to count keyList
set end of masterList to item i of keyList & space & (item i of valueList as string)
end repeat
set text item delimiters to tid