Spec-Zone.ru › Octave 6

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

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

for [ val, key ] = expression
  body
endfor

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

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–2022 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/v5.2.0/Looping-Over-Structure-Elements.html

Spec-Zone.ru

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