Discuss Scratch
- Discussion Forums
- » Suggestions
- » A block that returns a value corresponding to the position of an item in a list!
- Romi67
-
Scratcher
27 posts
A block that returns a value corresponding to the position of an item in a list!
Hello Scratchers,
Ok, I know right, Scratch is pretty great. But it could be even greater. Like having a block that returns a value corresponding to the position of an item in a list! It would be so useful! Like this :
In order to explain it, let's say we have a list like this :
list of fruits
1 - Peach
2 - Apple
3 - Banana
4 - Mango
And we construct a script like this :
Normally, the item … of list … block only returns the name of the entry at 2nd position, that is, in our case, “Apple”.
But with the help of this block, the sprite would say 2, because the position of the said entry (Apple) is equal to 2.
I think this block would be useful for memory quizzes where you see a list for a few seconds, then the list hides itself, The sprite then asks you questions on where a certain item is in the list.
Now, what would you like to see added in Scratch?
Ok, I know right, Scratch is pretty great. But it could be even greater. Like having a block that returns a value corresponding to the position of an item in a list! It would be so useful! Like this :
(position of item ( v) of [list v] :: list)
In order to explain it, let's say we have a list like this :
list of fruits
1 - Peach
2 - Apple
3 - Banana
4 - Mango
And we construct a script like this :
when green flag clicked
say (position of item ( 2) of [list of fruits v] :: list)
Normally, the item … of list … block only returns the name of the entry at 2nd position, that is, in our case, “Apple”.
But with the help of this block, the sprite would say 2, because the position of the said entry (Apple) is equal to 2.
I think this block would be useful for memory quizzes where you see a list for a few seconds, then the list hides itself, The sprite then asks you questions on where a certain item is in the list.
when green flag clicked
show list [ list of fruits]
wait (6) secs
hide list [ list of fruits]
ask [Where is located "Peach" in the list?] and wait
if <(answer) = (position of item ( 1) of [list of fruits v] :: list)> then
say [Right!] for (2) secs
else
say [Wrong!] for (2) secs
end
Now, what would you like to see added in Scratch?
(Romi67)
Last edited by Romi67 (July 5, 2017 08:55:38)
- Blaze349
-
Scratcher
1000+ posts
A block that returns a value corresponding to the position of an item in a list!
But to get the value you would have to do
To get the position you could just do
item (variable) of list (list)
To get the position you could just do
set (pos) to (variable)
- FancyFoxy
-
Scratcher
500+ posts
A block that returns a value corresponding to the position of an item in a list!
I think this is a duplicate.
- Romi67
-
Scratcher
27 posts
A block that returns a value corresponding to the position of an item in a list!
Blaze349 wrote:
But to get the value you would have to do
item (variable) of list (list)
To get the position you could just do
set (pos) to (variable)
Yeah!
I never thought about that. But I guess my solution is somewhat simpler, since it's less complicated and easier to understand

FancyFoxy wrote:
I think this is a duplicate.
What do you mean by that? You mean there's a post just like mine?
Last edited by Romi67 (July 5, 2017 10:27:07)
- asivi
-
Scratcher
1000+ posts
A block that returns a value corresponding to the position of an item in a list!
So, is this a sort of joke?
Position(item number) of item number?
Position(item number) of item number?
- walkcycle
-
Scratcher
500+ posts
A block that returns a value corresponding to the position of an item in a list!
So instead of
it would be
What is the report when a list has the same item in more than one position?
(item ( v) of [list v] :: list)
it would be
(position of item [ ] in [list v] :: list)
What is the report when a list has the same item in more than one position?
- Romi67
-
Scratcher
27 posts
A block that returns a value corresponding to the position of an item in a list!
So instead of(item ( v) of [list v] :: list)
it would be(position of item ( v) in [list v] :: list)
What is the report when a list has the same item in more than one position?
Very good question! I don't know how Scratch would deal with items appearing multiple times.
However, I can suppose that it would return the position of the first appearance of the item only.
- Paddle2See
-
Scratch Team
1000+ posts
A block that returns a value corresponding to the position of an item in a list!
Are you sure you don't mean
Where you give it the item value and it returns the item number? That would be a handy thing to have.
say (position of item [Apple] in [list of fruits v] :: list)
Where you give it the item value and it returns the item number? That would be a handy thing to have.
- Romi67
-
Scratcher
27 posts
A block that returns a value corresponding to the position of an item in a list!
Uh… Yeah! You found a better solution than mine! And yes, it can be really handy for Scratch projects! 
However, there's a downfall. Let's say we have this list:
1 - Yay this is just a great item for a list XD
2 - Lolz ZOMG
… and we want to return the position value of the first value:
Using your method, It would be quite tedious having to copy exactly the right string to get the exact result. Wouldn't that be “unconvenient” for long strings?

However, there's a downfall. Let's say we have this list:
1 - Yay this is just a great item for a list XD
2 - Lolz ZOMG
… and we want to return the position value of the first value:
(position of item [Yay this is just a great item for a list XD] of [list v] :: list)
Using your method, It would be quite tedious having to copy exactly the right string to get the exact result. Wouldn't that be “unconvenient” for long strings?

Last edited by Romi67 (July 5, 2017 12:33:25)
- Charles12310
-
Scratcher
1000+ posts
A block that returns a value corresponding to the position of an item in a list!
(Before you complain about necroposting, remember that necroposting is fine depending on if the necropost contributes and adds more to the topic, and if there wasn't any final decision. Besides, it's not against the rules to necropost.)
No support, what if there are two items in a list that report the same string?
No support, what if there are two items in a list that report the same string?
Last edited by Charles12310 (Oct. 12, 2017 16:52:47)
- walkcycle
-
Scratcher
500+ posts
A block that returns a value corresponding to the position of an item in a list!
No support, what if there are two items in a list that report the same string?
Because we have
(item ( v) of [list v] :: list) // already in Scratch
this would be
(position of item [ ] in [list v] :: list) // or similar
a block that reports the position (or number) of the item, not a string.
See above.
- TheUltimatum
-
Scratcher
1000+ posts
A block that returns a value corresponding to the position of an item in a list!
Why not implement something like this yourself? What is your specific reasoning for this besides getting native speeds?
- DaEpikDude
-
Scratcher
1000+ posts
A block that returns a value corresponding to the position of an item in a list!
- Romi67
-
Scratcher
27 posts
A block that returns a value corresponding to the position of an item in a list!
(Before you complain about necroposting, remember that necroposting is fine depending on if the necropost contributes and adds more to the topic, and if there wasn't any final decision. Besides, it's not against the rules to necropost.)
No support, what if there are two items in a list that report the same string?
I necropost, and so, it bothers you?
Well, it's pretty much a bug. But if I had to fix that, only the block closer to the top would be taken in account.
Why not implement something like this yourself? What is your specific reasoning for this besides getting native speeds?
What the hell do you mean? What are native speeds? And, to answer your question, no, I couldn't implement such a thing myself, given that this block isn't available yet. They might however implement the feature sometime in the future.
hey look at that, my topic duped someone
Didn't even see that topic. And yes, that was poetic

- TheUltimatum
-
Scratcher
1000+ posts
A block that returns a value corresponding to the position of an item in a list!
Please be polite when replying to someone.Why not implement something like this yourself? What is your specific reasoning for this besides getting native speeds?What do you mean? What are native speeds? And, to answer your question, no, I couldn't implement such a thing myself, given that this block isn't available yet. They might however implement the feature sometime in the future.
Language is uncalled for.Native meaning on scratch's virtual machine or interpreter or just flash in general instead of running in scratch blocks itself.
You can implement this quite easy with the following custom block:
//You'll need to change the list used to the name of the list you'd like to find the item in.
define get_index (item)
set [i v] to [0]
repeat until <<(item (i) of [list v]) = (item::custom)> or <(i) > (length of [list v])>> //This loop finds the item.
change [i v] by (1)
end
if <(i) > (length of [list v])> then
set [get_index v] to (-1) //Return this if it couldn't find the item in list.
else
set [get_index v] to (i) //Return the index of the item in the list.
end
- Romi67
-
Scratcher
27 posts
A block that returns a value corresponding to the position of an item in a list!
Please be polite when replying to someone.Why not implement something like this yourself? What is your specific reasoning for this besides getting native speeds?What do you mean? What are native speeds? And, to answer your question, no, I couldn't implement such a thing myself, given that this block isn't available yet. They might however implement the feature sometime in the future.Language is uncalled for.
Native meaning on scratch's virtual machine or interpreter or just flash in general instead of running in scratch blocks itself.
You can implement this quite easy with the following custom block://You'll need to change the list used to the name of the list you'd like to find the item in.
define get_index (item)
set [i v] to [0]
repeat until <<(item (i) of [list v]) = (item::custom)> or <(i) > (length of [list v])>> //This loop finds the item.
change [i v] by (1)
end
if <(i) > (length of [list v])> then
set [get_index v] to (-1) //Return this if it couldn't find the item in list.
else
set [get_index v] to (i) //Return the index of the item in the list.
end
Sorry, I tend to get a bit angry and stressed after a day's work, I really didn't mean to be mean in a message like that.

Anyhoo, that script looks quite cool. I'll have to try to understand it! I must say I haven't understood everything the first time I look at that script…
- Paddle2See
-
Scratch Team
1000+ posts
A block that returns a value corresponding to the position of an item in a list!
This block now exists.
- Discussion Forums
- » Suggestions
-
» A block that returns a value corresponding to the position of an item in a list!