How to resolve the algorithm Associative array/Creation step by step in the RLaB programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Associative array/Creation step by step in the RLaB programming language
Table of Contents
Problem Statement
The goal is to create an associative array (also known as a dictionary, map, or hash).
Related tasks:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Associative array/Creation step by step in the RLaB programming language
Source code in the rlab programming language
x = <<>>; // create an empty list using strings as identifiers.
x.red = strtod("0xff0000"); // RLaB doesn't deal with hexadecimal numbers directly. Thus we
x.green = strtod("0x00ff00"); // convert it to real numbers using ''strtod'' function.
x.blue = strtod("0x0000ff");
// print content of a list
for (i in members(x))
{ printf("%8s %06x\n", i, int(x.[i])); } // we have to use ''int'' function to convert reals to integers so "%x" format works
// deleting a key/value
clear (x.red);
// we can also use numeric identifiers in the above example
xid = members(x); // this is a string array
for (i in 1:length(xid))
{ printf("%8s %06x\n", xid[i], int(x.[ xid[i] ])); }
// Finally, we can use numerical identifiers
// Note: ''members'' function orders the list identifiers lexicographically, in other words
// instead of, say, 1,2,3,4,5,6,7,8,9,10,11 ''members'' returns 1,10,11,2,3,4,5,6,7,8,9
x = <<>>; // create an empty list
for (i in 1:5)
{ x.[i] = i; } // assign to the element of list ''i'' the real value equal to i.
You may also check:How to resolve the algorithm Doomsday rule step by step in the Raku programming language
You may also check:How to resolve the algorithm Filter step by step in the Erlang programming language
You may also check:How to resolve the algorithm Classes step by step in the Fortran programming language
You may also check:How to resolve the algorithm ABC problem step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Flow-control structures step by step in the Wren programming language