ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Iteration (https://www.excelbanter.com/excel-programming/400480-iteration.html)

Alan Beban[_2_]

Iteration
 
A repost that might clarify what's going on:

Is there a standard way of handling something like
the following?

Function DeleteColumn(inputArray, ColumnNumber)
If IsArray(ColumnNumber) Then
[Run the DeleteColumn function itself]
[on each column number in turn]
inputArray = arrOut
DeleteColumn = arrOut
Exit Function
Else
[code to create an array of the input]
[array less the single column number,]
[assigned to the variable arrOut]
inputArray = arrOut
DeleteColumn = arrOut
End If
End Function

I ended up creating a DeleteColumnAdjunct function
that is the same as the DeleteColumn function
except for name, and ran

Function DeleteColumn(inputArray, ColumnNumber)
If IsArray(ColumnNumber) Then
For w = UBound(ColumnNumber) To _
LBound(ColumnNumber) Step -1
arrOut = DeleteColumnAdjunct(inputArray, ColumnNumber(w))
Next
inputArray = arrOut
DeleteColumn = arrOut
Exit Function
Else
[code to create an array of the input]
[array less the single column number,]
[assigned to the variable arrOut]
inputArray = arrOut
DeleteColumn = arrOut
End If
End Function

It works, but I can't help thinking I'm missing the forest for the trees.

Thanks,
Alan Beban

Tim Williams

Iteration
 
Why not just wrap your main column-deleting code in a loop which loops
backwards through the ColumnNumber input?

If ColumnNumber isn't an array then this would take care of that case:

If Not IsArray(ColumnNumber) Then ColumnNumber=Array(ColumnNumber)
For x=ubound(ColumnNumber) to lbound(ColumnNumber) step -1
'delete column ColumnNumber(x) from inputArray
Next x

Easier than recursion.

However, if you have two functions with the same internal mechanism and
parameters then you don't need the second one: just have the first call
itself.

Tim




"Alan Beban" wrote in message
...
A repost that might clarify what's going on:

Is there a standard way of handling something like
the following?

Function DeleteColumn(inputArray, ColumnNumber)
If IsArray(ColumnNumber) Then
[Run the DeleteColumn function itself]
[on each column number in turn]
inputArray = arrOut
DeleteColumn = arrOut
Exit Function
Else
[code to create an array of the input]
[array less the single column number,]
[assigned to the variable arrOut]
inputArray = arrOut
DeleteColumn = arrOut
End If
End Function

I ended up creating a DeleteColumnAdjunct function
that is the same as the DeleteColumn function
except for name, and ran

Function DeleteColumn(inputArray, ColumnNumber)
If IsArray(ColumnNumber) Then
For w = UBound(ColumnNumber) To _
LBound(ColumnNumber) Step -1
arrOut = DeleteColumnAdjunct(inputArray, ColumnNumber(w))
Next
inputArray = arrOut
DeleteColumn = arrOut
Exit Function
Else
[code to create an array of the input]
[array less the single column number,]
[assigned to the variable arrOut]
inputArray = arrOut
DeleteColumn = arrOut
End If
End Function

It works, but I can't help thinking I'm missing the forest for the trees.

Thanks,
Alan Beban




Alan Beban[_2_]

Iteration
 
Tim Williams wrote:
Why not just wrap your main column-deleting code in a loop which loops
backwards through the ColumnNumber input?

If ColumnNumber isn't an array then this would take care of that case:

If Not IsArray(ColumnNumber) Then ColumnNumber=Array(ColumnNumber)
For x=ubound(ColumnNumber) to lbound(ColumnNumber) step -1
'delete column ColumnNumber(x) from inputArray
Next x

Easier than recursion.

However, if you have two functions with the same internal mechanism and
parameters then you don't need the second one: just have the first call
itself.

Tim


Thanks; somehow I had myself tangled up in my own underwear. I had
already tried the second above (it is the second snippet I posted below,
with the call to DeleteColumn itself instead of DeleteColumnAdjunct) and
for some reason didn't get it to work. But it does.

For the exercise, I also wrote it per your first suggestion above, which
also works.

Thanks for taking the time to address a silly posting.

Alan Beban




"Alan Beban" wrote in message
...

A repost that might clarify what's going on:

Is there a standard way of handling something like
the following?

Function DeleteColumn(inputArray, ColumnNumber)
If IsArray(ColumnNumber) Then
[Run the DeleteColumn function itself]
[on each column number in turn]
inputArray = arrOut
DeleteColumn = arrOut
Exit Function
Else
[code to create an array of the input]
[array less the single column number,]
[assigned to the variable arrOut]
inputArray = arrOut
DeleteColumn = arrOut
End If
End Function

I ended up creating a DeleteColumnAdjunct function
that is the same as the DeleteColumn function
except for name, and ran

Function DeleteColumn(inputArray, ColumnNumber)
If IsArray(ColumnNumber) Then
For w = UBound(ColumnNumber) To _
LBound(ColumnNumber) Step -1
arrOut = DeleteColumnAdjunct(inputArray, ColumnNumber(w))
Next
inputArray = arrOut
DeleteColumn = arrOut
Exit Function
Else
[code to create an array of the input]
[array less the single column number,]
[assigned to the variable arrOut]
inputArray = arrOut
DeleteColumn = arrOut
End If
End Function

It works, but I can't help thinking I'm missing the forest for the trees.

Thanks,
Alan Beban






All times are GMT +1. The time now is 01:18 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com