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

#1 2012-09-30 14:19:03

dmr89xo
New Scratcher
Registered: 2012-09-28
Posts: 6

Removing an item

How does one make the code remove an item(name, etc)  from the list regardless of how many times it appears?

Offline

 

#2 2012-09-30 14:21:58

jvvg
Scratcher
Registered: 2008-03-26
Posts: 1000+

Re: Removing an item

You can try something like this:

when gf clicked
set [counter v] to [0]
repeat (length of [list v])
change [counter v] by (1)
if <(item (counter) of [list v]) = [whatever you want to remove]>
delete (counter) of [list v]
end
end


http://tiny.cc/zwgbewhttp://tiny.cc/e1gbewhttp://tiny.cc/zygbewhttp://tiny.cc/izgbew
Goodbye, Scratch 1.4  sad                                                        Hello Scratch 2.0!  smile

Offline

 

#3 2012-09-30 14:39:18

dmr89xo
New Scratcher
Registered: 2012-09-28
Posts: 6

Re: Removing an item

Okay cool! Thank you  smile

Offline

 

#4 2012-09-30 15:19:59

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Removing an item

jvvg wrote:

You can try something like this:

when gf clicked
set [counter v] to [0]
repeat (length of [list v])
change [counter v] by (1)
if <(item (counter) of [list v]) = [whatever you want to remove]>
delete (counter) of [list v]
end
end

Actually, that wouldn't work 100% of the time.

For example, lets say that we want to remove "pie" from the list and we have this list:

candy
pie
pie
chocolate

As soon as your script removes the first "pie", it will move onto the "chocolate" and entirely skip the second "pie", so this is the script I would use:

when gf clicked
set [counter v] to (1)
repeat until <not <[list v] contains [whatever you want to remove]>>
if <(item (counter) of [list v]) = [whatever you want to remove]>
delete (counter) of [list v]
else
change [counter v] by (1)
end
end


http://i46.tinypic.com/35ismmc.png

Offline

 

#5 2012-09-30 15:47:01

ImagineIt
Scratcher
Registered: 2011-02-28
Posts: 1000+

Re: Removing an item

ErnieParke wrote:

jvvg wrote:

You can try something like this:

when gf clicked
set [counter v] to [0]
repeat (length of [list v])
change [counter v] by (1)
if <(item (counter) of [list v]) = [whatever you want to remove]>
delete (counter) of [list v]
end
end

Actually, that wouldn't work 100% of the time.

For example, lets say that we want to remove "pie" from the list and we have this list:

candy
pie
pie
chocolate

As soon as your script removes the first "pie", it will move onto the "chocolate" and entirely skip the second "pie", so this is the script I would use:

when gf clicked
set [counter v] to (1)
repeat until <not <[list v] contains [whatever you want to remove]>>
if <(item (counter) of [list v]) = [whatever you want to remove]>
delete (counter) of [list v]
else
change [counter v] by (1)
end
end

Smart.

Offline

 

#6 2012-09-30 15:49:46

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Removing an item

ImagineIt wrote:

ErnieParke wrote:

jvvg wrote:

You can try something like this:

when gf clicked
set [counter v] to [0]
repeat (length of [list v])
change [counter v] by (1)
if <(item (counter) of [list v]) = [whatever you want to remove]>
delete (counter) of [list v]
end
end

Actually, that wouldn't work 100% of the time.

For example, lets say that we want to remove "pie" from the list and we have this list:

candy
pie
pie
chocolate

As soon as your script removes the first "pie", it will move onto the "chocolate" and entirely skip the second "pie", so this is the script I would use:

when gf clicked
set [counter v] to (1)
repeat until <not <[list v] contains [whatever you want to remove]>>
if <(item (counter) of [list v]) = [whatever you want to remove]>
delete (counter) of [list v]
else
change [counter v] by (1)
end
end

Smart.

Thanks you.

Edit: Wait, shouldn't this be in Help with Scripts and not Questions about Scratch?

Last edited by ErnieParke (2012-09-30 15:56:59)


http://i46.tinypic.com/35ismmc.png

Offline

 

Board footer