For Each Loop (Section) — Skript

Addon: Skript · Category: Section · Since: 2.10, 2.14 (stable release)

Syntax

(for [each]|loop) [value] %~object% in %objects%

Description

A specialised loop section run for each element in a list. Unlike the basic loop, this is designed for extracting the key & value from pairs. The loop element's key/index and value can be stored in a variable for convenience.

When looping a simple (non-indexed) set of values, e.g. all players, the index will be the loop counter number.

Examples

for each {_player} in players:
	send "Hello %{_player}%!" to {_player}
loop {_item} in {list of items::*}:
	broadcast {_item}'s name
for each key {_index} in {list of items::*}:
	broadcast {_index}
loop key {_index} and value {_value} in {list of items::*}:
	broadcast "%{_index}% = %{_value}%"
for each {_index}, {_value} in {my list::*}:
	broadcast "%{_index}% = %{_value}%"

View source