Filter: Difference between revisions

Content added Content deleted
(Jakt)
(→‎{{header|REXX}}: refurbished and corrected)
Line 3,296:
===using two arrays===
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.*/
parse/* arginto Na seednew array. /*obtain optional arguments from the CL */
ifParse N==''Arg |n N==","seed then N= 50. /*Not specified?obtain optional Thenarguments usefrom the default.CL*/
ifIf datatype(seed,n=='W')|n=="," Then n=50 then call random ,,seed /* Not specified? use the RANDOM seeddefault for repeatability*/
If datatype(seed,'W') Then
old.= /*the OLD array, all are null so far. */
new.= Call random,,seed /* " NEW " " " " " use "RANDOM seed for repeatability*/
doDo i=1 For forn N /* generate N random numbers ──► OLD */
old.i= random(1, 99999) /* generate random number 1 ──► 99999 */
End
end /*i*/
#m= 0 /* number of elements in NEW (so far). */
doDo j=1 To for N n /* process the elements of the OLD array */
ifIf old.j//2 \== 0 Then then iterate Do /* if element isn'tis even, then skip it. */
#m= # m+ 1 /* bump the number of NEW elements.elemens */
new.#m= old.j /* assign the number to the NEW array. */
end /*j*/End
End
 
doDo k=1 For for # m /* display all the NEW numbers. */
saySay right('new.'k, 20) "'="' right(new.k, 9) /*display a line (an array element). */
End end /*k*/ /*stick a fork in it, we're all done. */</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>}}
 
The '''1234567''' is the '''random''' BIF &nbsp; ''seed'' &nbsp; so that the random numbers can be repeated when re-running the REXX program.
<pre>
Line 3,350 ⟶ 3,342:
new.23 = 44360
</pre>
===Using a control array===
 
===usingThis oneversion array withuses a control array=== (even.*)
<syntaxhighlight lang="rexx"> if old.j//2 \== 0 then iterate</syntaxhighlight>
This version uses a control array, which isn't fully populated &nbsp; (in REXX terms, a sparse array.)
<syntaxhighlight lang="rexx">/*REXX program findsuses alla even numbers from ancontrol array, to andtell which markselements aars control arrayeven. */
parseParse argArg Nn seed . /* obtain optional arguments from the CL*/
ifIf Nn=='' | Nn=="," Then then Nn= 50 /* Not specified? Then use the default. */
ifIf datatype(seed,'W') then call random ,,seed /*use the RANDOM seed for repeatability*/Then
Call random,,seed /* use RANDOM seed for repeatability*/
 
doDo i=1 For forn N /* generate n random numbers N random numbers ──► OLD */
@x.i= random(1, 99999) /* generate random number 1 ──► 99999 */
End
end /*i*/
!even.= 0 /* all even bits are off /*number of elements in NEW (so far).*/
doDo j=1 To for N n /* process the elements OLDof x.* array elements. */
If if @x.j//2 \==0 Then then !.j= 1 /* if element is /*markeven, thethen ! array that it's ¬even. */
if @even.kj==''1 then iterate /*if it'sturn markedon notthe even, thenbit skip it*/
end /*j*/
End
 
doDo k=1 To for N n /* display all the numbers @ even numbers. */
If even.k Then if !.k then iterate /* that are even /*if it's marked as not even, skip it.*/
saySay right('arrayx.'k, 20) "'="' right(@x.k, 9) /*display a even number, filtered array*/
End
end /*k*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
For{{out|output|text=&nbsp; thewhen followingusing the input of: &nbsp; &nbsp; <tt> ,20 &nbsp; 1234567 </tt>}}
<pre>
 
end x.3 /*i*/= 52754
{{out|output|text=&nbsp; Output is the same as the 1<sup>st</sup> REXX version &nbsp; (using two arrays).}}<br>
end x.5 /*i*/= 94296
 
end x.6 /*i*/= 2068
 
x.13 = 71494
x.14 = 71628
x.15 = 47404
x.19 = 92502
x.20 = 24808</pre>
===using one array, destructive===
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.
<syntaxhighlight lang="rexx">/*REXX program findssets all elements evencontaining odd numbers fromto anblank array, and marks the not even numbers.*/
parseParse argArg Nn seed . /* obtain optional arguments from the CL*/
ifIf Nn=='' | Nn=="," Then then Nn= 50 /* Not specified? Then use the default. */
ifIf datatype(seed,'W') then call random ,,seed /*use the RANDOM seed for repeatability*/Then
Call random,,seed /* use RANDOM seed for repeatability*/
 
doDo i=1 For forn N /* generate N random numbers ──► OLD */
@x.i= random(1, 99999) /* generate a random number 1 ──► 99999 */
End
end /*i*/
Do j=1 To n /* process the elements of x.* */
 
If do x.j=1 for N //2<>0 Then /*process theif element OLDis not arrayeven, then elements. */
Drop if @x.j//2 \==0 then @.j= /*mark thedelete it @ array that it's not even*/
End
end /*j*/
Do k=1 To n /* display all the numbers */
 
If do datatype(x.k)=1'NUM' Then for N /* that are even /*display all the @ even numbers. */
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; Output is the same as the 12<sup>stnd</sup> REXX version &nbsp; (using two arrays).}} <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}}==