An index (plural indices or indexes) is a numerical representation of an item's position in a sequence such as a list or string of characters. In some programming languages, an array can be defined by key-value pairs, where a specific key points to a value. An indexing system for lists in Scratch works the same way except the "keys" are numerically ordered indices that cannot be changed.

Usage

Indices exist so specific stored data can be acquired. If a program is created to look for the letter "r" in a string of characters and the string "ektsjerlsk" is entered, the program needs to check each character until it reaches an "r" or the end. It can only iterate through the characters if it has some method of referring to a particular character in the sequence. This methodology is known as indexing. Since each character is represented by a numerical, ordered index, the programmer can create the program to check each consecutive index for the letter "r".

First Index

The first index is often either 0 or 1. In many programming languages it is 0 and called zero-based indexing. In Scratch however, the first index is 1, which is one-based indexing. This is closer to how items are counted in natural language but comes with disadvantages including being less convenient when used in math.

Some programming languages that use zero-based indexing are found below:

  • JavaScript
  • Python
  • PHP
  • Java
  • C++
  • C#
  • Ruby
  • Lisp
  • C

In Scratch

Scratch has a few places where indices are found. All use one-based indexing.

Lists

Image
In this list, "shovel" has an index of 3.

A list in Scratch is a sequence of values.

The following list blocks use numerical parameters representing an index:

In addition, the item # of [] in [ v] block returns an index ("item number"), given a list item.

Strings

One string block accepts an index:

Costumes

Costume numbers in Scratch are indices. Costume numbers below 1 are wrapped, so costume 0 is the last costume, costume -1 is the second last, and so on.

This block accepts an index:

These blocks return the current costume number:

See Also

Cookies help us deliver our services. By using our services, you agree to our use of cookies.