By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,456 Members | 1,952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,456 IT Pros & Developers. It's quick & easy.

Determining the highest array value without cycling through

P: n/a
Hello,

Is there an easy way to determine the highest point in an array that
contains a value? I'm dimensioning an array to hold up to 255 items, but if
it only contains three, I don't want to cycle through all 255 if that's
possible.

any ideas?
Thanks!
Nov 13 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Jozef wrote:
Hello,

Is there an easy way to determine the highest point in an array that
contains a value? I'm dimensioning an array to hold up to 255 items, but if
it only contains three, I don't want to cycle through all 255 if that's
possible.


Yes, you could create a separate counter variable to let you know how may of the array
slots are populated. Or you could use a marker in the slot after the last valid entry.

You could also dynamically resize the array using the Redim statement but there may be
performance issues to consider.

--
'---------------
'John Mishefske
'---------------
Nov 13 '05 #2

P: n/a
rkc
Jozef wrote:
Hello,

Is there an easy way to determine the highest point in an array that
contains a value?
Not unless you were keeping track while the array was being
populated.
I'm dimensioning an array to hold up to 255 items, but if
it only contains three, I don't want to cycle through all 255 if
that's possible.


It's possible to check for a value and exit the loop if there isn't
one. Something you should be doing any way if you're performing
some operation on each value as you cycle through them.

Nov 13 '05 #3

P: n/a
My idea (for a string array, a) is

UBound(Split(RTrim(Join(a))))

Please, let me know how and if this works as it's "total air" code.

Nov 13 '05 #4

P: n/a
ly******@yahoo.ca <ly******@yahoo.ca> wrote:
: My idea (for a string array, a) is

: UBound(Split(RTrim(Join(a))))

: Please, let me know how and if this works as it's "total air" code.
Does this mean that this is invalid?

Dim Aray(26) as String
Aray(19) = "This "
Aray(8) = "is "
Aray(12) = "my "
Aray(0) = "array."

This 26 element array with four defined elements has its last
defined element at place 20
A separate counter seems like the safest route to me, updated like
this:

If array_position_just_defined > highest_array_position Then
highest_array_position = array_position_just_defined

with highest_array_position initialized to -1 for the empty array.

--thelma
Nov 13 '05 #5

P: n/a
rkc
ly******@yahoo.ca wrote:
My idea (for a string array, a) is

UBound(Split(RTrim(Join(a))))

Well I'll be dipped in pancake batter.
Nov 13 '05 #6

P: n/a
Thelma Lubkin <th****@alpha2.csd.uwm.edu> wrote:
: Dim Aray(26) as String
: Aray(19) = "This "
: Aray(8) = "is "
: Aray(12) = "my "
: Aray(0) = "array."

: This 26 element array with four defined elements has its last
: defined element at place 20

arghh... 19
--thelma

: A separate counter seems like the safest route to me, updated like
: this:

: If array_position_just_defined > highest_array_position Then
: highest_array_position = array_position_just_defined

: with highest_array_position initialized to -1 for the empty array.

Nov 13 '05 #7

P: n/a
"Jozef" <SP**********@telus.net> wrote in
news:w1Lue.1786745$6l.247118@pd7tw2no:
Is there an easy way to determine the highest point in an array
that contains a value? I'm dimensioning an array to hold up to
255 items, but if it only contains three, I don't want to cycle
through all 255 if that's possible.


Why do you have blank entries in your array?

If you need to have a certain number of parameters with or without
values, what about using the array in conjunction with a collection,
and you'd use the collection to manage which items had values?

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #8

P: n/a


Thelma Lubkin wrote:
Thelma Lubkin <th****@alpha2.csd.uwm.edu> wrote:
: Dim Aray(26) as String
: Aray(19) = "This "
: Aray(8) = "is "
: Aray(12) = "my "
: Aray(0) = "array."

: This 26 element array with four defined elements has its last
: defined element at place 20

arghh... 19
--thelma


Huh?

Nov 13 '05 #9

P: n/a
Thanks for your help folks. I'm already using a counter, I was just curious
if there was a single function I could call (there should be,
Microsoft...are you listening?) Something like MaxArr(MyArray) ....tha'd be
good.
"Jozef" <SP**********@telus.net> wrote in message
news:w1Lue.1786745$6l.247118@pd7tw2no...
Hello,

Is there an easy way to determine the highest point in an array that
contains a value? I'm dimensioning an array to hold up to 255 items, but
if it only contains three, I don't want to cycle through all 255 if that's
possible.

any ideas?
Thanks!

Nov 13 '05 #10

P: n/a
ly******@yahoo.ca wrote:

Thelma Lubkin wrote:
Thelma Lubkin <th****@alpha2.csd.uwm.edu> wrote:
: Dim Aray(26) as String
: Aray(19) = "This "
: Aray(8) = "is "
: Aray(12) = "my "
: Aray(0) = "array."

: This 26 element array with four defined elements has its last
: defined element at place 20

arghh... 19
--thelma

Huh?


Right first time, element numbered 19 is place 20 if it's zero based :-)

--
[OO=00=OO]
Nov 13 '05 #11

P: n/a
Trevor Best <no****@besty.org.uk> wrote:
: ly******@yahoo.ca wrote:
:>
:> Thelma Lubkin wrote:
:>
:>>Thelma Lubkin <th****@alpha2.csd.uwm.edu> wrote:
:>>: Dim Aray(26) as String
:>>: Aray(19) = "This "
:>>: Aray(8) = "is "
:>>: Aray(12) = "my "
:>>: Aray(0) = "array."
:>>
:>>: This 26 element array with four defined elements has its last
:>>: defined element at place 20
:>>
:>> arghh... 19
:>> --thelma
:>>
:>
:>
:> Huh?
:>

: Right first time, element numbered 19 is place 20 if it's zero based :-)

Yes. I was thinking of 'place' in the sense of the
counter-number you'd use in a 'while i <=' type loop,
but it is of course the 20'th 'physical' place in the array.
--thelma
: --
: [OO=00=OO]
Nov 13 '05 #12

P: n/a
I think you have rightly implied that
UBound(Split(RTrim(Join(a))))
doesn't work when some of the elements (strings) have spaces. These
elements are counted twice.

Maybe this will help:

UBound(Split(RTrim(Join(a)))) - UBound(Filter(a, " ", True))

It gives 19 for your array. Does it work for every string array? I
doubt it but it might be something that could be developed further.

BTW it seems filter does not filter for the string given, but for any
occurrence of the string in any of the string elements, rather like a
wildcard. Thus it returns true for
" ab", and "a b" and "ab " and "a b", etc.

Nov 13 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.