Cross-Language Gotcha: split() with a Limit

I have always assumed that the split() and join() methods of JavaScript’s String and Array objects were inspired by Perl. It’s a striking resemblance if my assumption is wrong.

Briefly the split() function splits a string on a delimiter producing an array of substrings. join() is a complementary function. join() operates on an array and produces a string by concatenating all the elements of the array with the specified delimiter.

In Perl, split() looks something like:
      split separator, stringExpr, limit

And in JavaScript:
      stringObj.split(separator, limit)

In both languages separator can be a literal string or a regular expression. Languages vary in their support of regular expressions so it’s no surprise that Perl and JavaScript interpret certain separators differently.

The gothca that tripped me up is that JavaScript and Perl don’t agree on what limit means. In both languages limit is optional but when specified split() will produce an array with only limit number of elements.

This Perl code:

my $str = "apple:boat:cat";
my @vec = split(':', $str, 2);

will produce an array with the elements “apple” and “boat:cat”.

This JavaScript code:

var str = "apple:boat:cat";
var vec = str.split(':', 2);

will produce an array with the elements “apple” and “boat”.

Conceptually JavaScript splits the whole string but only returns the first limit number of substrings while Perl returns the first limit - 1 substrings and the ‘unsplit’ balance of the string.

The JavaScript behavior can be imitated in Perl something like this:

my $str = "apple:boat:cat";
my @vec;
($vec[0], $vec[1]) = split(':', $str);

There is no equivalent for the Perl behavior in JavaScript.