Plucking an Array. Why is a random letter removed rather than the requested letter?

Please specify version and format if asking for help, or apply optional tags above:
Twine Version: 2
Story Format: Sugarcube 2.34.1

==code==
<<set $alphabet to []>>
<<set $alphabet.push(“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j”, “k”, “l”, “m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”, “w”, “x”, “y”, “z”)>>

Alphabet length is $alphabet.length
This is the alphabet: $alphabet

`<<set $letter to prompt(“Which letter do you wish to remove?”)>>

<<if $alphabet.contains($letter)>>
Alright, we’ll remove $letter.
<<set $index to $alphabet.indexOf($letter)>>
<<if $index > 0>>
Alphabet length before plucking: $alphabet.length
Alphabet before plucking: $alphabet
<<set $alphabet.pluck($index, $index + 1)>>
Alphabet after plucking: $alphabet
Alphabet length after plucking: $alphabet.length
<</if>>
<<else>>
Sorry, that letter, $letter, has already been removed.
<</if>>

==endcode==

Question 1: If I request that code to remove the letter “c”, it removes a letter, but not “c”, and not the same letter twice running? Why is that?

Question 2: What do the two numbers in the pluck method do?
<<set $alphabet.pluck($index, $index + 1)>>
I set them to 3,5 thinking I would remove d, e, f, g, and h, or maybe just d and e, or d, e, and f, but only one letter is removed.

Question 3: Is it possible to indent code in this forum?

Both your questions are answered in the documentation:

https://www.motoslave.net/sugarcube/2/docs/#methods-array-prototype-method-pluck

It seems you’ve misunderstood what pluck does; it returns a random element.

To remove an element you can use remove instead:

https://www.motoslave.net/sugarcube/2/docs/#methods-array-prototype-method-delete

You don’t need to use the index like you do there, but if you want/need to you can use deleteAt instead.

https://www.motoslave.net/sugarcube/2/docs/#methods-array-prototype-method-deleteat

1 Like

Thanks, Tobias.

The documentation is still nearly opaque to me, so I was working from a Vegetarian Zombie Youtube tutorial in which he uses the pluck method to remove specific items from an array.

But it didn’t work for me, and you’ve explained why. Thanks.

The code you’re using would have worked in SugarCube v1, but the version of the .pluck() method in SugarCube v2 doesn’t take any arguments.

1 Like

Thanks. I was working from a Youtube video that must have been using version 1.