This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2010-08-08 11:28:44

rogererens
New Scratcher
Registered: 2010-08-08
Posts: 3

How do I remove a specific item from a list?

Say I have a list called 'colours' that is filled randomly with colours resulting in
colours[1] = Blue
colours[2] = Red
colours[3] = Yellow

How do I remove a specific item from the list, e.g. the colour Red, when I don´t know its index?
This run it is item #2, but maybe next run it is item #3, or #8.

Thanks in advance,

Roger.

Offline

 

#2 2010-08-08 11:46:10

Lellowsfuzz
Scratcher
Registered: 2009-04-17
Posts: 500+

Re: How do I remove a specific item from a list?

Use the (delete 1 of [colors]) block. Highlight the 1 and change it to any number.

Offline

 

#3 2010-08-08 12:48:22

hmnwilson
Scratcher
Registered: 2007-07-04
Posts: 1000+

Re: How do I remove a specific item from a list?

Lellowsfuzz wrote:

Use the (delete 1 of [colors]) block. Highlight the 1 and change it to any number.

He means that the position of the color changes and the project needs to be able to find it.

The best way would be create a variable called "listchecker" and use a script like this:

set [listchecker] to (1)
repeat until <(item (listchecker) of [colors])) = [yellow]>
  change [listchecker] by (1)
[end of loop]
delete item (listchecker) of [colors]

This would search the list for an item called "yellow" (you can change that to any color you want), and deletes it after it finds it. Just note that the "repeat until" block is kind of slow by default, so try not to use it when you need it to work really fast.

Last edited by hmnwilson (2010-08-08 12:52:45)


I'm taking a break from Scratch until 2.0 comes out. Any messages sent between then and now probably won't be read - sorry.
(Oct. 20, 2011)

Offline

 

#4 2010-08-08 15:42:35

rogererens
New Scratcher
Registered: 2010-08-08
Posts: 3

Re: How do I remove a specific item from a list?

hmnwilson wrote:

set [listchecker] to (1)
repeat until <(item (listchecker) of [colors])) = [yellow]>
  change [listchecker] by (1)
[end of loop]
delete item (listchecker) of [colors]

This would search the list for an item called "yellow" (you can change that to any color you want), and deletes it after it finds it. Just note that the "repeat until" block is kind of slow by default, so try not to use it when you need it to work really fast.

Thanks,

I thought of this just after posting. What I'm really trying is to create a bingo game.
I filled one list (drawable_balls) with incrementing integers, e.g. (1, 2, 3, 4, 5).
I'd like to pick randomly one integer out of that list and move it to another list (drawn_balls).
It's a pity that this doesn't work:

Code:

repeat until <length of {drawable_balls}> = 0
..set [ball] to <item[any]> of {drawable_balls}
..add [ball] to {drawn_balls}
..delete [ball] of {drawable_balls}

The lists' delete method only works using indices, not objects, if I understand correctly.

So what I ended up with is creating a help variable random_index:

Code:

repeat until <length of {drawable_balls}> = 0
..set [random_index] to (pick random 1 to (length of {drawable_balls}))
..set [ball] to <item[random_index]> of {drawable_balls}
..add [ball] to {drawn_balls}
..delete [random_index] of {drawable_balls}

Thanks for your reactions,

Roger

Offline

 

Board footer