6

I'm having a problem with splitting strings in Javascript in Max/MSP.

outlet is the Max/MSP version of printf etc.

The string splits weirdly, but it seems to only output both words comma seperated.

function sample_callback(args)  // Callback
{
    var keyword=args;
    var trackname=keyword.toString().split(" ");
    var name = trackname[0]; // trackname[1] outputs nothing.
    outlet(0, name);
}

Any help is greatly received.

Adam
  • 473
  • 1
  • 5
  • 15
  • I suspect that the code calling the callback, sample_callback, isn't binding correctly, but no way to tell w/o that call. Can you include some of the code that calls sample_callback? – mrk Nov 28 '12 at 15:49
  • Please show us how you're calling `sample_callback`, and the definition of `outlet`. BTW: if you're coming from a C/C++ background, bear in mind that JS has more in common with Scheme/Lisp, so treat functions as objects (pass them as arguments/return values at will) – Elias Van Ootegem Nov 28 '12 at 15:51
  • I've realised a mistake but it still doesn't work - I've edited the question. Thanks for you're quick reply so far. – Adam Nov 28 '12 at 15:59
  • 8
    What is the value returned from `keyword.toString()`? – Aaron Kurtzhals Nov 28 '12 at 16:07
  • It returned the words comma separated instead of space. It must have converted it during the toString function. Thank you @Aaron Kurtzhals – Adam Nov 28 '12 at 16:12

2 Answers2

12

Big thanks to Aaron Kurtzhals . Hopefully the upvote in the comment counts towards your rep!

A simple overlooked checking of what the string is helped me out. oops. The working code is now..

function sample_callback(args)  // Callback
{
  var keyword=args.toString();
  var trackname=keyword.split(",");
  var name = trackname[0];
  outlet(0, name);
}

Cheers

Adam
  • 473
  • 1
  • 5
  • 15
1
function sample_callback(args)  // Callback
{
    var keyword=args.toString()`enter code here`;
    var trackname=keyword.toString().split(" ");
    var name = trackname[0]; // trackname[1] outputs nothing.
    outlet(0, name);
}