Adding a number argument to ListToArray

Over in Javascript land, they are looking at this option

## python

print('a|b|c|d|e|f'.split('|', 2))

# ['a', 'b', 'c|d|e|f']

Currently, ListToArray() processes the whole string, regardless

We could add a fifth parameter, (name?) specifying number of elements.

  • when positive, the remaining string gets added as the last array element
  • when negative, nothing more happens, only the first N items get added to the array

This would be useful in terms of performance and memory, as well as being useful when processing complex strings

3 Likes

I actually can see some benefits with that
+1

1 Like

@bennadel said on twitter

so I think adding this extra param to ListFirst and ListRest makes sense too

1 Like

Just to add some color to what I mean, I’ll often attempt to split names like:

name = "John Smith Jr.";
firstName = name.listFirst( " " ); // John
lastName = name.listRest( " " ); // Smith Jr.

So, if I could do a split with a count:

name = "John Smith Jr.";
parts = name.listToArray( ... parts = 2 ... ) ; // [ "John", "Smith Jr." ]
firstName = parts[ 1 ];
lastName = parts[ 2 ];

Ironically, this is more work :laughing: but, I just wanted to add color to my tweet.

3 Likes

I pinged the Adobe team about this proposal

3 Likes

https://luceeserver.atlassian.net/browse/LDEV-4083

3 Likes

Bear in mind that this is already doable in CFML:

'a|b|c|d|e|f'.split("\|", 2) // ["a", "b|c|d|e|f"]

The downside of this is that the resultant array is a Java String[], not a native CFML one (so: can’t call CFML array methods on it; need to use the function version instead: arrayLen(a), not a.len(), etc).

There’s some merit in having a native CFML treatment to remove that “downside” I guess, but I would say given it’s already pretty much doable without taking the time to add anything into the language, I question the merit of adding a fifth parameter to listToArray.

Also not completely convinced this is a “list” operation; it’s a string-splitting operation. There’s a slight notional difference there, perhaps.

Not sure I would prioritise adding this to CFML. And (you know me…) def not unless Adobe added it first & you then had a compat issue to deal with. If I was talking to them about this, I’d be going “really? Is this really something that needs focus on?”. However I’d agree Lucee ought to add it if Adobe did.


Adam