Item number of () in (): Difference between revisions

Item number of () in ()
Content deleted Content added
Created page with "{{Infobox Block |category = Lists |title = item # of () in () |block_name = item # of [] in [ v] |block_type = Reporter Block }} {{Incorrect Title|item # of () in ()}} The '''item # of () in ()''' block is a reporter block in the Lists category that reports the item number of the first occurrence of the inputted item in the selected list. It was introduced in Scratch 3.0. ==Workaround== This block can be worked around using the following code: <scratchblocks> def..."
 
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 6:
}}
{{Incorrect Title|item # of () in ()}}
The '''item # of () in ()''' block is a reporter block in the [[Lists]] categorysubcategory 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:
<scratchblocks>
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
</scratchblocks>
This is similar to [[() = ()]], and [[() contains ()? (Lists)|<sb>[ v] contains ()?</sb>]].
==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:
<scratchblocks>
define find item # of (string1)
set [item number v] to [0]::#0869c2 //keep track of the item in the list
if <[listcharacter names v] contains (string1)?> then
repeat until <(item (get [item number]::#0869c2) of [listcharacter names v]) = (string1)>
change [item number v] 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
</scratchblocks>
The custom block would then be used like this:
<scratchblocks>
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
</scratchblocks>
==See also==
*[[index of () in ()]]
*[[last index of () in ()]]
{{DISPLAYTITLE:item # of () in ()}}
{{Blockcat|cat=List|type=Reporter|sc=y}}