Task

Select certain elements from an Array into a new Array in a generic way.



To demonstrate, select all even numbers from an Array.

As an option, give a second solution which filters destructively,
by modifying the original Array rather than creating a new Array.




sRandom As New String[]
'______________________________________________________________________________________________________
Public Sub Main()
Dim siCount As Short
 
For siCount = 0 To 19
  sRandom.Add(Rand(1, 100))
Next
 
Print sRandom.join(",")
 
NewArray
Destructive
 
End
'______________________________________________________________________________________________________
Public Sub NewArray() 'Select certain elements from an array into a new array in a generic way
Dim sEven As New String[]
Dim siCount As Short
 
For siCount = 0 To sRandom.Max
  If Even(sRandom[siCount]) Then sEven.Add(sRandom[siCount])
Next
 
Print sEven.join(",")
 
End
'______________________________________________________________________________________________________
Public Sub Destructive() 'Give a second solution which filters destructively
Dim siIndex As New Short[]
Dim siCount As Short
 
For siCount = 0 To sRandom.Max
  If Odd(sRandom[siCount]) Then siIndex.Add(siCount)
Next
 
For siCount = siIndex.max DownTo 0
  sRandom.Extract(siIndex[siCount], 1)
Next
 
Print sRandom.join(",")
 
End