Array of compound keys

# Thread: Array of compound keys

1. Senior Member
Join Date
Dec 1969
Posts
2,892

## Array of compound keys

I have a form that sends sets of DB keys together. The data comes in like this:<BR>strRawCompoundKeys = "16675-4566, 177865-1255, 177632-8890"<BR><BR>I create an array:<BR><BR>arrKeys = Split( strRawCompoundKeys, ",")<BR>... which gives me the array:<BR><BR>arrKeys(0) = 16675-4566<BR>arrKeys(1) = 177865-1255<BR>arrKeys(2) = 177632-8890<BR><BR>Obviously that, by itself, isn&#039;t going to do it. So, which should be faster - splitting each arrKeys again on "-" to create a new 2D array, or just looping the 1st array and using a string function to pull apart the compound key into two unique parts?<BR><BR>Anyone hazard a guess? I think I can do both, but I would perefer to spend time developing along the correct path - whichever will process faster.<BR><BR>Thanks<BR><BR>Kurt

2. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## How would you get a 2D array

with the second split? You would have 2 1D arrays right??<BR><BR>you can go with that, it should work fine<BR><BR>

3. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## Whoops

ok not 2 1D arrays but 1 main 1D and multiple smaller 1D arrays<BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
2,892

## RE: Whoops

I see what you mean, assuming I did a true newArray = Split (arrKeys(i)) thingee.<BR><BR>But what if I did something along the lines of:<BR><BR>For x = 0 to UBound(arrKeys)<BR> NewArray(x, 0) = (string function to pull left key)<BR> NewArray(x, 1) = (string func to pull right key)<BR>Next<BR><BR>Okay, so that&#039;s not quite right, but would that be fairly fast?

5. Senior Member
Join Date
Dec 1969
Posts
224

## RE: Array of compound keys

i would split them again, into a 2d array.<BR><BR>public function makeDBkeys(arrKeys)<BR> dim arr,tmpArr<BR> dim i : for i = 0 to ubound(arrKeys)<BR> redim preserve arr(1,i)<BR> tmpArr = split(arrKeys,"-")<BR> arr(0,i) = tmpArr(0)<BR> arr(1,i) = tmpArr(1)<BR> next<BR> makeDBKeys = arr<BR>end function

6. Senior Member
Join Date
Dec 1969
Posts
224

## ****

how do you make this forum preserve indenting?

7. Senior Member
Join Date
Dec 1969
Posts
2,892

## RE: ****

Thanks Thorpe. We&#039;re on the same track here.<BR><BR>I either type the spaces in right here, or when I cut n paste from WordPad they are preserved. (Though not in preview mode.)

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## I disagree...

...with several points in that post.<BR><BR>First of all, why do you *WANT* a 2D array???<BR><BR>How often are you going to need to get values from the single cells of the 2D array??? If you create the 2D array and then only go looking at the values one time, why not just do it "on the fly" as you get them???<BR><BR>That is, do the SPLIT only as needed.<BR><BR>Secondly, if you do need the cell contents frequently, then save work and space:<BR> For i = 0 To UBound(arrKeys)<BR> arrKeys(i) = Split( arrKeys(i), "-" )<BR> Next<BR>And then, when you need a single value, you do<BR> arrKeys(row)(column)<BR><BR>Yes, a double set of parens. Or you can do<BR> keyrow = arrKeys(row)<BR> cell = keyrow(column)<BR><BR>FINALLY, if you still want the 2D array:<BR><BR>Don&#039;t do REDIM PRESERVE every time through the loop! That&#039;s one of the most expensive operations you can do in VBS.<BR><BR>So:<BR><BR>public function makeDBkeys(arrKeys)<BR> dim arr,tmpArr,i<BR> ReDim arr(1,UBound(arrKeys)) &#039; one time only<BR> for i = 0 to ubound(arrKeys)<BR> tmpArr = split(arrKeys,"-")<BR> arr(0,i) = tmpArr(0)<BR> arr(1,i) = tmpArr(1)<BR> next<BR> makeDBKeys = arr<BR>end function<BR><BR>***********<BR><BR>But again, if there&#039;s no real *NEED* to split the values until just before they are used, why do so? Can&#039;t tell, of course, from what has been posted here whether or not this applies.<BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
96,118

## Tell us how you will use...

...this 2D array. How many dozen times will you access each cell?<BR><BR><BR>

10. Senior Member
Join Date
Dec 1969
Posts
224

## RE: I disagree...

"Don&#039;t do REDIM PRESERVE every time through the loop! That&#039;s one of the most expensive operations you can do in VBS"<BR><BR>once again, ive learnt something new. thanks heaps. never really thought about dim&#039;ing the array once using the ubound of the other array, and i sure didnt know this was such an expensive task.<BR><BR>ive now got about 20 array functions to go fix. i love this board. the boars i used to hang out at was so slow and mostly guys with less experience than myself so i rarely picked up tips.<BR><BR>but here.... well, 2 in one day.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•