(Redirected from () Contains () (block))

() Contains ()?
2.0 () Contains ()?.png
Category List
Type Boolean
Introduced in 1.4
"() Contains ()" redirects here. For the Operators block that checks if one string contains the other, see () Contains ()? (Operators block).

The () Contains ()? block is a List block and a Boolean block. The block checks if any items in the specified list are equal to the given text—if at least one of them is, the block returns true; if none of them are, it returns false. The item must contain the exact text; for example, "abc" would not work if the item contained "abcde". This block was case-sensitive in Scratch 1.4.

Example Uses

If an object must be checked if it is in a list or not, this block can be used.

Some common uses for the () Contains ()? block:

  • Scanning lists
if <[lunch v] contains [spinach]?> then
say [Yuck! Spinach!]
end
  • Checking if an item already exists before adding it to an inventory
if <not <[collection v] contains [fan]?>> then
add [fan] to [collection v]
else
say [I already have a fan in my collection.]
end
  • Checking if a word is in a list of words
if <[words v] contains [chocolate]?> then
say [Chocolate!? I'm allergic to chocolate!]
End
  • Making sure an item is in an inventory before performing a command.
if <[collection v] contains [fan]?> then
broadcast [action v]
else
say [It's too far to reach the pile of sand on that platform.]
end

Workaround

Main article: List of Block Workarounds

This block can be replicated with the following code:

set [count v] to [1]
repeat until <<(count) > (length of [list v])> or <(item (count) of [list v]) = (thing)>>
  change [count v] by (1)
end
set [report v] to <(count) > (length of [list v])>

When it is needed,<(report) = [true]> is placed into the boolean insert.

Using the workaround gives either an advantage or a disadvantage: this block is case-sensitive, while the equals block is not.

Related Suggestion

Some Scratchers want a block that checks if a given string contains another string: <[] contains []?::operators>.[1] It would be in the operators category. This block will come in Scratch 3.0.[2]

References

  1. https://scratch.mit.edu/discuss/topic/165516/
  2. https://wiki.scratch.mit.edu/wiki/()_Contains_()_(Operators_block)