Associative array/Iteration: Difference between revisions

Content added Content deleted
m (restore alphabetical order)
(→‎{{header|MATLAB}} / {{header|Octave}}: more concise explanation)
Line 734: Line 734:
=={{header|MATLAB}} / {{header|Octave}}==
=={{header|MATLAB}} / {{header|Octave}}==


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


<lang Matlab> s.a = 1;
<lang Matlab> keys = fieldnames(hash);
s.b = 2;
s.C = [3,4,5];

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


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


<lang Matlab> keys = fieldnames(hash);
scalar structure containing the fields:
for k=1:length(keys),

a = -1
key = keys{k};
value = hash.(key); % get value of key
b = -2
hash.(key) = -value; % set value of key
C =
end; </lang>

-3 -4 -5</pre>


=={{header|NetRexx}}==
=={{header|NetRexx}}==