一半君的总结纸

听话只听一半君

#10 How do I declare a matrix whose size is determined at run-time?

MEL does not allow you to declare a matrix using dynamic variables for its size. The following does not work:

int $a = 4;
int $b = 4;
matrix $m[$a][$b];
// Error: Line 1.12: Syntax error //

To work around this, generate a string command for the declaration and use the ‘eval’ command to execute it:

int $a = 4;
int $b = 4;
string $declare = ( "matrix $m[" + $a + "][" + $b + "];" );
eval $declare;
print $m;
<< 0, 0, 0, 0;
   0, 0, 0, 0;
   0, 0, 0, 0;
   0, 0, 0, 0 >>

Note: I’ve had a number of e-mails reporting that this example does not work for them. It seems that, while the example does work when run from the Script Editor, it reports an error when run from a procedure:

// Error: "$m" is an undeclared variable.

I have not determined a solution for this yet. Sorry about that.

So far, the best work-around I’ve found is to staticly declare your matrix for the largest size you think is required, and using only the sub-matrix from this as needed. Provide an assertion in your script so the desired size does not exceed the declared size.

Friday, September 29, 2000

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: