Re: count of items in a reference to a list
Re: count of items in a reference to a list
- Subject: Re: count of items in a reference to a list
- From: Nigel Garvey <email@hidden>
- Date: Tue, 10 Jul 2001 08:47:01 +0100
John W Baxter wrote on Mon, 9 Jul 2001 11:36:21 -0700:
>
>On 7/9/01 9:48 AM, email@hidden <email@hidden> wrote:
>
>
>
>>set x to {"a", "b", "c"}
>
>>set countx to count of items in x
>
>>log countx
>
>>--> 3
>
>>set xr to a reference to x
>
>>set countxr to count of items in xr
>
>>--> every item of x of<<script>> doesn't understand the count message.
>
>
>
>
set x to {"a", "b", "c"}
>
set countx to count of items in x
>
log countx
>
--> 3
>
set xr to a reference to x
>
set countxr to count of items in contents of xr
This looks like a two-stage reference needing to be resolved before using
it. ('Items in xr' is a reference that includes another reference, which
is passed to 'count'.) Another dereferencing tactic that works is:
set countxr to count (get items in xr)
... or its two-line equivalent. Yet another approach is the little-used:
set countxr to count xr each item
>
will work, as will various forms which don't talk about items
>
count xr
>
count of xr
This is a better approach anyway, as 'item' is the default element to be
counted. In other words, 'count' just counts whatever's there:
count {"a", "b", "c"}
--> 3
count "Hello"
--> 5
Timings suggest that if you specify 'items of' in a count, extra steps
are taken to ensure that only 'items' are counted - which is a waste of
effort.
>
and the left-field way:
>
set countxr to length of xr
I've found this to be *the* best way with references. Getting the
properties of referenced objects is very fast.
NG