RosettaCodeData/Task/Associative-array-Iteration/RLaB/associative-array-iteration...

23 lines
755 B
Plaintext

x = <<>>; // create an empty list
x.hello = 1;
x.world = 2;
x.["!"] = 3;
// to iterate over identifiers of a list one needs to use the function ''members''
// the identifiers are returned as a lexicographically ordered string row-vector
// here ["!", "hello", "world"]
for(i in members(x))
{ printf("%s %g\n", i, x.[i]); }
// occasionally one needs to check if there exists member of a list
y = members(x); // y contains ["!", "hello", "world"]
clear(x.["!"]); // remove member with identifier "!" from the list "x"
for(i in y)
{ printf("%s %g\n", i, x.[i]); } // this produces error because x.["!"] does not exist
for(i in y)
{
if (exist(x.[i]))
{ printf("%s %g\n", i, x.[i]); } // we print a member of the list "x" only if it exists
}