On 26/01/2009, at 9:15 PM, Rick Gordon wrote: Thanks, Barry. ------------------ On 1/26/09 at 9:52 AM +0000, Barry Wainwright wrote in a message entitled "Re: Flattening Nested Lists": On 26 Jan 2009, at 09:30, Rick Gordon wrote:
I'm spending too much time trying to do something that I think should be easy enough: flattening a list of lists (of lists (of lists)), so that the the result is a list containing no list elements. Can someone point me to some code or instruction on handling this? I'm figuring that it probably takes a recursive handler, but I'm getting recursed in my own mind, at this point.
set aList to {{"item 1", "item 2", {"item 1", "item 2", "item 3"}}, {"item 1", "item 2", "item 3"}, "item 3"}
set newList to {}
my flattenList(aList)
newList
on flattenList(theList)
global newList
repeat with anItem in theList
if class of anItem is list then
my flattenList(anItem)
else
copy anItem to end of newList
end if
end repeat
end flattenList
Of course, if you want the list flattened as a list of items, instead of an expanded list of lists, just coerce the variable, so...
global newList set aList to {{"item 1", "item 2", {"item 1", "item 2", "item 3"}}, {"item 1", "item 2", "item 3"}, "item 3"} set newList to {} set extendlist to {} my flattenList(aList) newList
on flattenList(theList) repeat with anItem in theList if class of anItem is list then my flattenList(anItem) else set end of newList to anItem as text end if end repeat end flattenList
Regards
Santa |