length of ()

From PenguinMod Wiki (Official)
Revision as of 20:25, 23 July 2024 by MediansSC (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Length of () (Lists)
Jump to navigation Jump to search

This article is about the block in the Lists subcategory. For the Operators block, see length of () (Operators). For the Sound block, see length of ().

The length of () block is a reporter block in the Lists subcategory that returns the number of items in the selected list. It originates from Scratch.

Workaround

This block can be worked around by counting each item in the list:

define find list length
set [count] to [0]::#0869c2
for each item [saved var v] in [list v] {
change [count] by (1)::#0869c2
}::list
return (get [count]::#0869c2)::custom cap

Or by getting the index of the last item:

(item # of (item [last] of [list v]) in [list v])

The "last" option was an option that was added for compatibility with Scratch 1.x and 2.0. This doesn't work if the last item also appears earlier in the list because the item # of [] in [ v] block can only report the first instance of the string in the selected list. It can also be replicated with:

define find list length
set [item num] to [1]::#0869c2
set [count] to [0]::#0869c2
repeat until <(item (get [item num]::#0869c2) of [list v]) = []> 
change [count] by [1]::#0869c2
change [item num] by [1]::#0869c2
end
return (get [count]::#0869c2)::custom cap //the input in the repeat until that has nothing is blank