How to insert an int at a specific index in a list ?

george.profenza's icon

Title pretty much says it all.
Say I have a list of 10 zeroes and I want to add a one at the 2nd position,
how would I do that ?

Also, any recommendations on a elegant way to generate a list of zeros ?

Thank you,
George

Chris Muir's icon
Max Patch
Copy patch and select New From Clipboard in Max.

If the number of places is known, why not just have a message box with that many zeros in it? At any rate, this is a completely non-general solution to the stated problem:

$Adam's icon

Hi,

is this what you mean?

Max Patch
Copy patch and select New From Clipboard in Max.

Cheers,
Ádám

P.S. My solution assumes that you work with 0-based list counting, so the '2nd element' in this example is the third item of the list.

ahmet kizilay's icon

I think you could work with a jit.matrix object. That would be the easiest way to manipulate a list of numbers.
Another way of creating a list of zeros is to use zl group with uzi sending 0 as a message.

Fora's icon
Max Patch
Copy patch and select New From Clipboard in Max.

A slice-less version :)

Richard Garrett's icon

You can also use this feature of [zl nth] -which I like.

Max Patch
Copy patch and select New From Clipboard in Max.

Richard

george.profenza's icon

Wow, thanks a lot guys! This is very cool!