Spec-Zone.ru › Octave 9

Наверх: Оператор for [Содержание][Индекс]

10.5.1 Перебор элементов структуры ¶

Специальная форма оператора for позволяет перебрать все элементы структуры:

for [ val, key ] = expression
  body
endfor

В этой форме оператора for значение выражения должно быть структурой. Если это так, ключ и значение устанавливаются на имя элемента и соответствующее значение поочерёдно, пока не закончатся все элементы. Например:

x.a = 1
x.b = [1, 2; 3, 4]
x.c = "string"
for [val, key] = x
  key
  val
endfor

     -| key = a
     -| val = 1
     -| key = b
     -| val =
     -|
     -|   1  2
     -|   3  4
     -|
     -| key = c
     -| val = string

Элементы не доступны в определённом порядке. Если вам нужно циклически пройтись по списку определённым образом, вам нужно будет использовать функцию fieldnames и отсортировать список самостоятельно.

© 1996–2023 The Octave Project Developers
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://docs.octave.org/v9.2.0/Looping-Over-Structure-Elements.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API