help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: `clear variable` does not work ?


From: Sergei Steshenko
Subject: Re: `clear variable` does not work ?
Date: Fri, 1 Feb 2008 19:13:54 -0800 (PST)

--- "John W. Eaton" <address@hidden> wrote:

> On 31-Jan-2008, Matthias Brennwald wrote:
> 
> | I can't offer a solution to your problem, but I can add a bit to the
> | confusion. Consider this (with Octave 3.0 on Ubuntu Linux 7.10):
> | 
> | octave:1> global x = [1 2 3]
> | octave:2> clear x
> | octave:3> x
> | error: `x' undefined near line 33 column 1
> | 
> | So, it looks like x has been cleared and does not exist anymore. But, if
> | I continue with the following, this seems not to be the case:
> | 
> | octave:4> global x = [9 9 9]
> | octave:5> x
> | x =
> | 
> |    1   2   7
> | 
> | There are two things that confuse me:
> | 1. Why is x not equal to [9 9 9]?
> | 2. Why does Octave remember the previous value of x, although it has
> | been cleared previously?
> | 
> | I don't believe this is a bug, because it is so fundamental. I guess
> | this has something to do with me (and others) not understanding well how
> | things work. Can someone enlighten me?
> 
> The statement
> 
>   global x
> 
> creates a local variable called X, and, if it does not already
> exist, a variable X in the global namespace.  The local variable X is
> linked to the global variable X.  If you then write
> 
>   clear x
> 
> you are clearing the local variable only.  You need to use
> 
>   clear all
> 
> or
> 
>   clear global x
> 
> to remove X from the global namespace.
> 
> Once a global variable is initialized with a statement like
> 
>   global x = 13
> 
> it can't be initialized again unless it is cleared from the global
> namespace first.
> 
> Does that make it any clearer?
> 
> jwe
> _______________________________________________


Actually, it looks even more complicated now.

What does "The local variable X is linked to the global variable X" exactly 
mean ?

If in a given scope there are two entities named "X", how does one differentiate
between them ?

In Perl, for example, global variables can be disambiguated using package name, 
e.g.

our $x; # global variable, let's assume we're in package "main"
$x = 1;
  {
  my $x; # lexically scoped variable
  $x = 2;
  warn "\$x=$x"; # should print 2
  warn "\$main::x=$main::x"; # should print 1
  }

Thanks,
  Sergei.


Applications From Scratch: http://appsfromscratch.berlios.de/


      
____________________________________________________________________________________
Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs


reply via email to

[Prev in Thread] Current Thread [Next in Thread]