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

#1 2012-09-12 00:11:40

bullelk12
Scratcher
Registered: 2012-05-26
Posts: 100+

deleting a specific item in a list

I just need a script that deletes a specific item like 'silver' out of a list no matter what location the item is in, in the list.


http://mag.racked.eu/cimage/i6000/Achievement++get%21/Scratcher+love+minecraft%21/mca.png

Offline

 

#2 2012-09-12 00:26:40

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: deleting a specific item in a list

You'll need to create a variable (counter) and (checking) for this.

set [counter v] to [1]
set [checking v] to [0]
if <[list v] contains [silver]>
repeat until <<(counter) < ((length of [list v])-(1))>or<(checking)=(1)>>
if <<item (counter) of [list v]>=[silver]>
delete (counter) of [list v]
set [checking v] to [1]
else
change [counter v] by (1)
end
end
end

Offline

 

#3 2012-09-12 07:27:14

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: deleting a specific item in a list

if <[list v] contains [silver]>
set [counter v] to [1]
repeat until <(counter) > (length of [list v])>
if <<item (counter) of [list v]>=[silver]>
delete (counter) of [list v]
set [checking v] to (length of [list v])
end
change [counter v] by (1)
end
end


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#4 2012-09-12 07:28:50

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: deleting a specific item in a list

TorbyFork234 wrote:

You'll need to create a variable (counter) and (checking) for this.

set [counter v] to [1]
set [checking v] to [0]
if <[list v] contains [silver]>
repeat until <<(counter) < ((length of [list v])-(1))>or<(checking)=(1)>>
if <<item (counter) of [list v]>=[silver]>
delete (counter) of [list v]
set [checking v] to [1]
else
change [counter v] by (1)
end
end
end

Pretty sure you need a "greater than" in that script, not a "less than"


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#5 2012-09-12 09:16:43

BirdByte
Scratcher
Registered: 2012-07-07
Posts: 1000+

Re: deleting a specific item in a list

Guys, you're over-complicating this.

set [counter v] to [1]
if <[list v] contains [silver]>
 repeat until <(item (counter) of [list v]) = [silver]>
  change [counter v] by (1)
 end
 delete (counter) of [list v]
end
That will only delete the first "silver" in the list. If you would like to delete all the "silver"s in the list, do this:

if <[list v] contains [silver]>
 repeat until <not <[list v] contains [silver]>>
  set [counter v] to [1]
  repeat until <(item (counter) of [list v]) = [silver]>
   change [counter v] by (1)
  end
  delete (counter) of [list v]
 end
end

Last edited by BirdByte (2012-09-12 09:23:07)


http://i50.tinypic.com/312u714.jpg

Offline

 

#6 2012-09-12 12:28:21

Prestige
Scratcher
Registered: 2008-12-15
Posts: 100+

Re: deleting a specific item in a list

BirdByte wrote:

Guys, you're over-complicating this.

set [counter v] to [1]
if <[list v] contains [silver]>
 repeat until <(item (counter) of [list v]) = [silver]>
  change [counter v] by (1)
 end
 delete (counter) of [list v]
end
That will only delete the first "silver" in the list. If you would like to delete all the "silver"s in the list, do this:

if <[list v] contains [silver]>
 repeat until <not <[list v] contains [silver]>>
  set [counter v] to [1]
  repeat until <(item (counter) of [list v]) = [silver]>
   change [counter v] by (1)
  end
  delete (counter) of [list v]
 end
end

That one is the simplest ^ and also easiest to understand. The counter variable goes through each item of the script, first, second etc until the item which corresponds to the counter value, eg the 34th value = silver. It then deletes this item.

The only problem with this is that you delete a value from the list. If the first and second items are silver, you would encounter a problem. Counter, =1, detects silver and deletes it, and then sets counter to 2. BUT, the original second item (silver) is NOW item 1, and the counter won't check it but instead move on. Theoretically, you could still have silver's in the list. To correct this simply don't change the counter if silver was detected:

if <[list v] contains [silver]>
set [counter v] to (1)
repeat until <(counter) > (length of [list v])>
if <(item (counter) of [list v]) = [silver]>
delete (counter) of [list v]
else
change [counter v] by (1)

Last edited by Prestige (2012-09-12 12:30:03)


"Don't insult someone until you've walked a mile in their shoes. That way, if they don't like what you have to say, you'll be a mile away and still have their shoes  smile  "

Offline

 

#7 2012-09-12 14:13:11

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: deleting a specific item in a list

BirdByte wrote:

Guys, you're over-complicating this.
...code and stuff

if <[list v] contains [silver]>
 repeat until <not <[list v] contains [silver]>>
  set [counter v] to [1]
  repeat until <(item (counter) of [list v]) = [silver]>
   change [counter v] by (1)
  end
  delete (counter) of [list v]
 end
end

As long as we're simplifying, you don't even need that first if statement.  tongue   And using "repeat until" rather than an if statement means you are unnecessarily looping through parts of the list that you've already checked.

Edit: don't use the script below

set [counter v] to (1)
repeat until <not <[list v] contains [silver]>>
if<(item (counter) of [list v]) = [silver]>
delete (counter) of [list v]
end
change [counter v] by (1)
end

Last edited by MoreGamesNow (2012-09-12 16:33:49)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#8 2012-09-12 14:17:50

BirdByte
Scratcher
Registered: 2012-07-07
Posts: 1000+

Re: deleting a specific item in a list

MoreGamesNow wrote:

BirdByte wrote:

Guys, you're over-complicating this.
...code and stuff

if <[list v] contains [silver]>
 repeat until <not <[list v] contains [silver]>>
  set [counter v] to [1]
  repeat until <(item (counter) of [list v]) = [silver]>
   change [counter v] by (1)
  end
  delete (counter) of [list v]
 end
end

As long as we're simplifying, you don't even need that first if statement.  tongue   And using "repeat until" rather than an if statement means you are unnecessarily looping through parts of the list that you've already checked.

set [counter v] to (1)
repeat until <not <[list v] contains [silver]>>
if<(item (counter) of [list v]) = [silver]>
delete (counter) of [list v]
end
change [counter v] by (1)
end

That script won't work because counter will go higher than the length of the list. Use this, then:

repeat until <not <[list v] contains [silver]>>
 set [counter v] to (1)
 repeat (length of [list v])
  if<(item (counter) of [list v]) = [silver]>
   delete (counter) of [list v]
  end
  change [counter v] by (1)
 end
end

Last edited by BirdByte (2012-09-12 14:18:35)


http://i50.tinypic.com/312u714.jpg

Offline

 

#9 2012-09-12 16:33:25

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: deleting a specific item in a list

BirdByte wrote:

That script won't work because counter will go higher than the length of the list. Use this, then:

repeat until <not <[list v] contains [silver]>>
 set [counter v] to (1)
 repeat (length of [list v])
  if<(item (counter) of [list v]) = [silver]>
   delete (counter) of [list v]
  end
  change [counter v] by (1)
 end
end

Wow, that was embarrassing.  Why are you using "repeat until" instead of just the if-block?

Fixed Script from above:

set [counter v] to (1)
repeat until <<not <[list v] contains [silver]>> or <(counter) > (length of [list v])>>
if<(item (counter) of [list v]) = [silver]>
delete (counter) of [list v]
end
change [counter v] by (1)
end


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#10 2012-09-12 17:49:44

bullelk12
Scratcher
Registered: 2012-05-26
Posts: 100+

Re: deleting a specific item in a list

sweet, they all worked. thx.


http://mag.racked.eu/cimage/i6000/Achievement++get%21/Scratcher+love+minecraft%21/mca.png

Offline

 

#11 2012-09-12 18:06:29

BoltBait
Scratcher
Registered: 2009-03-09
Posts: 1000+

Re: deleting a specific item in a list

Here is another way to do it:

set [counter v] to (length of [list v])
repeat until <(counter) = (0)>
  if <(item (counter) of [list v]) = [silver]>
    delete (counter) of [list v]
  end
  change [counter v] by (-1)
end

Last edited by BoltBait (2012-09-12 18:08:20)


Animated sigs must be banned!
http://boltbait.com/j.pnghttp://boltbait.com/s.pnghttp://boltbait.com/d.pnghttp://boltbait.com/a.pnghttp://boltbait.com/p.png

Offline

 

#12 2012-09-12 18:11:11

bullelk12
Scratcher
Registered: 2012-05-26
Posts: 100+

Re: deleting a specific item in a list

lol, so many options!


http://mag.racked.eu/cimage/i6000/Achievement++get%21/Scratcher+love+minecraft%21/mca.png

Offline

 

Board footer