Re: Newbie Text Manipulation
Re: Newbie Text Manipulation
- Subject: Re: Newbie Text Manipulation
- From: Christopher Stone <email@hidden>
- Date: Tue, 22 Jun 2004 04:36:25 -0500
At 09:18 +0100 06/22/2004, John Clark wrought:
I have a list of names {"john smith", "dave geek", "freddy kruger"} etc
How can extract the first and second names of each.
This will create a list of first names and another list of last names:
----------------------------------------------------------------------
set l to {"john smith", "dave geek", "freddy kruger"}
set fNames to {}
set lNames to {}
repeat with i from 1 to length of l
set {end of fNames, end of lNames} to {word 1, word 2} of item i of l
end repeat
{fNames, lNames}
----------------------------------------------------------------------
There are a variety of ways to manipulate this sort of data. A couple of
examples:
----------------------------------------------------------------------
set l to {"john smith", "dave geek", "freddy kruger"}
set fname1 to word 1 of item 1 of l
set lname1 to word 2 of item 1 of l
----------------------------------------------------------------------
set l to {"john smith", "dave geek", "freddy kruger"}
tell item 1 of l
set {fname, lname} to words
end tell
----------------------------------------------------------------------
-- Using the Satimage osax
--
http://www.satimage.fr/software/en/downloads_osaxen.html
set l to {"john smith", "dave geek", "freddy kruger"}
set AppleScript's text item delimiters to {return}
set lStr to l as string
set AppleScript's text item delimiters to {""}
try
set fNames to find text "^\\w+" in lStr with regexp, all occurrences
and string result without case sensitive
on error
set fNames to false
end try
try
set lNames to find text "\\w+$" in lStr with regexp, all occurrences
and string result without case sensitive
on error
set lNames to false
end try
----------------------------------------------------------------------
You can also use a record structure, but I generally don't because of its
inflexibility.
set reco to {{fname:"john", lname:"smith"}, {fname:"dave",
lname:"geek"}, {fname:"freddy", lname:"kruger"}}
fname of item 1 of reco
lname of item 1 of reco
tell item 1 of reco
{its fname, its lname}
end tell
----------------------------------------------------------------------
Chris
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.