• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: lists and items and position of items
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: lists and items and position of items


  • Subject: Re: lists and items and position of items
  • From: Axel Luttgens <email@hidden>
  • Date: Sat, 12 Mar 2005 17:08:01 +0100

kai wrote:


[...]

For the benefit of those not familiar with this issue, Christian's script was comparing strings like "Sal" with the references like: item 1 of {"Sal", "Sue", "Bob", "Carl"}. While we might recognise these as having essentially similar meanings, they differ nevertheless - unless and until the reference is evaluated to an exact match, such as (in this case): "Sal". Christian's 'list-position' handler therefore found zero matches in every comparison.

[...]

Unless I'm wrong, one very precise point hasn't been made explicit throughout the thread.
So, if you allow, I'll bring my 2 cents too...


As a general rule, AppleScript tends to automatically dereference and coerce as needed. [1]

But the operator "is" in fact is very special in the language specification: it first compares the classes of its operands, without derefencing or coercing them.

So, in the following loop, x is always a reference and "Sal" is always a string, hence the four consecutive falses:

   set this_list to {"Sal", "Sue", "Bob", "Carl"}
   repeat with x in this_list
       log x is "Sal"
   end repeat
   --> (*false*)
   --> (*false*)
   --> (*false*)
   --> (*false*)

By contrast, this is an illustration of AppleScript's usual behavior:

   set this_list to {"Sal", "Sue", "Bob", "Carl"}
   repeat with x in this_list
       log x & "Sal"
   end repeat
   --> (*SalSal*)
   --> (*SueSal*)
   --> (*BobSal*)
   --> (*CarlSal*)

In the above, on each pass, operator "&" dereferences x and gets a string to which "Sal" may just be concatenated.

Another example involving automatic dereferencing AND coercion:

   set this_list to {1, 2, 3, 4}
   repeat with x in this_list
       log "Sal" & x
   end repeat
   --> (*Sal1*)
   --> (*Sal2*)
   --> (*Sal3*)
   --> (*Sal4*)

So, yes, the "repeat with ... in ..." indeed is special in that the looping variable is set to consecutive references.
But this mainly leads to surprises when used in conjubction with those very special operators "is" and "is not".


HTH,
Axel

[1] Well... this used to be the general rule. Before the increasing need of those "explicit get".

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Re: lists and items and position of items (From: kai <email@hidden>)

  • Prev by Date: Re: lists and items and position of items
  • Next by Date: Re: Script for the monitor pref pane
  • Previous by thread: Re: lists and items and position of items
  • Next by thread: Re: lists and items and position of items
  • Index(es):
    • Date
    • Thread