List Info

Thread: Re: parsing ( strings )




Re: parsing ( strings )
country flaguser name
United States
2007-10-12 17:10:56

In fact, here is a quote from Microsoft's support site regarding Ubound:

"You can use UBound to determine the total number of elements in an array,
but you must adjust the value it returns to account for the fact that the subscripts start at 0. "

Steve

----- Original Message ----
From: Steve Manser <smansertwcny.rr.com>
To: helpwithvbyahoogroups.com
Sent: Friday, October 12, 2007 12:12:13 PM
Subject: RE: [helpwithvb] parsing ( strings )

Oh Steve,

 

>>&nbsp; Split returns a zero-based array, and UBound returns
>>;  the number of array elements, not the Last Index of the array. So you
>>;  have subtract 1 from the UBound result to use it as an array index.

 

That is not right, it is incorrect, it is wrong.

 

...and I certainly hope I didn¢t say that, or imply that.

 

Do not subtract one from the value UBound() returns.

 

UBound() Tells The Truth, Always.

 

Note that UBound() may not return what we *expect* to see,

but that is often because we fed it the wrong information.

 

 

UBound() returns the highest valid subscript ( Index )

for that specified dimension of our array.

 

MSDN:

Returns a Long containing the largest available subscript for the indicated dimension of an array.

 

 

It will not, it does NOT, and Never Will, return the count

of how many elements are in that dimension of the array.

 

Remember That ! &nbsp;  It will be on the Test  < g >

 

-----

 

ReDim MyArray( 6 To 99 )

 

X = UBound(MyArray, 1)

 

T = UBound(MyArray)

 

The Long Variable ¡X¢ will now have a value of 99&

and so will the variable ¡T¢ [ Tee ]

 

The second argument tells UBound() what dimension

we need the value for, and the Default is ONE,

and we need not specify the second argument

if we do not need to do so.

 

However, with a Two_Dee Array, if we need the highest

Index Value for that second dimension we need to do this:

 

ReDim Array_Two( 10 To 50, 1 To 42 )

 

X = UBound( Array_Two, 2&)

 

The Variable ¡X¢ will now contain, hold, have a value of, 42&

 

-------

 

>>&nbsp; I  &nbsp;didn't check to see what Split returns,   if there are two spaces
>>; &nbsp;  between tokens,  and delimiter is set for one space. &nbsp;  Do you know offhand&nbsp; ?

<HardBall>

 

Even with all of this coffee in me belly,&nbsp; I know not the answer,

and for you to remember the answer you should write a bit of code

and see what the test code displays for you.

 

</Hardball>

 

My best guess is that Split () will treat the extra spaces

as a sub-string to be returned as an element for the array.

 

It may even believe that the extra space chars are part

of the next sub-string, and they will show up in the array

as being glued onto the front of the number, and this will

totally mess-up your code if it is not properly addressed.

 

However, you need to spend 5 minutes and do the code test.

 

Stevie always, religiously, runs his Big Strings through

a function that will remove any and all doubled-up, or

tripled-up, delimiters.

 

It is just a very good programming practice to follow, and

to make as part of your instinctive way of writing code.

 

I never forget to do this, unless I forget to do it.

 

--------

 

>> How do I get the array¢s element count ?

 

Element_Count = UBound(MyArray, 1) minus LBound(MyArray, 1) +plus One

 

 

All The Best,

Steve

.

Send Out Around

15:13 EST Friday, October 12, 2007

.

.

 

 


From: helpwithvbyahoogro ups.com [mailto: helpwithvbyahoogro ups.com ] On Behalf Of Steve Trigero
Sent: Friday, October 12, 2007 12:48 PM
To: helpwithvbyahoogro ups.com
Subject: Re: [helpwithvb] parsing ( strings )

 

You're correct that Split returns a zero-based array, and UBound returns
the number of array elements, not the Last Index of the array. So you
have subtract 1 from the Ubound result to use it as an array index.

I didn't check to see what Split returns if there are two spaces between tokens
and delimiter is set for one space. Do you know offhand?

Steve
.

 < Me Snipped, for your convenience >

.


__._,_.___
.

__,_._,___
RE: parsing ( strings )
country flaguser name
United States
2007-10-12 19:35:58


>&gt;> I did just that, subtracted one from the result. And it works.

The point I was trying to make was that your approach
will not work in all cases, or in all situations.

VB is full of, and always will be, full of these
little things we have to watch out for, and
that we must write a bit of extra code for.

So, rather than subtracting one from the UBound() value
it is much better to subtract the LBound() value from
the UBound() value, and then add a one to that result.

That handles the " N + 1 " situation.

-----------

>> My string had 9 numeric strings in it separated by spaces and
>>; Ubound returned 9. But to use the result as an index into
>>; a zero-based array I had to subtract one so it indexed
>>; from 0 to 8, 9-elements.

If UBound() returned 9 then you have 10 elements in the array.

The array will span: 0 To 9 and have 10 elements.

There may have been some extra spaces that caused
another element to be created with the Split() function.

That may have taken you down the wrong track with this.

BTW, we can always use LBound and UBound like this,
rather than computing what the start and end values are:

For X = LBound(aNums, 1) To UBound(aNums, 1)

The For Loop's header values are evaluated and cached
so they are not re-evaluated through each loop trip.

-----------

A few e-mails back I mentioned:

>&gt;>> How do I get the array’s element count ?
>&gt;>
&gt;> Element_Count = UBound(MyArray, 1) minus LBound(MyArray, 1) +plus One

or perhaps this might be clearer:

Element_Count = UBound(MyArray, 1) - LBound(MyArray, 1) + 1

Quiz: Johnny has an array dimensioned as: ( 86 To 739 )
How many elements does the array have ?

Hint: Use the formula I mention in the earlier e-mail.

Ans: 739 - 86 + 1 equals 654

Check that. I did the math in my head. Could be off by 20 or 30.

By the way, it is common to use those arrow symbols >>
to indicate the portion of the e-mail that someone
might want to address, or talk about, from an earlier e-mail.

For example, in my last e-mail, I started out like this..

( You )
>&gt; Split returns a zero-based array, and UBound returns the
>> number of array elements, not the Last Index of the array. So you
>>; have subtract 1 from the UBound result to use it as an array index.

( Me )
That is not right, it is incorrect, it is wrong.

I was only trying to reinforce the fact that when you wrote..

".... and UBound returns the number of array elements,
not the Last Index of the array.&quot;

...that you were a touch off base with that.

UBound() does not return the count of array elements.

I think we covered that above.

------

Sorry I wasn't a bit clearer about why we should not get into
the habit of just subtracting one from the UBound() value, but
it is very easy for us to carry these mistakes directly
into our code, and our code may fail if it encounters
an array which is not one or zero based.

All The Best,
Steve
.
Sent Out Around
20:33 EST Friday, October 12, 2007
.
.
_______________________________________
From: helpwithvb%40yahoogroups.com">helpwithvbyahoogroups.com [mailto: helpwithvb%40yahoogroups.com">helpwithvbyahoogroups.com]
On Behalf Of Steve Trigero
Sent: Friday, October 12, 2007 6:11 PM
To: helpwithvb%40yahoogroups.com">helpwithvbyahoogroups.com
Subject: Re: [helpwithvb] parsing ( strings )

In fact, here is a quote from Microsoft's support site regarding Ubound:

"You can use UBound to determine the total number of elements in an array,
but you must adjust the value it returns to account for the fact that the
subscripts start at 0. "

Steve

----- Original Message ----
From: Steve Manser < smanser%40twcny.rr.com">smansertwcny.rr.com>
To: helpwithvb%40yahoogroups.com">helpwithvbyahoogroups.com
Sent: Friday, October 12, 2007 12:12:13 PM
Subject: RE: [helpwithvb] parsing ( strings )
Oh Steve,
 
>>  Split returns a zero-based array, and UBound returns
>>;  the number of array elements, not the Last Index of the array.
&gt;> So you have subtract 1 from the UBound result
>>; to use it as an array index.
 
That is not right, it is incorrect, it is wrong.
 
...and I certainly hope I didn't say that, or imply that.
 
Do not subtract one from the value UBound() returns.
 
UBound() Tells The Truth, Always.
 
Note that UBound() may not return what we *expect* to see,
but that is often because we fed it the wrong information.
 
 
UBound() returns the highest valid subscript ( Index )
for that specified dimension of our array.
 
MSDN:
Returns a Long containing the largest available
subscript for the indicated dimension of an array.
 
 
It will not, it does NOT, and Never Will, return the count
of how many elements are in that dimension of the array.
 
Remember That !    It will be on the Test  < g >
 
-----
 
ReDim MyArray( 6 To 99 )
 
X = UBound(MyArray, 1)
 
T = UBound(MyArray)
 
The Long Variable 'X' will now have a value of 99&
and so will the variable 'T' [ Tee ]
 
The second argument tells UBound() what dimension
we need the value for, and the Default is ONE,
and we need not specify the second argument
if we do not need to do so.
 
However, with a Two_Dee Array, if we need the highest
Index Value for that second dimension we need to do this:
 
ReDim Array_Two( 10 To 50, 1 To 42 )
 
X = UBound( Array_Two, 2&)
 
The Variable 'X' will now contain, hold, have a value of, 42&
 
-------
 
>>  I didn't check to see what Split returns, if there are two
>>  spaces between tokens,  and delimiter is set for one space.
&gt;> Do you know offhand  ?

<;HardBall&gt;
 
Even with all of this coffee in me belly,  I know not the answer,
and for you to remember the answer you should write a bit of code
and see what the test code displays for you.
 
</Hardball>
 
My best guess is that Split () will treat the extra spaces
as a sub-string to be returned as an element for the array.
 
It may even believe that the extra space chars are part
of the next sub-string, and they will show up in the array
as being glued onto the front of the number, and this will
totally mess-up your code if it is not properly addressed.
 
However, you need to spend 5 minutes and do the code test.
 
Stevie always, religiously, runs his Big Strings through
a function that will remove any and all doubled-up, or
tripled-up, delimiters.
 
It is just a very good programming practice to follow, and
to make as part of your instinctive way of writing code.
 
I never forget to do this, unless I forget to do it.
 
--------
 
>>; How do I get the array's element count ?
 
Element_Count = UBound(MyArray, 1) minus LBound(MyArray, 1) +plus One
 
 
All The Best,
Steve
.
Send Out Around
15:13 EST Friday, October 12, 2007
.
.
________________________________________
From: helpwithvbyahoogro ups.com
[mailto: helpwithvbyahoogro ups.com ]
On Behalf Of Steve Trigero
Sent: Friday, October 12, 2007 12:48 PM
To: helpwithvbyahoogro ups.com
Subject: Re: [helpwithvb] parsing ( strings )
 
You're correct that Split returns a zero-based array, and UBound returns
the number of array elements, not the Last Index of the array. So you
have subtract 1 from the Ubound result to use it as an array index.

I didn't check to see what Split returns if there are
two spaces between tokens and delimiter is set for one space.
Do you know offhand?

Steve
.
 < Me Snipped, for your convenience >
.

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )