Re: deleting variables

From: David Ian Brown <dbrown_at_nyahnyahspammersnyahnyah>
Date: Fri, 2 Mar 2007 12:19:11 -0700

Mateus and Dennis,

Last things first: the "then" in if-then-[else]-end if constructions
is indeed optional "syntactic sugar" (and it is an undocumented feature
of NCL).
We should probably update the documentation.

Regarding the possibility of a procedure that could delete multiple
variables,
presumably passed in as some kind of list.
Consider the following:

a = 1
b = a
delete(b)

Since NCL, like many languages, maintains reference counts, deleting 'b'
does not get rid of 'a', and I think everyone would agree that this is
the proper
behavior. It is somewhat (though not exactly) the same situation if
you pass a
reference to a variable in some kind of parameter list. Anyway NCL
doesn't
like it if you have a procedure such as:

delete_var(a)
begin
        delete (a)
end

What you could do, given NCL's existing state, is have a procedure
without
parameters that deletes variables defined at a higher scope. If you
typically use
a certain set of temporary variable names, you could, for instance,
define a
procedure like this:

procedure delete_vars()
begin
        if (isvar("temp_var_1")) then
                delete(temp_var_1)
          if (isvar("temp_var_2")) then
                delete(temp_var_2)
          ;etc.
end

do while (.not. done)
         temp_var_1 = <some array>
         temp_var_2 = <another array>
         <do stuff>
         delete_vars()
end do

This is not really satisfying however. In order to implement something
that would
be general, we would need to implement something else. Options include:

Implementing a new built-in procedure
procedure delete_vars(varnames : string)

Implementing a function (and I don't know how hard this would be) that
returns a reference to a variable given its name without incrementing
the
reference count:

var = get_primary_ref(varname : string)

Implementing new syntax similar to the string ref syntax for file
variables that
would work for regular variables. Probably harder.
Then you could write, e.g.:
  delete($varname$) where varname is a string

With either of these last 2 options users could write their own
delete_vars type
procedures.

But probably the simplest thing for us to do would be a new built-in
proc.

  -dave

On Mar 2, 2007, at 10:07 AM, Dennis Shea wrote:

>> In some scripts I have to delete many variables within DO loops. I
>> found
>> only one procedure that delete variables and I was thinking in make a
>> procedure that delete a group of variables specified by a list. But,
>> I'm
>> not sure if I can access variables of the main program with a function
>> and delete them. Is it possible?
>
> I am sure there is an NCL "rule" that states
> that variables not local to a function or
> procedure can not be deleted within a called procedure/function.
>
> In the past, I have asked for the "delete"
> procedure to be expanded
>
> a =
> b =
> c =
>
> delete(a)
> delete(b)
> delete(c)
>
> to
> delete(a, b, c)
>
>
> I was told this was not possible. :-(
>
> A delete that could operate on multiple variables
> would lead to cleaner codes.
>
> Note: variables locally created within procedures or functions
> are automatically deleted by NCL. The user need not
> explicitly delete them. This is a nice feature.
>
>
>>
>> By the way: in the NCL reference guide (online), in the part that talk
>> about IF statement the syntax provided is:
>>
>> if(/scalar_logical_expression/) then
>>
>> / statement list /
>>
>> end if
>> if(/ scalar_logical_expression/) then
>>
>> / statement list /
>>
>> else
>>
>> / statement list /
>>
>> end if
>>
>> Seeing carefully one of my scripts I verify that my IF statement
>> hadn't
>> the THEN word, but the scripts still works. So, is the reference
>> manual
>> wrong don't showing that THEN is optional? Or is there a malfunction
>> in
>> IF statement?
>> _______________________________________________
>
>
> A developer will have to answer this one.
> I think the "then" is optional but
> the "end if" is required to complete the "block"
>
> fortran: if (s.eq.5) pi = 4*atan(1) ! no need for an "end if"
>
> NCL: if (s.eq.5) then
> pi = ..
> end if
> also
> if (s.eq.5) pi = 4*atan(1)
> end if ; <=== required
>
> if (isvar("pi")) then ; just in case s.ne.5 :-)
> print(pi)
> end if
>
>
> _______________________________________________
> ncl-talk mailing list
> ncl-talk_at_ucar.edu
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk

_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Fri Mar 02 2007 - 12:19:11 MST

This archive was generated by hypermail 2.2.0 : Mon Mar 05 2007 - 09:27:29 MST