Difference between revisions of "Error Messages/40616"

(Corrected misleading text and added a workaround to Remedies)
Line 9: Line 9:
 
This example triggers this error. Here BadDef tries to assign to a slice of the global variable X:
 
This example triggers this error. Here BadDef tries to assign to a slice of the global variable X:
  
<code>Index I := 1..9
+
Index I := 1..9
 +
Variable X := Table(I)(7, 8, 3, 2, 5, 4, 9, 1, 5)
 +
Variable BadDef := (X[I = 5] := 99)
  
Variable X := Table(I)(7, 8, 3, 2, 5, 4, 9, 1, 5)
 
  
Variable BadDef := (X[I = 5] := 99)
+
= Remedies  =
</code>
 
  
= Remedies  =
+
While you may not normally assign to a global variable, either directly or to a slice of the variable, in the definition of a variable, you may assign to a global variable (but not to a slice) in a Button script or in a Function called from a Button script [[Scripting|Scripts and Buttons]]. The latter does not violate the No-side effects principle because by clicking a button the user causes the effect directly.
 +
 
 +
You can often work around the prohibition against slice assignment by writing a function, F, that
 +
# copies the global array, X, into a local variable, local_x,
 +
# assigns to a slice of the array, local_x, and then
 +
# assigns the value of local_x back to the global, X,
 +
# and finally, cal function F from a Button script.
  
You may assign to a global variable in a Button script or in a Function called from a Button script [[Scripting|Scripts and Buttons]], directly or indirectly. The rationale is that this does not violate the No-side effects principle because clicking a button means the user is causing the effect directly.
+
For example, suppose you want to assign 999 to the 5th element over index I of global variable X:
 +
 +
  Function Assign_to_X(v)
 +
    Definition:
 +
        Variable local_x := X;
 +
        local_x[I=5] := v;
 +
        X := local_x
  
You can often work around this issue by copying the global array into a local, assigning to a slice of the array, and then assigning the local back to the global within a function called from a Button script. For example, suppose you want to assign 999 to the 10th element over I of global variable X.  If you define a variable
+
   Button Do_Assign
  Variable TrytoAssignX := X[I=10] := 999
+
    Script:  Assign_to_X(999)
it will give the error above.
 
So instead define
 
  Function AssigntoX(v)
 
  Definition: Variable local_X:= X;
 
        local_X[i=10] := v;
 
        X := local_X
 
   Button Assign_to_X
 
  Script:  Assign_to_X(999)
 
  
When you press the button, it will call Assign_to_X(999) and make the assignment.
+
When you press the button, Do_Assign, it calls Assign_to_X(999) and completes the slice assignment.
  
 
<br><comments />
 
<br><comments />

Revision as of 02:45, 17 August 2011

Error Message Examples

You cannot assign to a slice or subscript of a global variable object.

Cause

You are trying to assign a value to a slice (subscripted element) of a global variable. This is not permitted, since it would contravene Analytica's general principle of no side-effects. You may assign to a slice of a local variable.

This example triggers this error. Here BadDef tries to assign to a slice of the global variable X:

Index I := 1..9
Variable X := Table(I)(7, 8, 3, 2, 5, 4, 9, 1, 5)
Variable BadDef := (X[I = 5] := 99)


Remedies

While you may not normally assign to a global variable, either directly or to a slice of the variable, in the definition of a variable, you may assign to a global variable (but not to a slice) in a Button script or in a Function called from a Button script Scripts and Buttons. The latter does not violate the No-side effects principle because by clicking a button the user causes the effect directly.

You can often work around the prohibition against slice assignment by writing a function, F, that

  1. copies the global array, X, into a local variable, local_x,
  2. assigns to a slice of the array, local_x, and then
  3. assigns the value of local_x back to the global, X,
  4. and finally, cal function F from a Button script.

For example, suppose you want to assign 999 to the 5th element over index I of global variable X:

  Function Assign_to_X(v)
    Definition: 
       Variable local_x := X;
       local_x[I=5] := v;
       X := local_x
  Button Do_Assign
    Script:  Assign_to_X(999)

When you press the button, Do_Assign, it calls Assign_to_X(999) and completes the slice assignment.



You are not allowed to post comments.

Comments
<comments />