Re: Flattening Nested Lists
Re: Flattening Nested Lists
- Subject: Re: Flattening Nested Lists
- From: Paul Berkowitz <email@hidden>
- Date: Mon, 26 Jan 2009 09:24:08 -0800
- Thread-topic: Flattening Nested Lists
If all your sub-items are strings, there's a much easier and faster way to
do this, that just flattens the whole thing in one fell swoop with no
recursion at all:
set aList to {{"item 1", "item 2", {"item 1", "item 2", "item 3"}}, {"item
1", "item 2", "item 3"}, "item 3"}
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"????"}
set aString to aList as text
set aList to text items of aString
set AppleScript's text item delimiters to oldDelims
aList
--> {"item 1", "item 2", "item 1", "item 2", "item 3", "item 1", "item 2",
"item 3", "item 3"}
If you think any of your strings might contain "????", pick your tids
accordingly. With Unicode options in AS 2.0, you can choose something very
weird that couldn't possibly run into trouble.
--
Paul Berkowitz
> From: "Michael Schmidt (BRT)" <email@hidden>
> Date: Mon, 26 Jan 2009 13:30:15 +0100
> To: AppleScript-Users <email@hidden>
> Subject: Re: Flattening Nested Lists
>
> @Brian Christmas:
> But that does not work on a list like
> set aList to {{"item 1", 5, {"item 1", "item 2", "item 3"}}, {"item
> 1", "item 2", "item 3"}, "item 3"}
> because everything is coerced to text.
>
> If you have a list of strings only, you can also use this:
>
> on flattenList(theList)
> set retour to {}
> if theList {} then
> set AppleScript's text item delimiters to "\", \""
> set retour to run script "{\"" & theList & "\"}"
> set AppleScript's text item delimiters to ""
> end if
> return retour
> end flattenList
>
> Greetings from Hamburg,
> Michael Schmidt
>
> Am 26.01.2009 um 12:13 schrieb Brian Christmas:
>
>>
>> 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
>> _______________________________________________
>> Do not post admin requests to the list. They will be ignored.
>> AppleScript-Users mailing list (applescript-
>> email@hidden)
>> Help/Unsubscribe/Update your Subscription:
>> email@hidden
>> Archives: http://lists.apple.com/archives/applescript-users
>>
>> This email sent to email@hidden
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> AppleScript-Users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
> Archives: http://lists.apple.com/archives/applescript-users
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden