Re: Counting the items in a nested list
Re: Counting the items in a nested list
- Subject: Re: Counting the items in a nested list
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 15 May 2001 12:04:41 -0700
On 5/15/01 10:48 AM, "Ehsan Saffari" <email@hidden> wrote:
>
say there is a nested list and we count it's items:
>
>
set x to {{"a", "b"}, {{"c", "d"}, {"e", "f"}}}
>
set cnt to count of items in x --> 2
>
>
is it possible to count the items in x (=3) without a repeat loop?
>
>
hope that's clear
>
Not really clear, no. Why do you say (=3)? At the first level, there are 2
items (lists). At the deepest level, I guess there are 6 strings altogether.
At no (single) level are there 3 items.
You seem to be looking for something really specific - perhaps "the total
number of 2-item lists", or "the total number of (sub)-lists when you go as
deeply as you can, but not as far as individual strings". Anything as
specific as that will have to be carefully defined, and will certainly
involve a repeat loop, especially if you have to reject list items that are
not themselves lists.
If you only want to count the lowest level of list, and to keep going deeper
until you hit that, then you'd need something like the following. Even Akua
Sweets' various list osaxen can't do this without a repeat loop.
global cnt
set x to {{"a", "b"}, {{"c", "d"}, {"e", "f"}}}
set cnt to 0
ExtractSublist(x)
cnt
on ExtractSublist(n)
local sl, z
set sl to 0
repeat with i from 1 to (count n)
set z to item i of n
if class of z is list then
set sl to (sl + 1)
ExtractSublist(z)
end if
end repeat
if sl = 0 then -- no more sublists deeper in
set cnt to (cnt + 1)
end if
end ExtractSublist
--3
--
Paul Berkowitz