Iterate through a set of arrays

I have six arrays FX(), PH(), SR(), VT(), PB(), HB(). I need to do the same work on each array of integers.

To make the code simple I’d like to iterate through the six arrays with something like a for-next loop.

Any ideas? I’ve tried an array of arrays and don’t want to iterate through numerous properties to find the six.

not enough info about what you want to do.
Is


for x as integer = 0 to somenumber
//handle FX(x)
//handle PH(x)
//handle VT(x)
//and so on
next

not what you want to do?

2 Likes

This is what that creates:

for j as integer = 0 to 5
select case j
case 0
FX.sortwith(name, level)

  for i as integer = 0 to name.lastIndex
    if FX(i) > 0 then addToPrintArray name(i), level(i), j, i, competition
    
  next i
  
case 1
  PH.sortwith(name, level)
  
  for i as integer = 0 to name.lastIndex
    if PH(i) > 0 then addToPrintArray name(i), level(i), j, i, competition
    
  next i
  
case 2
  SR.sortwith(name, level)
  
  for i as integer = 0 to name.lastIndex
    if SR(i) > 0 then addToPrintArray name(i), level(i), j, i, competition
    
  next i
  
case 3
  VT.sortwith(name, level)
  
  for i as integer = 0 to name.lastIndex
    if VT(i) > 0 then addToPrintArray name(i), level(i), j, i, competition
    
  next i
  
case 4
  PB.sortwith(name, level)
  
  for i as integer = 0 to name.lastIndex
    if PB(i) > 0 then addToPrintArray name(i), level(i), j, i, competition
    
  next i
  
case 5
  HB.sortwith(name, level)
  
  for i as integer = 0 to name.lastIndex
    if HB(i) > 0 then addToPrintArray name(i), level(i), j, i, competition
    
  next i
  
end select

next j

Add them to an Array() of Variant

Var FX() As Integer = Array(1, 2, 3)
Var PH() As Integer = Array(4, 5, 6)
Var SR() As Integer = Array(7, 8, 9)

Var variantArray() As Variant

variantArray.Add FX
variantArray.Add PH
variantArray.Add SR

For Each v As Variant In variantArray
  
  Var currentArray() As Integer = v
  
  For Each i As Integer In currentArray
    
    // Your code here
    
  Next
  
Next
1 Like

There it is. The issue is that variantArray = array(FX(), PH() . . .) doesn’t work.

you have to use .add FX, .add PH . . .

thanks

If you have problems like that then you usually have a code smell and it’s time for a refactor. Like make a class out of the arrays. But it really depends on what you are doing with the arrays. Perhaps you need an interface or an iterator.

You can create a method that get the array as argument
Like:

//name, level and competion exist in the same scope otherwise you have to pass them as arguments
sub sortAndCheck(values() as integer, j as integer)
  values.sortWith(name, level)
  for i as integer=0 to name.lastIndex //you should check that values and name have the same count
    if values(i)>0 then addToPrintArray name(i), level(i), j, i, competition
  next
end sub

//you can call directly
sortAndCheck FX, 0
sortAndCheck PH, 1
// and so on
//or use the case solution
for j as integer=0 to 5
  var toCheck() as integer
  toCheck=nil
  select case j
    case 0
      toCheck=FX
    case 1
      toCheck=PH
//....
  end select
  if toSort<>nil then //just to avoid a missing case
    sortAndCheck toSort, j
  end if
next