Associative array/Iteration: Difference between revisions

Content added Content deleted
(→‎{{header|MATLAB}} / {{header|Octave}}: associative arrays can be built with structs)
Line 748: Line 748:
end fn
end fn
</lang>
</lang>

=={{header|MATLAB}} / {{header|Octave}}==

Associative arrays are not native to Matlab and Octave, but one can (mis-)use "struct" as an associative array.

<lang Matlab> s.a = 1;
s.b = 2;
s.C = [3,4,5];

keys = fieldnames(s);
for k=1:length(keys),
key = keys{k};
value = getfield(s,key); % get value of key
disp(value);
s = setfield(s,key,-value); % set value of key
end;
disp(s) </lang>

Output:
<pre> 1
2
3 4 5
>> disp(s)

scalar structure containing the fields:

a = -1
b = -2
C =

-3 -4 -5</pre>


=={{header|Objective-C}}==
=={{header|Objective-C}}==