~ubuntu-branches/debian/sid/octave3.0/sid

« back to all changes in this revision

Viewing changes to doc/interpreter/HTML/Looping-Over-Structure-Elements.html

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere
  • Date: 2007-12-23 16:04:15 UTC
  • Revision ID: james.westby@ubuntu.com-20071223160415-n4gk468dihy22e9v
Tags: upstream-3.0.0
ImportĀ upstreamĀ versionĀ 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<html lang="en">
 
2
<head>
 
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; } 
 
21
--></style>
 
22
</head>
 
23
<body>
 
24
<div class="node">
 
25
<p>
 
26
<a name="Looping-Over-Structure-Elements"></a>
 
27
Up:&nbsp;<a rel="up" accesskey="u" href="The-for-Statement.html#The-for-Statement">The for Statement</a>
 
28
<hr>
 
29
</div>
 
30
 
 
31
<h4 class="subsection">10.5.1 Looping Over Structure Elements</h4>
 
32
 
 
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:
 
36
 
 
37
<pre class="example">     for [ <var>val</var>, <var>key</var> ] = <var>expression</var>
 
38
       <var>body</var>
 
39
     endfor
 
40
</pre>
 
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,
 
45
 
 
46
<pre class="example">     x.a = 1
 
47
     x.b = [1, 2; 3, 4]
 
48
     x.c = "string"
 
49
     for [val, key] = x
 
50
       key
 
51
       val
 
52
     endfor
 
53
     
 
54
          -| key = a
 
55
          -| val = 1
 
56
          -| key = b
 
57
          -| val =
 
58
          -|
 
59
          -|   1  2
 
60
          -|   3  4
 
61
          -|
 
62
          -| key = c
 
63
          -| val = string
 
64
</pre>
 
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.
 
68
 
 
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
 
72
known.
 
73
 
 
74
   </body></html>
 
75