unexpected syntax error when defining a global variable?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

unexpected syntax error when defining a global variable?

nrjank
simple example:

-------------------
>> segments = 5;
>> p_i(1:segments+1)= 0
p_i =

   0   0   0   0   0   0

BUT

>> segments = 5;
>> global p_i(1:segments+1)= 0
parse error:
  syntax error
>>> global p_i(1:segments+1)= 0
                             ^
-----------------------------------
if the characters spacing is messed up, it's pointing to the (

Is that supposed to happen? I didn't think a global designation was supposed to change anything with syntax. bug?

Nick J.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: unexpected syntax error when defining a global variable?

jbect
Le 21/04/2015 18:43, Nicholas Jankowski a écrit :
simple example:

-------------------
>> segments = 5;
>> p_i(1:segments+1)= 0
p_i =

   0   0   0   0   0   0

BUT

>> segments = 5;
>> global p_i(1:segments+1)= 0
parse error:
  syntax error
>>> global p_i(1:segments+1)= 0
                             ^
-----------------------------------
if the characters spacing is messed up, it's pointing to the (

Is that supposed to happen? I didn't think a global designation was supposed to change anything with syntax. bug?

Nick J.

I don't think you can declare a global and use indexing on it at the same time. Try in two steps:

global p_i
p_i(1:segments+1)= 0;

or

global p_i
p_i = zeros (1, segments + 1);

https://www.gnu.org/software/octave/doc/interpreter/Global-Variables.html#Global-Variables


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: unexpected syntax error when defining a global variable?

nrjank
ok, thanks. I saw the help said you could declare global and assign in a single command, but it does state "A global variable may only be initialized once in a global statement..."  as the only limitation. I guess it's related.

FYI - just tried on a friend's PC in ML and got a similar error (Error: Unbalanced or unexpected parenthesis or bracket.) so that behavior is compatible, but also noticed that ML doesn't allow you to do simple assignment at all.

ML (R2014b):
global p_i = 5
Error: The expression to the left of the equals sign is not a valid target for an assignment.

Octave (3.8.2)
>> global p_i=5
>> p_i
p_i =  5




On Tue, Apr 21, 2015 at 12:51 PM, Julien Bect <[hidden email]> wrote:
Le 21/04/2015 18:43, Nicholas Jankowski a écrit :
simple example:

-------------------
>> segments = 5;
>> p_i(1:segments+1)= 0
p_i =

   0   0   0   0   0   0

BUT

>> segments = 5;
>> global p_i(1:segments+1)= 0
parse error:
  syntax error
>>> global p_i(1:segments+1)= 0
                             ^
-----------------------------------
if the characters spacing is messed up, it's pointing to the (

Is that supposed to happen? I didn't think a global designation was supposed to change anything with syntax. bug?

Nick J.

I don't think you can declare a global and use indexing on it at the same time. Try in two steps:

global p_i
p_i(1:segments+1)= 0;

or

global p_i
p_i = zeros (1, segments + 1);

https://www.gnu.org/software/octave/doc/interpreter/Global-Variables.html#Global-Variables


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave



_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave