Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General / July 2004



Tip: Looking for answers? Try searching our database.

How to use text as array index

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Top Spin - 30 Jul 2004 00:29 GMT
What's the recommended way to get from a text string to an array
index?

I have a series of data records that I will be reading one at a time.
They have a format like:

 name class amount

I want to add up all of the amounts and subtotal them by name and
class:

 name    class    amount
 Joe       101      250
 Joe       107a    347
 Joe       121      123
 Sue      107b     223
 Sue      246       119

I don't know in advance which or how many names or classes I will
have, but less than 100.

I would like to define a 2-dimensional array

  SubTotals(100,100)

where SubTotals(2,5) would be the subtotal for user #2 in class #5.

I would also have a Name(100) array and a Class(100) array.

When I get a new record, I need to determine if the name and class
have been seen before or not. If not, add them to the Name() and/or
Class() arrays. If they have, determine the index numbers so I can use
that to index into SubTotals(100,100).

I know I can search Name() and Class() each time and use the index to
index into SubTotals(), but is there an easier way?

Thanks

Signature

Running MS VB 6.0 Pro (SP5) on Win2K-SR2
For email, use Usenet-20031220 at spamex.com

anon - 30 Jul 2004 01:21 GMT
"What's the recommended way to get from a text string to an array
index?"

Use the split function to take a string with certain delimeters and turn it into an array.

> What's the recommended way to get from a text string to an array
> index?
[quoted text clipped - 34 lines]
>
> Thanks
Top Spin - 30 Jul 2004 01:57 GMT
>"What's the recommended way to get from a text string to an array
>index?"
>
>Use the split function to take a string with certain delimeters and turn it into an array.

I am getting the text one word at a time -- there is nothing to split.

>> What's the recommended way to get from a text string to an array
>> index?
[quoted text clipped - 34 lines]
>>
>> Thanks

Signature

Running MS VB 6.0 Pro (SP5) on Win2K-SR2
For email, use Usenet-20031220 at spamex.com

Larry Serflaten - 30 Jul 2004 05:00 GMT
> What's the recommended way to get from a text string to an array
> index?

The simple method would be to use the keys in a collection

Dim NameIndex As Collection
Set NameIndex = New Collection

' Now add the names and indices:

  NameIndex.Add 1, "Joe"
  NameIndex.Add 2, "Sue"
  NameIndex.Add 3, "Bob"

' To get the index back out, use the name (key)

   index = NameIndex("Sue")  ' Returns 2

The keys are case sensative, if you don't want case
sensativity, then add the members in a case insensative way:

  NameIndex.Add 1, "joe"
  NameIndex.Add 2, "sue"
  NameIndex.Add 3, "bob"

and be sure to check for them in the same manner:

   index = NameIndex("sue")

HTH
LFS
Wolff - 30 Jul 2004 07:31 GMT
> The keys are case sensative, if you don't want case
> sensativity, then add the members in a case insensative way:

At least in VB6 keys of collection items are not case sensitive.

Wolff
Top Spin - 30 Jul 2004 13:27 GMT
>> What's the recommended way to get from a text string to an array
>> index?
[quoted text clipped - 13 lines]
>
>    index = NameIndex("Sue")  ' Returns 2

That sounds like exactly the solution I was looking for. I will work
on that. Thanks.

Signature

Running MS VB 6.0 Pro (SP5) on Win2K-SR2
For email, use Usenet-20031220 at spamex.com

Top Spin - 31 Jul 2004 02:38 GMT
>> What's the recommended way to get from a text string to an array
>> index?
[quoted text clipped - 13 lines]
>
>    index = NameIndex("Sue")  ' Returns 2

If I understand this correctly, the collection mechanism associates a
set of "items" with a set of "keys":

    object.add item,key

The items can be of type numeric or string, but the keys are strings.

Once added, I can use the key ("Sue") to get to the item (2):

    ?NameIndex("Sue")
    2

But how to I get the key (2) to get to the item ("Sue")?

Thanks

>The keys are case sensative, if you don't want case
>sensativity, then add the members in a case insensative way:
[quoted text clipped - 6 lines]
>
>    index = NameIndex("sue")

On my machine, "Sue", "sue", and "SUE" all seemed to work the same

Signature

Running MS VB 6.0 Pro (SP5) on Win2K-SR2
For email, use Usenet-20031220 at spamex.com

Larry Serflaten - 31 Jul 2004 04:29 GMT
> >> What's the recommended way to get from a text string to an array
> >> index?

> But how to I get the key (2) to get to the item ("Sue")?

That wasn't the question.  Your question was how to get from the
text string ("Sue") to an index (2).

If you want to go both ways, you might try a Dictionary object,
or depending on the limits, you might use a fixed length string for
the keys and store them all in another string at fixed offsets.

InStr will help you find a Key, and you can divide that found position
by the fixed length to get the index.

LFS

LFS
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.