Re: a script with a variable name to load

From: David Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Wed Apr 06 2011 - 15:22:10 MDT

Hi Mark,
As a belated response to this question, I don't think it's correct to say that 'loadscript' cannot
be used to load global variable definitions using a dynamically generated file path.
The only thing that is peculiar about loadscript is that its definitions are not available within
the current begin-end block. After you exit from the current block the definition is available, outside
any block or inside any subsequent block. Here is a script to illustrate. Assume the script "adef.ncl"
contains the single line:

a = 1

then this script:

dir = "./"

begin
   loadscript(dir + "adef.ncl")
   print("a is defined: " + isvar("a"))
end

print("after begin-end block")
print("a is defined: " + isvar("a"))

delete(a)
print("after deleting a")
print("a is defined: " + isvar("a"))

loadscript(dir + "adef.ncl")
begin
   print("in second begin-end block")
   print("a is defined: " + isvar("a"))
end

produces the following output:

 Copyright (C) 1995-2011 - All Rights Reserved
 University Corporation for Atmospheric Research
 NCAR Command Language Version 6.0.0-beta
 The use of this software is governed by a License Agreement.
 See http://www.ncl.ucar.edu/ for more details.
(0) a is defined: False
(0) after begin-end block
(0) a is defined: True
(0) after deleting a
(0) a is defined: False
(0) in second begin-end block
(0) a is defined: True

The last part of the script:
loadscript(dir + "adef.ncl")
begin
   print("in second begin-end block")
   print("a is defined: " + isvar("a"))
end

represents the way we would generally recommend that you use loadscript to define global variables.
Hope this helps.
 -dave

On Apr 1, 2011, at 4:22 PM, mark collier wrote:

> Hi Dennis,
> no I think loadscript will not allow "a" to be seen by the rest of the
> script (ie "a" will not have global extent).
> Regards,
>
> On 02/04/2011, at 2:07 AM, Dennis Shea wrote:
>
>> Is "loadscript" what you need?
>>
>> http://www.ncl.ucar.edu/Document/Functions/Built-in/loadscript.shtml
>>
>> On 4/1/11 12:18 AM, mark collier wrote:
>>> Hi,
>>> often I run code from an external NCL script.
>>>
>>> For example, the external script a.ncl might contain
>>>
>>> a=1.0
>>>
>>> and so in my parent script I can simply go
>>>
>>> begin
>>> load "a.ncl"
>>> print(a)
>>> end
>>>
>>> and so the variable a is effectively global in extent, and it often
>>> helps to keep the parent script compact and the code more clumped by
>>> relevance.
>>>
>>> However, I can't load a script in a variable location, eg
>>>
>>> idir="/home"
>>> load idir+"/a.ncl"
>>> print(a)
>>>
>>> or variations, I get a message like "input in flex scanner failed".
>>> It
>>> works if I put the full path in, however, the aim is to have a
>>> variable path. I think I understand why it is failing, but are
>>> looking
>>> for a work around?
>>>
>>> Regards,
>>> Mark.
>>> _______________________________________________
>>> ncl-talk mailing list
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
> _______________________________________________
> ncl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Wed Apr 6 15:22:16 2011

This archive was generated by hypermail 2.1.8 : Fri Apr 08 2011 - 09:34:26 MDT