An Index is a numerical representation of an item's position in a sequence. This sequence can refer to many things: a list, a string of characters, or any arbitrary sequence of values. 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 indexes that cannot be changed.

Usage

Indexes 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 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".

Scratch Lists

In this list, "Cheese" has an index of 3.

A list in Scratch is a sequence of values. Lists can store numbers, words, or random jargon. An empty list has zero elements or items in it. When a list does have items present, those items are accessed by their index. The first item in a list has an index of "1", the second has an index of "2", and so on.

The following list blocks use numerical parameters representing an index:

Zero-Based Indexing

In Scratch, indexing starts at "1". This means the first letter in a string is letter "1", the first item in a list is item "1", etc. However, most other programming languages have indexes that start at "0". This means that the 5th item in a list would have an index of "4". Some programming languages that use zero-based indexing are found below:

  • Javascript
  • Python
  • PHP
  • C++