WeBWorK Problems

MathObjects Methods

MathObjects Methods

by Siman Wong -
Number of replies: 1
I have a totally trivial question about the basic syntax for using MathObject  Methods.  Here is an elementary example:  I would like to determine the length of an array based on

https://webwork.maa.org/wiki/Common_MathObject_Methods

(under "length").  But the code

$v = List("(1,2), (3,4), (5,6)");
$len->lenght($v);

generates the error message

Can't call method "length" on an undefined value

I clearly misunderstood how this works.  Your help is most appreciated. 

Somewhat related question:  In the aforementioned wiki, the entry for "length" says that this method is useful for e.g. determining the number of coordinates in a point or vector.  How might I search for examples of ww problems that use this feature?

Thanks!

In reply to Siman Wong

Re: MathObjects Methods

by Danny Glin -
The arrow is used to call a method associated with an object (named by a variable), so when you type "$len->length" you are trying to call the method "length" on the object $len.  Since the variable $len is undefined you get the error you quoted.

What you want to do is call the "length" method on $v, i.e. $v->length evaluates to 3 in your example.  If you are trying to store that in $len, then what you want is
$len = $v->length;

In terms of how to search for examples using such things, there isn't really any mechanism to do this from within WeBWorK.  If you have command line access to your WeBWorK server you could grep (text search) the OPL for a particular string:
cd /opt/webwork/libraries/webwork-open-problem-library/OpenProblemLibrary
grep -r "\->length" *
(You need the backslash in front of the - because - is a special character in bash.)

In many cases you can just search for a word, but because "length" is a common term the results you're looking for will probably get lost in all the other hits.

If you don't have command-line access to your WeBWorK server (or for convenience), you can download a copy of the OPL from Github onto your local machine, and then search it from there.  grep is built-in on Macs and Linux.  There is probably a Windows/Powershell equivalent, but I don't know it.