• 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: Variable Ponderance
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Variable Ponderance


  • Subject: Re: Variable Ponderance
  • From: Paul Berkowitz <email@hidden>
  • Date: Thu, 30 Sep 2004 10:37:31 -0700

You need to read up on lists and on scope in the AppleScript Language Guide
or, better, in Matt Neuburg's AppleScript: The Definitive Guide. (Matt's
very good on this.)

Lists, records, dates and script objects are "data sharing" - i.e. mutable
in place. Furthermore, top-level variables are global in scope unless
specifically declared local. So unless you declare y to be local in your
handler, or at the top level of the script, or both, it's going to be
global.

--
Paul Berkowitz


> From: Ben Waldie <email@hidden>
> Date: Thu, 30 Sep 2004 13:27:09 -0400
> To: AppleScript User's List <email@hidden>
> Subject: Variable Ponderance
>
> I think I have noticed this before, but just never questioned it.
>
> In the following examples, I set a local variable to a value, and then
> pass that variable down to a handler as a parameter.  In the handler, I
> change the value of this parameter.  I would expect this change to be
> local to the handler only, and not global to the script.  So, at the
> main level of the script, I would expect the variable to retain it's
> original value, no matter what happens to it in the handler.
>
> In the first example, I pass down a variable containing a number,
> completely change it to a value of 2, and the variable at the top level
> of my script behaves as I would expect, retaining its original value.
> In the second and third examples though, I pass down a variable
> containing a list or a record, modify part of the list or record, and
> the variable at the top level of my script does not behave as I would
> expect.  In these 2 cases, the main script treats the variable as it if
> is a global, remembering the change I made in the handler.  This
> appears to occur only when manipulating the variable, and not when
> changing it completely.  Is this the intended behavior?
> --------------------
> EXAMPLE 1
> --------------------
> set x to 1
> doSomething(x)
> return x
> --> 1 -- I would expect 1
>
> on doSomething(y)
> set y to 2
> end doSomethingElse
> --------------------
> EXAMPLE 2
> --------------------
> set x to {1, 2, 3}
> doSomething(x)
> return x
> --> {2,2,3} -- I would expect {1,2,3}
>
> on doSomething(y)
> set item 1 of y to 2
> end doSomething
> --------------------
> EXAMPLE 3
> --------------------
> set x to {name:"Ben"}
> doSomething(x)
> return x
> --> {name:"Waldie"} -- I would expect {name:"Ben"}
>
> on doSomething(y)
> set name of y to "Waldie"
> end doSomething
> --------------------
>
>
> -Ben
>
> Benjamin S. Waldie
> President & CEO
> Automated Workflows, LLC
> 610.935.0652
> =============================================
> AppleScript and Workflow Automation
> Consulting - <http://www.automatedworkflows.com>
> AppleScript Info - <http://www.applescriptguru.com>
> AppleScript Training - <http://www.applescripttraining.com>
> =============================================
>
>  _______________________________________________
> 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
>


 _______________________________________________
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

  • Follow-Ups:
    • Re: Variable Ponderance
      • From: Ben Waldie <email@hidden>
References: 
 >Variable Ponderance (From: Ben Waldie <email@hidden>)

  • Prev by Date: Variable Ponderance
  • Next by Date: Re: Variable Ponderance
  • Previous by thread: Variable Ponderance
  • Next by thread: Re: Variable Ponderance
  • Index(es):
    • Date
    • Thread