3
<title>Looping Over Structure Elements - Untitled</title>
4
<meta http-equiv="Content-Type" content="text/html">
5
<meta name="description" content="Untitled">
6
<meta name="generator" content="makeinfo 4.11">
7
<link title="Top" rel="start" href="index.html#Top">
8
<link rel="up" href="The-for-Statement.html#The-for-Statement" title="The for Statement">
9
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
10
<meta http-equiv="Content-Style-Type" content="text/css">
11
<style type="text/css"><!--
12
pre.display { font-family:inherit }
13
pre.format { font-family:inherit }
14
pre.smalldisplay { font-family:inherit; font-size:smaller }
15
pre.smallformat { font-family:inherit; font-size:smaller }
16
pre.smallexample { font-size:smaller }
17
pre.smalllisp { font-size:smaller }
18
span.sc { font-variant:small-caps }
19
span.roman { font-family:serif; font-weight:normal; }
20
span.sansserif { font-family:sans-serif; font-weight:normal; }
26
<a name="Looping-Over-Structure-Elements"></a>
27
Up: <a rel="up" accesskey="u" href="The-for-Statement.html#The-for-Statement">The for Statement</a>
31
<h4 class="subsection">10.5.1 Looping Over Structure Elements</h4>
33
<p><a name="index-structure-elements_002c-looping-over-547"></a><a name="index-looping-over-structure-elements-548"></a>
34
A special form of the <code>for</code> statement allows you to loop over all
35
the elements of a structure:
37
<pre class="example"> for [ <var>val</var>, <var>key</var> ] = <var>expression</var>
41
<p class="noindent">In this form of the <code>for</code> statement, the value of <var>expression</var>
42
must be a structure. If it is, <var>key</var> and <var>val</var> are set to the
43
name of the element and the corresponding value in turn, until there are
44
no more elements. For example,
46
<pre class="example"> x.a = 1
65
<p>The elements are not accessed in any particular order. If you need to
66
cycle through the list in a particular way, you will have to use the
67
function <code>fieldnames</code> and sort the list yourself.
69
<p>The <var>key</var> variable may also be omitted. If it is, the brackets are
70
also optional. This is useful for cycling through the values of all the
71
structure elements when the names of the elements do not need to be