0 votes
by (160 points)

I made a mechanism that if three slots aren't full other Items in a queue get pushed.

The code looks like this:

<<nobr>>
	<<set $slots = ["Fish"]>>
	<<set $items = ["Bread", "Salad", "Potatos", "Water", "Juice"]>>
	<<if $slots.length < 3>>
		<<for $slots.length < 3>>
			<<set _item = $items[0]>>
			Item: <<print _item>><br>

			<<set $items.pluck(0, 1)>>
			Items: <<print $items>><br>

			<<set $slots.push(_item)>>
			Slots: <<print $slots>><br>
		<</for>>
	<</if>>
<</nobr>>
\What do you want to buy?

Right now nothing bad, but when I run it, it looks like this:

Item: Bread
Items: Salad, Potatos, Water, Juice
Slots: Fish, Bread
Item: Potatos
Items: Salad, Water, Juice
Slots: Fish, Bread, Potatos
What do you want to buy?

The next time it looks completely different.

Is there a problem with the 'pluck' ? Something different I don't know or It's a bug.

1 Answer

0 votes
by (63.1k points)

That's not how pluck works / what pluck does. For one thing, to my knowledge. It doesn't accept arguments. It returns and removes one random element from the array. You seem to want delete. 

Everything in your <<for>> loop can be reduced to something like this: 

Random: 
<<run $slots.push($items.pluck())>>

Not random: 
<<run $slots.push($items.deleteAt(0)[0])>>

 

by (68.6k points)
edited by

@Chapel: The <Array>.pluck() method used to take optional arguments to specify the used range within the array, but they've been deprecated for a bit now.  That said, the OP is using them incorrectly, which is I assume the cause of their confusion.

The following code:

$items.pluck(0, 1)

Is equivalent to:

$items.slice(0, 2).pluck()

In other words, the OP is plucking from the first two members of the array, rather than only the first, which is what I assume they wanted.

 

@Darxoon: As noted by Chapel, there are better ways to accomplish your goal.  In addition to the one given by Chapel, here's another "not random" example, which moves the first element of $items to $slots:

<<run $slots.push($items.shift())>>

 

by (159k points)

@TheMadExile: Did you mean to use the unshift() function in your example (which returns a length) or the shift() function? (which returns an element)

by (68.6k points)

Whoops.  That example was supposed to use the <Array>.shift() method, yes.

by (160 points)
thanks. that really helps
...