Reading from a List
-
Prerequisite
Description
A list is just like many variable items positioned sequentially. You can read the value of any item using its position, or get information about the list itself.
Getting the Length of a List
This reporter block tells you how many items are in the list.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/1ab485eb-845f-4cfe-9b0d-0ad56c216057.png" width="245" style="filter: drop-shadow(0.35rem 0.35rem 0.4rem rgba(0, 0, 0, 0.5));">
Demo
Note that the position indices of the items go from 1 to the list length. For example, if you want to insert an item before the last item of a list, you can use the list length as the position.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/bbc54e82-907c-4cbb-923d-83f2706c50b4.gif" width="970" style="border-radius: 5px; border: 1px solid #29622d;">
Reading an Item’s Value at a Position
If you know the position of an item, you can read its value using this block:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/95d4f784-1d6e-42d7-b966-ddd3eacdce09.png" width="297" style="">
The position index has to be at least 1 and at most the length of the list. For example, to get the last item’s value, you can do this:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4177dbc4-71d8-4ab8-9a4f-7b546a117b85.gif" width="970" style="border-radius: 5px; border: 1px solid #29622d;">
Searching for an Item by Its Value
You can not only get an item by its position, but also find the position of an item if you know its value.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/42000a98-2f4d-46fb-bd66-828c4446d84d.png" width="300" style="">
If the value is not found, then the reported value will be 0:https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b47db0ca-34d3-4274-8b60-d098b23a736d.gif" width="530" style="border-radius: 5px; border: 1px solid #29622d;">
Whether a Value is In the List
You can find out if a value is contained in a list using this boolean block, which will report “true” or “false”.
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/00074c7a-d2ce-489d-a7ad-1321ad5dc214.gif" width="539" style="border-radius: 5px; border: 1px solid #29622d;">
Searching for a part of an item
Sometimes you want to find any item that contains some value. In other words, you are searching for a part of an item. You can use this block:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b819fc8f-19e0-4a93-be9f-101a9323c060.png" width="400" style="">
For example, in the list of student names, you might want to find the index of the first item that contains “Mike” like this:
https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/16aba739-eb33-42df-afa0-611fd0157be0.gif" width="680" style="border-radius: 5px; border: 1px solid #29622d;">
-
I CreatiCode pinned this topic on
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login