Discuss Scratch

Deerleg
Scratcher
1000+ posts

Things in a List

How can you tell what order a thing in a list is? (first, second, etc.)


gigapouch
Scratcher
1000+ posts

Things in a List

(item (... v) of [list v])

Well, that tells you what the value of a certain item in a certain list is.

I have a Steam account. If you have one too, feel free to add me to your friends list.
Also check out my YouTube channel if you like the outdoors, Minecraft, cars, roller coasters, etc.
Deerleg
Scratcher
1000+ posts

Things in a List

No, I mean:
Take a thing in a list. Like maybe banana.
Then try to figure out what order it is in in the list.


levtey
Scratcher
100+ posts

Things in a List

Deerleg wrote:

No, I mean:
Take a thing in a list. Like maybe banana.
Then try to figure out what order it is in in the list.
This feature is not yet on Scratch.
DanloVorje
Scratcher
100+ posts

Things in a List

A somewhat crude method:

define find [string]
set [I v] to [0]
delete (all v) of [string locations v]
repeat (length of [list v])

change [I v] by (1)
if <(item (I) of [list v]) = (string)> then

add (I) to [string locations v]
end
end

When run without screen refresh, this should quickly give you a list (“string locations”) of all the indices in the original list where the string you are searching for appears. If you already know the string only appears once in the list, you can have “set (string location) to (I)” with variables instead of dealing with another list.

Hope this helps!

–Danlo

What I'm working on right now: Isle of Dolor
Julianthewiki
Scratcher
100+ posts

Things in a List

Deerleg wrote:

No, I mean:
Take a thing in a list. Like maybe banana.
Then try to figure out what order it is in in the list.
So, you have a list right? Say, something like this:

1 (Apple)
2 (Banana)
3 (Cherry)
4 (Strawberry)
5 (Orange)

So, Deerleg, you want to take something in a list? Well, use this block:

item (2) of [fruits v]

Then try to figure out what order it is in the list? Well then, use these blocks:
set [banana order v] to (2)

item (banana order) of [fruits v]

There, done. If something's wrong, tell me!

Julianthewiki | Double the Fun | Double the Thrill | Double the Coolness

Daydream: A Book By Julianthewiki | Official Studio | Chapter 1 | Chapter 2 | Chapter 3
World Population Clock | 24,618,660 | Learn More | Suggest Ideas
Two Year Anniversary Special | 101,405,320 | Learn More
Belated Happy Pi Day! | 102,312,109
Deerleg
Scratcher
1000+ posts

Things in a List

No. To both.
Okay, look at my project code.
http://scratch.mit.edu/projects/21665849/


Kenichi10B
Scratcher
74 posts

Things in a List

Danlo is actually right; instead of adding to a list though, set a variable to ‘I’. Then, put in:
delete ( 'I' v) of [list v]

Last edited by Kenichi10B (May 10, 2014 11:45:03)

Deerleg
Scratcher
1000+ posts

Things in a List

No. Again.
The thing is, I have no idea what order the thing in the list is.


Kenichi10B
Scratcher
74 posts

Things in a List

Deerleg wrote:

No. Again.
The thing is, I have no idea what order the thing in the list is.

Maybe I may be misunderstanding what you need, but it seems to me that it would work.


This is what Danlo's script does: It will check each item in the list one at a time for the input. If it finds an input, it just sets a variable to which attempt it is on. For example, let's assume we had a list. The script will find the location of ‘banana’ without us knowing what item # it is.

First check - Does Item 1 of Fruits = Banana? It doesn't; check item 2.
Second check - Does Item 2 of Fruits = Banana? It doesn't; check item 3.
Third check - Does Item 3 of Fruits = Banana? It doesn't; check item 4.
Fourth check - Does Item 4 of Fruits = Banana? It does!
Set some variable to ‘4’ (Though it would normally be a variable like illustrated by Danlo)
Now we know that item 4 of this list is banana. If it doesn't find the result anywhere in the list, it will keep the variable blank, and you can tell it is NOT in the list. If you think the input you are looking for may appear multiple times, than you can use a list like Danlo did, and it will put all of it's results in a list.

Hope that helps!

Powered by DjangoBB