Collections: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: corrected an obvious typo)
(→‎{{header|REXX}}: reworked a lot of the REXX programs and accompanying text.)
Line 2,687: Line 2,687:


=={{header|REXX}}==
=={{header|REXX}}==
There are several ways to store collections in REXX:
There are several methods to store collections in REXX:
* stemmed arrays
::::*   stemmed arrays   (or simply, arrays)
* lists or vectors
::::*   lists   or   vectors
* sparse stemmed arrays
::::*   sparse stemmed arrays;   (or simply, sparse arrays)

A collection can be numbers   (integer or floating point numbers),   or any
character strings   (including ''nulls'').

Indeed, even numbers in REXX are stored as characters.

===stemmed arrays===
===stemmed arrays===
To store (say) a collection of numbers (or anything, for that matter) into a stemmed array:
To store (say) a collection of numbers   (or anything,
<lang rexx>pr.=0 /*define a default for all elements for the stemmed array.*/
for that matter) &nbsp; into a stemmed array:
<lang rexx>pr. = /*define a default for all elements for the array*/


pr.1 =2 /*note that this array starts at 1 (one). */
pr.1 = 2 /*note that this array starts at 1 (one). */
pr.2 =3
pr.2 = 3
pr.3 =5
pr.3 = 5
pr.4 =7
pr.4 = 7
pr.5 =11
pr.5 = 11
pr.6 =13
pr.6 = 13
pr.7 =17
pr.7 = 17
pr.8 =19
pr.8 = 19
pr.9 =23
pr.9 = 23
pr.10=29
pr.10 = 29
pr.11=31
pr.11 = 31
pr.12=37
pr.12 = 37
pr.13=41
pr.13 = 41
pr.14=43
pr.14 = 43
pr.15 = 47


y.=0 /*define a default for all years. */
y. = 0 /*define a default for all years (Y) to be zero*/
y.1985 = 6020
y.1985 = 6020
y.1986 = 7791
y.1986 = 7791
y.1987 = 8244
y.1987 = 8244
y.1988 = 10075
y.1988 = 10075
x = y.2000 /*X will have a value of zero (0). */


fib.0 = 0 /*arrays may start with zero (0). */
x = y.2012 /*the variable X will have a value of zero (0).*/
fib.1 = 1
fib.2 = 1
fib.3 = 2
fib.4 = 3
fib.5 = 5
fib.6 = 8
fib.7 =17


do n=-5 to 5 /*define an array from -5 ──► 5 */
fib.0 = 0 /*this stemmed arrays will start with zero (0). */
fib.1 = 1
sawtooth.n=n
fib.2 = 1
end /*n*/ /*eleven elements will be defined. */</lang>
fib.3 = 2
Most often, programmers will assign the zeroeth entry to the
fib.4 = 3
number of elements in the stemmed array:
fib.5 = 5
<lang ress> pr.0=14 /*number of entries in the stemmed array.*/</lang>
fib.6 = 8
Programatically, a simple test could be performed to detect the end of the array (if there aren't any null values):
fib.7 = 17
<lang rexx> do j=1 while pr.j\==0

say 'prime' j 'is' pr.j
do n=-5 to 5 /*define a stemmed array from -5 to 5 */
sawtooth.n = n /*the sawtooth array is, well, a sawtooth curve*/
end /*n*/ /*note that eleven elements will be defined. */</lang>
Most often, programmers will assign the &nbsp; zero &nbsp; entry to the
number of elements in the stemmed array.

This means that any index of the stemmed array must be positive to be useful for
storing numbers.
<lang rexx> pr.0= 15 /*number of (data) entries in the stemmed array. */</lang>
Programmatically, a simple test could be performed to detect the
end of the array &nbsp; (if there aren't any &nbsp; ''null'' &nbsp; values):
<lang rexx> do j=1 while pr.j\==''
say 'prime' j "is" pr.j
end /*j*/
end /*j*/
/*at this point, J=15. */
/*at this point, J=16 (because of the DO */
j=j-1 /*J now has the count of primes stored.*/</lang>
/*loop incrementing the index. */
j= j-1 /*J now has the count of primes stored. */</lang>


===lists or vectors===
===lists or vectors===
To store (say) a collection of numbers (or anything, for that matter)
To store (say) a collection of numbers (or anything, for that matter) into a list:
<lang rexx>primeList = '2 3 5 7 11 13 17 19 23 29 31 37 41 43' /* or ··· */
into a list:
<lang rexx>primeList='2 3 5 7 11 13 17 19 23 29 31 37 41 43' /* or ... */
primeList = 2 3 5 7 11 13 17 19 23 29 31 37 41 43
primeList= 2 3 5 7 11 13 17 19 23 29 31 37 41 43


/*in this case, the quotes (') can be dropped. */
/*in this case, the quotes (') can be elided.*/


primes=words(primeList)
primes= words(primeList) /*the WORDS BIF counts the number of blank─ */
/*separated words (in this case, prime numbers)*/
/*in the value of the variable "primeList".*/


do j=1 for primes /*can also be coded: do j=1 to primes */
do j=1 for primes /*can also be coded as: do j=1 to primes */
say 'prime' j 'is' word(primeList,j)
say 'prime' j "is" word(primeList, j)
/*this method (using the WORD BIF) isn't */
/*very efficient for very large arrays (those */
/*with many many thousands of elements). */
end /*j*/</lang>
end /*j*/</lang>
The use of lists (in the above manner) is suitable for words (or numbers) that do not have
leading, embedded, or
<br>trailing blanks as part of their value.

One workaround is to use some other unique character &nbsp; (such as an
underbar/underscore &nbsp; <big>'''_'''</big> &nbsp; character) &nbsp; that
<br>that can be substituted, and then later, be translated to a true blank.

===sparse stemmed arrays===
===sparse stemmed arrays===
To store (say) a collection of numbers (or anything, for that matter) into a sparse stemmed array:
To store (for instance) a collection of numbers (or anything, for that matter) into
a sparse stemmed array:
<lang rexx>pr.=0 /*define a default for all elements for the stemmed array.*/
<lang rexx>pr. = 0 /*define a default for all elements for the array.*/
pr.2 =1
pr.3 =1
pr.2 = 1
pr.5 =1
pr.3 = 1
pr.7 =1
pr.5 = 1
pr.11=1
pr.7 = 1
pr.13=1
pr.11 = 1
pr.17=1
pr.13 = 1
pr.19=1
pr.17 = 1
pr.23=1
pr.19 = 1
pr.29=1
pr.23 = 1
pr.31=1
pr.29 = 1
pr.37=1
pr.31 = 1
pr.41=1
pr.37 = 1
pr.43=1
pr.41 = 1
pr.43 = 1
/*─────────────────────────────────────────────────────────────────────*/
pr.47 = 1
/*──────────────────────────────────────────────────────────────────────────────────────*/
primes=0
primes=0
do j=1 for 10000 /*this method isn't very efficient*/
do j=1 for 10000 /*this method isn't very efficient. */
if pr.j==0 then iterate
if \pr.j then iterate /*Not prime? Then skip this element. */
primes = primes+1
primes = primes + 1 /*bump the number of primes (counter).*/
end /*j*/
end /*j*/
/*note that the 10000 is a small "∞".*/

say '# of primes in list:' primes
say '# of primes in list:' primes
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*─────────────────────────────────────────────────────────────────────*/
#primes=0
#primes=0
do j=1 for 10000 /*this method isn't very efficient*/
do j=1 for 10000 /*this method is not very efficient. */
if pr.j\==0 then #primes = #primes+1
if pr.j\==0 then #primes = #primes + 1 /*Not zero? Bump the number of primes.*/
end /*j*/
end /*j*/ /* [↑] not as idiomatic as 1st program*/

say '# of primes in the list:' #primes
/*──────────────────────────────────────────────────────────────────────────────────────*/
Ps=0
do k=1 for 10000 /*and yet another inefficient method. */
if pr.k==0 then iterate /*Not a prime? Then skip this element.*/
Ps = Ps + 1 /*bump the counter for the # of primes.*/
say 'prime' Ps "is:" k /*might as well echo this prime number.*/
end /*k*/


say '# of primes in list:' #primes
say 'The number of primes found in the list is ' Ps
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*─────────────────────────────────────────────────────────────────────*/
s=0 /*yet another inefficient method. */
highPrime= 47 /*hardcode the highest prime in array. */
Ps = 0
do k=1 for 10000 /*this method isn't very efficient*/
Ps = Ps + (pn.k\==0) /*more obtuse, if ya like that. */
do k=1 for highPrime /*and much more efficient method. */
say 'prime' Ps "is:" k /*might as well echo the prime #. */
if \pr.k then iterate /*Not a prime? Then skip this element.*/
Ps = Ps + 1 /*bump the counter for the # of primes.*/
end /*k*/
say 'prime' Ps "is:" k /*might as well echo this prime number.*/
end /*k*/


say 'number of primes found in the list is' Ps</lang>
say 'The number of primes found in the list is: ' Ps</lang>
The above &nbsp; '''do''' &nbsp; loop could've been coded as:
<lang rexx>
do k=1 to highPrime /*and much more efficient method. */</lang>
but a &nbsp; '''for''' &nbsp; clause is faster than a &nbsp; '''to''' &nbsp; clause.
<br><br>


=={{header|Ring}}==
=={{header|Ring}}==