Getting 1-dimensional arrays from 2-dimensional?

# Thread: Getting 1-dimensional arrays from 2-dimensional?

1. Junior Member
Join Date
Dec 1969
Posts
4

## Getting 1-dimensional arrays from 2-dimensional?

I want to do something like this:<BR><BR>For Each lArray in arrBanners<BR> Call aFunction(lArray)<BR>Next<BR><BR>where aFunction takes 1-dimensional arrays as Argument but can&#039;t get it to work, am i doing anything wrong or can&#039;t this be done?<BR><BR>arrBanners is a 2dimensional array returned from a rs.getRows in this case!<BR><BR>Help Please!<BR>//Leif Landén, aka berinder

2. Senior Member
Join Date
Dec 1969
Posts
11,334

## RE: Getting 1-dimensional arrays from 2-dimensiona

Even though you&#039;re mucking with a 2-d array, you&#039;re still just passing it it&#039;s elements with the For Each... Loop. If you had an array of arrays, you&#039;d probably get what you&#039;re looking for.<BR><BR>Are you looking for say, just the first dimension&#039;s elements to get in? What are you trying to do in that function?<BR><BR>If you NEED to have that as an array, you&#039;ll have to create another array and pass all that muck into the new one, then pass that into the function. Surely there&#039;s a better solution... tell us what you&#039;re trying to do here.

3. Senior Member
Join Date
Dec 1969
Posts
16,931

## RE: Getting 1-dimensional arrays from 2-dimensiona

I&#039;m with Ghost here - there must be a better solution.<BR><BR>However, if you really want to do it this way:<BR><BR>Dim i, a, strTemp, arrTemp<BR>For i=0 to UBound(GetRowsArray, 2)<BR> strTemp = ""<BR> arrTemp = Array()<BR> For a=0 to UBound(GetRowsArray, 1)<BR> strTemp = GetRowsArray(a,i) & Chr(9)<BR> Next<BR> arrTemp = Split(strTemp, Chr(9))<BR> &#039; You&#039;ll end up with an extra element because of the<BR> &#039; Chr(9) at the end. Remove it.<BR> ReDim Preserve arrTemp(UBound(arrTemp)-1)<BR> aFunction(arrTemp)<BR>Next<BR><BR>Craig.

4. Senior Member
Join Date
Dec 1969
Posts
16,931

## RE: Getting 1-dimensional arrays from 2-dimensiona

oops - typo.<BR><BR>strTemp = GetRowsArray(a,i) & Chr(9)<BR><BR>should read<BR><BR>strTemp = strTemp & GetRowsArray(a,i) & Chr(9)<BR><BR><BR>Craig.

5. Junior Member
Join Date
Dec 1969
Posts
4

## RE: Getting 1-dimensional arrays from 2-dimensiona

Ok, to describe what i&#039;m doing... <BR><BR>First i&#039;m opening a recordset, taking out the data with getRows and getting rid of the rs. After that i have a Template that i populate with some data (got more pages, nr of pages bla bla.. ) it also got ten tokens for the ten rows i get with the getrows. <BR><BR>Those rows have a specific template for them, i made a function that takes the data from one of the rows as a parameter. The good with that way is that i can just rewrite that function if the data in the database changes. No need to rewrite both the function that calls it and the function itself.. <BR><BR>The simple solution would be to send each of the values to the function in the function call, but i don&#039;t wanna do that because of the reason just mentioned.<BR><BR>Better solutions proposed?

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

## VBS arrays NOT like Java/JS/C/C++!!

In those other languages, a 2D array is *really* an array *of* arrays. <BR><BR>NOT SO in VBScript! VBS is more like Fortran (and Pascal?).<BR><BR>A "row" of a VBS 2D array is *NOT* a 1D array! Period. <BR><BR>So the best you can do, other than the incredibly ugly code of copying the elements of one "row" of the 2D array into a separate 1D array [talk about a horrible performance hog!] is to do it Fortran-style:<BR><BR>You pass the ENTIRE ARRAY to the function/sub and *also* pass the row number. And the function/sub *must* be aware that it is working with a 2D array, instead of a 1D array.<BR><BR>This isn&#039;t hard. Don&#039;t make it more work than it is. Live with the differences, instead of fighting them.<BR><BR>

7. Junior Member
Join Date
Dec 1969
Posts
4

## RE: VBS arrays NOT like Java/JS/C/C++!!

Thanks for the clarification, coming from a C++/Java background that was the error i did. Making a two-dimensional array an array of arrays is the logical way ;)<BR><BR>It&#039;s nothing more to do than live with it, and i&#039;m sure i can :) Thanks for the suggestion of passing the whole array and the row-number, that will met the requirements of not having to do the recode both in the callling and the called function!<BR><BR>Be well, and don&#039;t forget that it&#039;s not long until The lord of the rings!

#### Posting Permissions

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