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

#1 2012-11-16 10:51:55

Dragonfyre173
New Scratcher
Registered: 2012-08-24
Posts: 2

Finding list number containing an item

It's a little hard to word this in a sentence, but I have a script with a set of numbers in it, that looks sorta like..
1. 2
2. 4
3. 5
etc.

Let's say that I have a script which needs that number containing the 2. For example, if Sprite X has Costume Number 5, I want to get 3 out of that. And if it uses costume number 3, that's not on the list, so it'll report something else entirely. I can't even begin to think about how I'll do that, so I've just been making another list that looks like
1. 0
2. 1002
3. 0
4. 1105

To report true or false for those values. However, that's very clunky, since I've got 150 values in that list, and they need to be in order - so when I accidentally delete one, I've got to start over.  I'm starting with something like this...

when gf clicked
if <(list of values) contains (costume # of sprite X)>
But I can't figure out where to go from there to get the list number from that. Sorry if this is vague, any help?

Offline

 

#2 2012-11-16 10:58:13

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

Re: Finding list number containing an item

set [counter v] to [0]
repeat (length of [list v])
change [counter v] by (1)
if <(item (counter) of [list v]) = [value you are looking for]>
set [index you need v] to (counter)
end
end
...and the variable "index you need" will have the list number you need

Last edited by jvvg (2012-11-16 10:58:27)


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-11-16 10:59:04

Dragonfyre173
New Scratcher
Registered: 2012-08-24
Posts: 2

Re: Finding list number containing an item

jvvg wrote:

set [counter v] to [0]
repeat (length of [list v])
change [counter v] by (1)
if <(item (counter) of [list v]) = [value you are looking for]>
set [index you need v] to (counter)
end
end
...and the variable "index you need" will have the list number you need

Thanks! That's just what I was looking for.

Offline

 

Board footer