Collections: Difference between revisions
Content added Content deleted
Walterpachl (talk | contribs) 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 |
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, |
To store (say) a collection of numbers (or anything, |
||
for that matter) into a stemmed 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 |
||
⚫ | |||
y.= |
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.2012 /*the variable X will have a value of zero (0).*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
fib.0 = 0 /*this stemmed arrays will start with zero (0). */ |
|||
⚫ | |||
sawtooth.n=n |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
do n=-5 to 5 /*define a stemmed array from -5 to 5 */ |
|||
sawtooth.n = n /*the sawtooth array is, well, a sawtooth curve*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
This means that any index of the stemmed array must be positive to be useful for |
|||
storing numbers. |
|||
⚫ | |||
⚫ | |||
end of the array (if there aren't any ''null'' values): |
|||
⚫ | |||
⚫ | |||
end /*j*/ |
end /*j*/ |
||
/*at this point, J= |
/*at this point, J=16 (because of the DO */ |
||
/*loop incrementing the index. */ |
|||
⚫ | |||
===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: |
||
⚫ | |||
into a list: |
|||
primeList = 2 3 5 7 11 13 17 19 23 29 31 37 41 43 |
|||
⚫ | |||
/*in this case, the quotes (') can be |
/*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 |
say 'prime' j "is" word(primeList, j) |
||
⚫ | |||
/*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 (such as an |
|||
underbar/underscore <big>'''_'''</big> character) that |
|||
<br>that can be substituted, and then later, be translated to a true blank. |
|||
===sparse stemmed arrays=== |
===sparse stemmed arrays=== |
||
To store ( |
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 array.*/ |
|||
⚫ | |||
pr. |
pr.2 = 1 |
||
pr. |
pr.3 = 1 |
||
pr. |
pr.5 = 1 |
||
pr. |
pr.7 = 1 |
||
pr. |
pr.11 = 1 |
||
pr. |
pr.13 = 1 |
||
pr. |
pr.17 = 1 |
||
pr. |
pr.19 = 1 |
||
pr. |
pr.23 = 1 |
||
pr. |
pr.29 = 1 |
||
pr. |
pr.31 = 1 |
||
pr. |
pr.37 = 1 |
||
pr. |
pr.41 = 1 |
||
pr.43 = 1 |
|||
/*─────────────────────────────────────────────────────────────────────*/ |
|||
pr.47 = 1 |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
primes=0 |
primes=0 |
||
do j=1 for 10000 /*this method isn't very efficient. */ |
|||
if \pr.j then iterate /*Not prime? Then skip this element. */ |
|||
primes = primes + 1 /*bump the number of primes (counter).*/ |
|||
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 is not very efficient. */ |
|||
if pr.j\==0 then #primes = #primes + 1 /*Not zero? Bump the number of primes.*/ |
|||
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.*/ |
|||
⚫ | |||
say ' |
say 'The number of primes found in the list is ' Ps |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
/*─────────────────────────────────────────────────────────────────────*/ |
|||
highPrime= 47 /*hardcode the highest prime in array. */ |
|||
Ps = 0 |
|||
⚫ | |||
do k=1 for highPrime /*and much more efficient method. */ |
|||
if \pr.k 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 'number of primes found in the list is' Ps</lang> |
say 'The number of primes found in the list is: ' Ps</lang> |
||
The above '''do''' loop could've been coded as: |
|||
<lang rexx> |
|||
do k=1 to highPrime /*and much more efficient method. */</lang> |
|||
but a '''for''' clause is faster than a '''to''' clause. |
|||
<br><br> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |