Filter: Difference between revisions

Content added Content deleted
(Jakt)
(→‎{{header|REXX}}: refurbished and corrected)
Line 3,296: Line 3,296:
===using two arrays===
===using two arrays===
This example uses two arrays.   The   '''random'''   BIF is used to generate the numbers.
This example uses two arrays.   The   '''random'''   BIF is used to generate the numbers.
<syntaxhighlight lang="rexx">/*REXX program selects all even numbers from an array and puts them ──► a new array.*/
<syntaxhighlight lang="rexx">/*REXX program selects all even numbers from an array and puts them */
parse arg N seed . /*obtain optional arguments from the CL*/
/* into a new array. */
if N=='' | N=="," then N= 50 /*Not specified? Then use the default.*/
Parse Arg n seed . /* obtain optional arguments from CL*/
if datatype(seed,'W') then call random ,,seed /*use the RANDOM seed for repeatability*/
If n==''|n=="," Then n=50 /* Not specified? use the default */
If datatype(seed,'W') Then
old.= /*the OLD array, all are null so far. */
new.= /* " NEW " " " " " " */
Call random,,seed /* use RANDOM seed for repeatability*/
do i=1 for N /*generate N random numbers ──► OLD */
Do i=1 For n /* generate N random numbers */
old.i= random(1, 99999) /*generate random number 1 ──► 99999*/
old.i=random(1,99999) /* generate random number */
End
end /*i*/
#= 0 /*number of elements in NEW (so far).*/
m=0 /* number of elements in NEW */
do j=1 for N /*process the elements of the OLD array*/
Do j=1 To n /* process the elements of the OLD */
if old.j//2 \== 0 then iterate /*if element isn't even, then skip it.*/
If old.j//2==0 Then Do /* if element is even, then */
#= # + 1 /*bump the number of NEW elements. */
m=m+1 /* bump the number of NEW elemens */
new.#= old.j /*assign the number to the NEW array.*/
new.m=old.j /* assign the number to the NEW */
end /*j*/
End
End

do k=1 for # /*display all the NEW numbers. */
Do k=1 For m /* display all the NEW numbers. */
say right('new.'k, 20) "=" right(new.k, 9) /*display a line (an array element). */
Say right('new.'k,20) '=' right(new.k,9)
end /*k*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
End </syntaxhighlight>
Programming note: &nbsp; the REXX statement
<syntaxhighlight lang="rexx"> if old.j//2 \== 0 then iterate</syntaxhighlight>
could've been replaced with
<syntaxhighlight lang="rexx"> if old.j//2 then iterate</syntaxhighlight>
but that would've assumed the numbers are integers &nbsp; (no matter what form they're expressed in).<br>
As it happens, the REXX program uses the numbers generated from the &nbsp; '''random''' &nbsp; BIF, &nbsp; which are integers.

{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> , &nbsp; 1234567 </tt>}}
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> , &nbsp; 1234567 </tt>}}

The '''1234567''' is the '''random''' BIF &nbsp; ''seed'' &nbsp; so that the random numbers can be repeated when re-running the REXX program.
The '''1234567''' is the '''random''' BIF &nbsp; ''seed'' &nbsp; so that the random numbers can be repeated when re-running the REXX program.
<pre>
<pre>
Line 3,350: Line 3,342:
new.23 = 44360
new.23 = 44360
</pre>
</pre>
===Using a control array===

===using one array with a control array===
This version uses a control array (even.*)
<syntaxhighlight lang="rexx"></syntaxhighlight>
This version uses a control array, which isn't fully populated &nbsp; (in REXX terms, a sparse array.)
<syntaxhighlight lang="rexx">/*REXX program finds all even numbers from an array, and marks a control array. */
/*REXX program uses a control array to tell which elements ars even. */
parse arg N seed . /*obtain optional arguments from the CL*/
Parse Arg n seed . /* obtain optional arguments from CL*/
if N=='' | N=="," then N= 50 /*Not specified? Then use the default.*/
If n==''|n=="," Then n=50 /* Not specified? use the default */
if datatype(seed,'W') then call random ,,seed /*use the RANDOM seed for repeatability*/
If datatype(seed,'W') Then
Call random,,seed /* use RANDOM seed for repeatability*/

do i=1 for N /*generate N random numbers ──► OLD */
Do i=1 For n /* generate n random numbers */
@.i= random(1, 99999) /*generate random number 1 ──► 99999*/
x.i=random(1,99999) /* generate random number */
End
end /*i*/
!.= 0 /*number of elements in NEW (so far).*/
even.=0 /* all even bits are off */
do j=1 for N /*process the OLD array elements. */
Do j=1 To n /* process the elements of x.* */
if @.j//2 \==0 then !.j= 1 /*mark the ! array that it's ¬even. */
If x.j//2==0 Then /* if element is even, then */
even.j=1 /* turn on the even bit */
end /*j*/
End

do k=1 for N /*display all the @ even numbers. */
Do k=1 To n /* display all the numbers */
if !.k then iterate /*if it's marked as not even, skip it.*/
If even.k Then /* that are even */
say right('array.'k, 20) "=" right(@.k, 9) /*display a even number, filtered array*/
Say right('x.'k,20) '=' right(x.k,9)
End
end /*k*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
For the following input: &nbsp; &nbsp; <tt> , 1234567 </tt>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 20 &nbsp; 1234567 </tt>}}
<pre>

x.3 = 52754
{{out|output|text=&nbsp; Output is the same as the 1<sup>st</sup> REXX version &nbsp; (using two arrays).}}<br>
x.5 = 94296

x.6 = 2068

x.13 = 71494
x.14 = 71628
x.15 = 47404
x.19 = 92502
x.20 = 24808</pre>
===using one array, destructive===
===using one array, destructive===
This version just uses one array to perform the filtering instead of creating a &nbsp; ''new'' &nbsp; array.
This version just uses one array to perform the filtering instead of creating a &nbsp; ''new'' &nbsp; array.
The result is a sparse array.

This method doesn't need as much memory to hold the sparse array.
This method doesn't need as much memory.
<syntaxhighlight lang="rexx">/*REXX program finds all even numbers from an array, and marks the not even numbers.*/
<syntaxhighlight lang="rexx">/*REXX program sets all elements containing odd numbers to blank */
parse arg N seed . /*obtain optional arguments from the CL*/
Parse Arg n seed . /* obtain optional arguments from CL*/
if N=='' | N=="," then N= 50 /*Not specified? Then use the default.*/
If n==''|n=="," Then n=50 /* Not specified? use the default */
if datatype(seed,'W') then call random ,,seed /*use the RANDOM seed for repeatability*/
If datatype(seed,'W') Then
Call random,,seed /* use RANDOM seed for repeatability*/

do i=1 for N /*generate N random numbers ──► OLD */
Do i=1 For n /* generate N random numbers */
@.i= random(1, 99999) /*generate a random number 1 ──► 99999 */
x.i=random(1,99999) /* generate random number */
End
end /*i*/
Do j=1 To n /* process the elements of x.* */

do j=1 for N /*process the OLD array elements. */
If x.j//2<>0 Then /* if element is not even, then */
if @.j//2 \==0 then @.j= /*mark the @ array that it's not even*/
Drop x.j /* delete it */
End
end /*j*/
Do k=1 To n /* display all the numbers */

do k=1 for N /*display all the @ even numbers. */
If datatype(x.k)='NUM' Then /* that are even */
Say right('x.'k,20) '=' right(x.k,9)
if @.k=='' then iterate /*if it's marked not even, then skip it*/
End</syntaxhighlight>
say right('array.'k, 20) "=" right(@.k, 9) /*display a line (an array element). */
For the following input: &nbsp; &nbsp; <tt> 20 1234567 </tt>
end /*k*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; is the same as the 2<sup>nd</sup> REXX version.}} <br><br>
For the following input: &nbsp; &nbsp; <tt> , 1234567 </tt>

{{out|output|text=&nbsp; is the same as the 1<sup>st</sup> REXX version &nbsp; (using two arrays).}} <br><br>


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