item # of () in ()

From PenguinMod Wiki (Official)
Item number of () in ()
Jump to navigation Jump to search

The correct title of this article is item # of () in (). PenguinMod Wiki uses Item number of () in () instead because of technical restrictions.

The item # of () in () block is a reporter block in the Lists subcategory that reports the item number of the first occurrence of the inputted item in the selected list. If the list doesn't contain the item, then "0" is reported. It was introduced in Scratch 3.0. The default string is "thing".

Case-Sensitivity

This block is not case-sensitive. For example:

when gf clicked
delete all of [characters v]
add [Bulbasaur] to [characters v]
add [Charmander] to [characters v]
add [Squirtle] to [characters v]
say (item # of [CHARMANDER] in [characters v]) for (2) seconds //this will still say the value "2" for 2 seconds, even though the name is capitalized

This is similar to () = (), and [ v] contains ()?.

Workaround

This block can be worked around using the following code, which iterates over the items in the list and checks for the first match:

define find item # of (string1)
set [item number] to [0]::#0869c2 //keep track of the item in the list
if <[character names v] contains (string1)?> then
repeat until <(item (get [item number]::#0869c2) of [character names v]) = (string1)>
change [item number] by (1)::#0869c2
end
end
return (get [item number]::#0869c2)::custom cap //this also reports 0 if the item isn't in the list, just like the actual block

The custom block would then be used like this:

when gf clicked
delete all of [character names v]
add [Ari] to [character names v]
add [Molly] to [character names v]
add [Stan] to [character names v]
add [Kyle] to [character names v]
add [Kenny] to [character names v]
add [Eric] to [character names v]
add [Molly] to [character names v]
set [item number wanted v] to (find item # of [Molly]::custom) //this would be 2, and not 7

See also