# Adding a number argument to ListToArray

**URL:** <https://dev.lucee.org/t/adding-a-number-argument-to-listtoarray/9714>\
**Category:** proposals\
**Created:** [February 22, 2022, 9:42pm UTC](https://dev.lucee.org/t/adding-a-number-argument-to-listtoarray/9714 "2022-02-22T21:42:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zackster](https://dev.lucee.org/user_avatar/dev.lucee.org/zackster/32/3484_2.png) [@Zackster](https://dev.lucee.org/u/Zackster)\
**Post date:** [February 22, 2022, 9:42pm UTC](https://dev.lucee.org/t/adding-a-number-argument-to-listtoarray/9714/1 "2022-02-22T21:42:43Z")

</div>

Over in Javascript land, they are looking at this option

> **[GitHub - tc39/proposal-reversible-string-split: String.prototype.splitn for...](https://github.com/tc39/proposal-reversible-string-split)**
>
> String.prototype.splitn for ECMAScript. Contribute to tc39/proposal-reversible-string-split development by creating an account on GitHub.

```auto
## python

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

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

```

Currently, `ListToArray()` processes the whole string, regardless

> **[ListToArray() :: Lucee Documentation](https://docs.lucee.org/reference/functions/listtoarray.html)**
>
> Copies the elements of a list to an array.

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

---

<div class="post-metadata">

**Author:** ![micstriit](https://dev.lucee.org/user_avatar/dev.lucee.org/micstriit/32/26_2.png) [@micstriit](https://dev.lucee.org/u/micstriit)\
**Post date:** [February 23, 2022, 4:58pm UTC](https://dev.lucee.org/t/adding-a-number-argument-to-listtoarray/9714/2 "2022-02-23T16:58:31Z")

</div>

I actually can see some benefits with that  
+1

---

<div class="post-metadata">

**Author:** ![Zackster](https://dev.lucee.org/user_avatar/dev.lucee.org/zackster/32/3484_2.png) [@Zackster](https://dev.lucee.org/u/Zackster)\
**Post date:** [February 23, 2022, 5:03pm UTC](https://dev.lucee.org/t/adding-a-number-argument-to-listtoarray/9714/3 "2022-02-23T17:03:49Z")

</div>

@bennadel said on twitter

> <https://twitter.com/BenNadel/status/1496461662127374350>

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

---

<div class="post-metadata">

**Author:** ![bennadel](https://dev.lucee.org/user_avatar/dev.lucee.org/bennadel/32/2110_2.png) [@bennadel](https://dev.lucee.org/u/bennadel)\
**Post date:** [February 23, 2022, 6:15pm UTC](https://dev.lucee.org/t/adding-a-number-argument-to-listtoarray/9714/4 "2022-02-23T18:15:20Z")

</div>

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

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

```

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

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

```

Ironically, this is _more_ work 😆 but, I just wanted to add color to my tweet.

---

<div class="post-metadata">

**Author:** ![Zackster](https://dev.lucee.org/user_avatar/dev.lucee.org/zackster/32/3484_2.png) [@Zackster](https://dev.lucee.org/u/Zackster)\
**Post date:** [February 24, 2022, 2:43pm UTC](https://dev.lucee.org/t/adding-a-number-argument-to-listtoarray/9714/5 "2022-02-24T14:43:41Z")

</div>

I pinged the Adobe team about this proposal

> <https://twitter.com/MarkTakata/status/1496588859844730881>

---

<div class="post-metadata">

**Author:** ![Zackster](https://dev.lucee.org/user_avatar/dev.lucee.org/zackster/32/3484_2.png) [@Zackster](https://dev.lucee.org/u/Zackster)\
**Post date:** [July 18, 2022, 9:07am UTC](https://dev.lucee.org/t/adding-a-number-argument-to-listtoarray/9714/6 "2022-07-18T09:07:15Z")

</div>

[https://luceeserver.atlassian.net/browse/LDEV-4083](https://luceeserver.atlassian.net/browse/LDEV-4083)

---

<div class="post-metadata">

**Author:** ![AdamCameron](https://dev.lucee.org/user_avatar/dev.lucee.org/adamcameron/32/4614_2.png) [@AdamCameron](https://dev.lucee.org/u/AdamCameron)\
**Post date:** [July 18, 2022, 2:42pm UTC](https://dev.lucee.org/t/adding-a-number-argument-to-listtoarray/9714/7 "2022-07-18T14:42:16Z")

</div>

Bear in mind that this is already doable in CFML:

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

```

> **[TryCF Gist - 393476489fd8e67bc606b3d64b7f0b23](https://trycf.com/gist/393476489fd8e67bc606b3d64b7f0b23/lucee5?theme=monokai)**
>
> Code snippet: ......

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
