ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   clearing array contents (https://www.excelbanter.com/excel-programming/331073-clearing-array-contents.html)

Mark

clearing array contents
 
Hello.

I am working with an array to aid in summing the cost of a bill of materials
structure.

The array has an element for each level of the bill of materials, say level
1 to level 8. (sometimes higher, program defins it based on min and max found
in the level structure)

There are times in the summing process when I need to clear out part of the
array contents, say from level 5 to level 8. I can do that by using a
counter and a for loop, and clearing each one.

My question is, is there a one program line method to clear a group of the
array elements, something that would assign levArray(lev5, lev6, lev7, lev8)
all to 0 in one step?

Thanks.
Mark

Tom Ogilvy

clearing array contents
 
Highly doubtful.

--
Regards,
Tom Ogilvy


"mark" wrote in message
...
Hello.

I am working with an array to aid in summing the cost of a bill of

materials
structure.

The array has an element for each level of the bill of materials, say

level
1 to level 8. (sometimes higher, program defins it based on min and max

found
in the level structure)

There are times in the summing process when I need to clear out part of

the
array contents, say from level 5 to level 8. I can do that by using a
counter and a for loop, and clearing each one.

My question is, is there a one program line method to clear a group of the
array elements, something that would assign levArray(lev5, lev6, lev7,

lev8)
all to 0 in one step?

Thanks.
Mark




Mark

clearing array contents
 
Highly doubtful.


okay, thanks.

I could do it by tracking the levels in a group of spreadsheet cells,
instead of an array in VB... that way, I could clear whatever portion of the
group of cells I wanted.

I was just looking for a more efficient programming method (not looping
through if it wasn't needed)...

But what I have runs quickly anyway, so it will be fine to the user.

Thanks Tom.

Tushar Mehta

clearing array contents
 
What I would do, and you may already have done this, is put the code to
clear those levels in a separate subroutine. Then, whereever you need
to do the clearing, just put a one line call to that routine. If/when
you find a better way to do what you want to do all you will have to do
is update that one routine.

Depending on what you are doing, you could set up the array as a
dynamic array, then use
Redim preserve x(1 to 4)
redim preserve x(1 to 8)

Is it faster/cleaner than looping? Dunno. But, then, I wouldn't care
as long as the code was in the above mentioned subroutine where its
intent was obvious and its impact was isolated.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Highly doubtful.



okay, thanks.

I could do it by tracking the levels in a group of spreadsheet cells,
instead of an array in VB... that way, I could clear whatever portion of the
group of cells I wanted.

I was just looking for a more efficient programming method (not looping
through if it wasn't needed)...

But what I have runs quickly anyway, so it will be fine to the user.

Thanks Tom.


Mark

clearing array contents
 
What I would do, and you may already have done this, is put the code to
clear those levels in a separate subroutine.


Thanks, that's a good type of thing to do.

dynamic array, then use
Redim preserve x(1 to 4)
redim preserve x(1 to 8)


I think that is exactly what I was asking about, in the first place..
thanks, I'll try it.

(few minutes later... tried it)

well, I thought that was it and was all happy, but

My array is like this

dim levArray() as double

and then I use:

redim preserve levArray(levMin to levMax) to set up the number of elements
needed in the array, once the Min and Max level have been determined.
Usually, it's only Max that would change, but I wanted to make it flexible,
and it's possible that at some time the thing might be either 0 based or 1
based, so I made it find the levMin in the code, too.

But, the redim that you wrote, with only a portion of the elements being
redimmed, is giving an error. It isn't letting me redim just the upper part
of the array... I really need it to keep the lower elements anyway.

Thanks for the suggestion. I may work with a bit more.

Tom Ogilvy

clearing array contents
 
My answer was to a question about an array like this: (Your example, not
mine)
levArray(lev5, lev6, lev7, lev8)

representing some ill defined hierarchy. If you only have a one dimensional
array, then disregard what I said.

But you can't redim a part of an array in a single dimension to clear it.
However, looping through an array is extremely fast, so you may be attacking
something that isn't a problem at all.


--
Regards,
Tom Ogilvy

"mark" wrote in message
...
What I would do, and you may already have done this, is put the code to
clear those levels in a separate subroutine.


Thanks, that's a good type of thing to do.

dynamic array, then use
Redim preserve x(1 to 4)
redim preserve x(1 to 8)


I think that is exactly what I was asking about, in the first place..
thanks, I'll try it.

(few minutes later... tried it)

well, I thought that was it and was all happy, but

My array is like this

dim levArray() as double

and then I use:

redim preserve levArray(levMin to levMax) to set up the number of elements
needed in the array, once the Min and Max level have been determined.
Usually, it's only Max that would change, but I wanted to make it

flexible,
and it's possible that at some time the thing might be either 0 based or 1
based, so I made it find the levMin in the code, too.

But, the redim that you wrote, with only a portion of the elements being
redimmed, is giving an error. It isn't letting me redim just the upper

part
of the array... I really need it to keep the lower elements anyway.

Thanks for the suggestion. I may work with a bit more.




Tushar Mehta

clearing array contents
 
Yes, when you use redim preserve you have to keep the lower bound
unchanged. But, since in your case you anyway want to throw away only
the values in the upper region of the array this technique should work.

However, and as Tom has already pointed out, looping through an array
is very fast. I suspect for even a large 1D array it is much faster to
loop and clear the contents than use redim's.

But that's not the point either. The primary purpose of good code is
to first and foremost be correct and then be easy to understand and
maintain while meeting the implicit or explicit performance
requirements.

As I pointed out, I routinely use a separate procedure to write highly
modular code. When warranted, I also express a short and obvious idea
that requires multiple lines of code by putting those statements on a
single line.

In the code below *each* line contains a complete intent.

Sub testIt3()
Dim x() As Double, lb As Integer, ub As Integer, _
i As Integer
lb = 2: ub = 10: ReDim x(lb To ub)

For i = lb To ub: x(i) = i: Next i
ReDim Preserve x(lb To ub - 4): ReDim Preserve x(lb To ub)
For i = lb To ub: Debug.Print x(i): Next i

End Sub

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
What I would do, and you may already have done this, is put the code to
clear those levels in a separate subroutine.


Thanks, that's a good type of thing to do.

dynamic array, then use
Redim preserve x(1 to 4)
redim preserve x(1 to 8)


I think that is exactly what I was asking about, in the first place..
thanks, I'll try it.

(few minutes later... tried it)

well, I thought that was it and was all happy, but

My array is like this

dim levArray() as double

and then I use:

redim preserve levArray(levMin to levMax) to set up the number of elements
needed in the array, once the Min and Max level have been determined.
Usually, it's only Max that would change, but I wanted to make it flexible,
and it's possible that at some time the thing might be either 0 based or 1
based, so I made it find the levMin in the code, too.

But, the redim that you wrote, with only a portion of the elements being
redimmed, is giving an error. It isn't letting me redim just the upper part
of the array... I really need it to keep the lower elements anyway.

Thanks for the suggestion. I may work with a bit more.


Mark

clearing array contents
 
My answer was to a question about an array like this: (Your example, not
mine) levArray(lev5, lev6, lev7, lev8)


Sorry.. it looks like I must have written the original question in an
unclear fashion.

The paragraph before said:
"clear out part of the array contents, say from level 5 to level 8."

I meant that in that example, elements 1 to 4 would remain as they were.

Good night.

Mark

clearing array contents
 
But that's not the point either. The primary purpose of good code is
to first and foremost be correct and then be easy to understand and
maintain while meeting the implicit or explicit performance
requirements.



No argument from me, there.

Modular code is wonderful stuff.

Most of the non-project specific stuff I do goes into a totally reusable,
callable module.

Where it belongs.

Mark

clearing array contents
 
Yes, when you use redim preserve you have to keep the lower bound
unchanged. But, since in your case you anyway want to throw away only
the values in the upper region of the array this technique should work.


I didn't want to get rid of the dimensioned element, just it's value.

The way I have it, the redim only rusn once... after the level count is
determined.

Thanks for the suggestions.

Good night.

Alan Beban[_2_]

clearing array contents
 
mark wrote:
Yes, when you use redim preserve you have to keep the lower bound
unchanged. But, since in your case you anyway want to throw away only
the values in the upper region of the array this technique should work.



I didn't want to get rid of the dimensioned element, just it's value.

The way I have it, the redim only rusn once... after the level count is
determined.

Thanks for the suggestions.

Good night.

It still isn't entirely clear exactly what you have been doing, but if
you mean to "clear" only the upper levels of a 1-D array you can do it
simply with ReDim Preserve. And if you originally asign the array to a
variant variable, you can change the lower bound with Redim Preserve as
well.
E.g.:

Sub test1()
Dim arr
ReDim arr(1 To 8) As Double
For i = 1 To 8
arr(i) = i
Next
'the array now has 1 thru 8 in arr(1) thru arr(8)
ReDim Preserve arr(1 To 4)
'the array now has 1 thru 4 in arr(1) thru arr(4)
ReDim Preserve arr(1 To 8)
'the array now has 1 thru 4 in arr(1) thru arr(4)
'and 0's in arr(5) thru arr(8)
ReDim Preserve arr(0 To 3)
'the array now has 1 thru 4 in arr(0) thru arr(3)
ReDim Preserve arr(0 To 7)
'the array now has 1 thru 4 in arr(0) thru arr(3)
'and 0's in arr(4) thru arr(7)
End Sub

Alan Beban

Tushar Mehta

clearing array contents
 
Interesting, Alan. Yet another exotic use for a variant.

Of course all this is limited to VB6 since .Net doesn't support
anything but zero as a lower bound.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Sub test1()
Dim arr
ReDim arr(1 To 8) As Double
For i = 1 To 8
arr(i) = i
Next
'the array now has 1 thru 8 in arr(1) thru arr(8)
ReDim Preserve arr(1 To 4)
'the array now has 1 thru 4 in arr(1) thru arr(4)
ReDim Preserve arr(1 To 8)
'the array now has 1 thru 4 in arr(1) thru arr(4)
'and 0's in arr(5) thru arr(8)
ReDim Preserve arr(0 To 3)
'the array now has 1 thru 4 in arr(0) thru arr(3)
ReDim Preserve arr(0 To 7)
'the array now has 1 thru 4 in arr(0) thru arr(3)
'and 0's in arr(4) thru arr(7)
End Sub


Mark

clearing array contents
 
ReDim Preserve arr(1 To 4)
'the array now has 1 thru 4 in arr(1) thru arr(4)
ReDim Preserve arr(1 To 8)
'the array now has 1 thru 4 in arr(1) thru arr(4)
'and 0's in arr(5) thru arr(8)



Hi, Alan,

Thanks for the thought... that looks like it would accomplish what I was
originally looking for.

Alan Beban[_2_]

clearing array contents
 
mark wrote:
ReDim Preserve arr(1 To 4)
'the array now has 1 thru 4 in arr(1) thru arr(4)
ReDim Preserve arr(1 To 8)
'the array now has 1 thru 4 in arr(1) thru arr(4)
'and 0's in arr(5) thru arr(8)




Hi, Alan,

Thanks for the thought... that looks like it would accomplish what I was
originally looking for.


I'm not sure how that relates to your comments about 0-based and
1-based. If you aren't changing the lower bound of the array it isn't
necessary to assign it to a variant variable.

Dim arr() As Double
ReDim arr(1 to 8) etc.

would do fine.

Alan Beban

Mark

clearing array contents
 
I'm not sure how that relates to your comments about 0-based and
1-based. If you aren't changing the lower bound of the array it isn't
necessary to assign it to a variant variable.



Hi, Alan. Thanks for the interest.

The 0 or 1 based statement only meant that I'm not sure that the input will
always be one or the other, on different inputs. For each single run, it
will be one or the other, and not change in run time.

My version that I wrote Saturday is at work, and I'm not... I'm pretty sure
I defined it as:

levArray() as double

and then
Redim Preserve (levMin to levMax)

As I mentioned near the beginning, as I am summing the structure, there are
places where it needs to clear the lower level (in the bill of materials)
cost... for instance, when it reaches a level 4 item in the structure, it
needs to clear levels 4 through 8... set them to 0. I did that originally
with a for loop and a counter, but it seemed like a lot of extra looping.

as Tom pointed out yesterday, looping through and array is fast... in fact,
I tested a 10,000 line 8 level structure, and it does it in a second or so.
Perfectly fine from the user's standpoint, and mine, timewise.

You showed me how to clear the upper elements of the array, yesterday

for instance, if levMin is 1, and the current level, levCur is 4, and levMax
is 8

Redim Preserve(levMin to levCur)
Redim Preserve(levMin to levMax)

would leave the array with the contents of the lower 4 elements intact, and
elements 5, 6, 7, and 8 redimmed to null, or 0... either is fine.

I didn't mean for my question to be unclear. Sorry.

Everything is working well.
Thanks

Alan Beban[_2_]

clearing array contents
 
Thanks for the feedback

Alan Beban


All times are GMT +1. The time now is 09:01 AM.

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