Filter: Difference between revisions
Content added Content deleted
(Jakt) |
Walterpachl (talk | contribs) (→{{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 |
<syntaxhighlight lang="rexx">/*REXX program selects all even numbers from an array and puts them */ |
||
/* into a new array. */ |
|||
Parse Arg n seed . /* obtain optional arguments from CL*/ |
|||
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. */ |
|||
Call random,,seed /* use RANDOM seed for repeatability*/ |
|||
Do i=1 For n /* generate N random numbers */ |
|||
old.i=random(1,99999) /* generate random number */ |
|||
End |
|||
⚫ | |||
m=0 /* number of elements in NEW */ |
|||
Do j=1 To n /* process the elements of the OLD */ |
|||
If old.j//2==0 Then Do /* if element is even, then */ |
|||
m=m+1 /* bump the number of NEW elemens */ |
|||
new.m=old.j /* assign the number to the NEW */ |
|||
End |
|||
End |
|||
Do k=1 For m /* display all the NEW numbers. */ |
|||
Say right('new.'k,20) '=' right(new.k,9) |
|||
End </syntaxhighlight> |
|||
Programming note: the REXX statement |
|||
⚫ | |||
could've been replaced with |
|||
<syntaxhighlight lang="rexx"> if old.j//2 then iterate</syntaxhighlight> |
|||
but that would've assumed the numbers are integers (no matter what form they're expressed in).<br> |
|||
As it happens, the REXX program uses the numbers generated from the '''random''' BIF, which are integers. |
|||
{{out|output|text= when using the input of: <tt> , 1234567 </tt>}} |
{{out|output|text= when using the input of: <tt> , 1234567 </tt>}} |
||
The '''1234567''' is the '''random''' BIF ''seed'' so that the random numbers can be repeated when re-running the REXX program. |
The '''1234567''' is the '''random''' BIF ''seed'' 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=== |
|||
This version uses a control array (even.*) |
|||
⚫ | |||
This version uses a control array, which isn't fully populated (in REXX terms, a sparse array.) |
|||
/*REXX program uses a control array to tell which elements ars even. */ |
|||
Parse Arg n seed . /* obtain optional arguments from CL*/ |
|||
If n==''|n=="," Then n=50 /* Not specified? use the default */ |
|||
If datatype(seed,'W') Then |
|||
Call random,,seed /* use RANDOM seed for repeatability*/ |
|||
Do i=1 For n /* generate n random numbers */ |
|||
x.i=random(1,99999) /* generate random number */ |
|||
End |
|||
⚫ | |||
even.=0 /* all even bits are off */ |
|||
Do j=1 To n /* process the elements of x.* */ |
|||
If x.j//2==0 Then /* if element is even, then */ |
|||
⚫ | |||
end /*j*/ |
|||
End |
|||
Do k=1 To n /* display all the numbers */ |
|||
If even.k Then /* that are even */ |
|||
Say right('x.'k,20) '=' right(x.k,9) |
|||
End |
|||
end /*k*/ /*stick a fork in it, we're all done. */</syntaxhighlight> |
|||
{{out|output|text= when using the input of: <tt> 20 1234567 </tt>}} |
|||
<pre> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
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 ''new'' array. |
This version just uses one array to perform the filtering instead of creating a ''new'' array. |
||
The result is a sparse array. |
|||
This method doesn't need as much memory |
This method doesn't need as much memory. |
||
<syntaxhighlight lang="rexx">/*REXX program |
<syntaxhighlight lang="rexx">/*REXX program sets all elements containing odd numbers to blank */ |
||
Parse Arg n seed . /* obtain optional arguments from CL*/ |
|||
If n==''|n=="," Then n=50 /* Not specified? use the default */ |
|||
If datatype(seed,'W') Then |
|||
Call random,,seed /* use RANDOM seed for repeatability*/ |
|||
Do i=1 For n /* generate N random numbers */ |
|||
x.i=random(1,99999) /* generate random number */ |
|||
End |
|||
⚫ | |||
Do j=1 To n /* process the elements of x.* */ |
|||
If x.j//2<>0 Then /* if element is not even, then */ |
|||
Drop x.j /* delete it */ |
|||
End |
|||
end /*j*/ |
|||
Do k=1 To n /* display all the numbers */ |
|||
If datatype(x.k)='NUM' Then /* that are even */ |
|||
Say right('x.'k,20) '=' right(x.k,9) |
|||
⚫ | |||
End</syntaxhighlight> |
|||
say right('array.'k, 20) "=" right(@.k, 9) /*display a line (an array element). */ |
|||
⚫ | |||
end /*k*/ /*stick a fork in it, we're all done. */</syntaxhighlight> |
|||
⚫ | |||
⚫ | |||
{{out|output|text= is the same as the 1<sup>st</sup> REXX version (using two arrays).}} <br><br> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |