String Split not working
Asked Answered
L

2

7

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.

Logsdon answered 28/11, 2012 at 15:46 Comment(5)
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?Prae
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)Pisano
I've realised a mistake but it still doesn't work - I've edited the question. Thanks for you're quick reply so far.Logsdon
What is the value returned from keyword.toString()?Greedy
It returned the words comma separated instead of space. It must have converted it during the toString function. Thank you @Aaron KurtzhalsLogsdon
L
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

Logsdon answered 28/11, 2012 at 16:14 Comment(0)
D
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);
}
Dawndawna answered 14/9, 2017 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.