Splitting and 2 dimensional array

# Thread: Splitting and 2 dimensional array

1. Ian
Senior Member
Join Date
Dec 1969
Posts
736

## Splitting and 2 dimensional array

i understand the concent of splitting a string into an array, but how would i split 2 strings that match up into one 2 dimensional array?<BR><BR>1, 2, 3, 4, 5<BR>hi, how, are, you, ?<BR><BR>i want to split it up so its<BR>thearray(1, 0) = 1<BR>thearray(1, 1) = hi<BR><BR>thearray(2, 0) = 2<BR>thearray(2, 1) = how<BR><BR>i could put them into 2 seperate arrays, however im sorting them so they would not stay aligned to where htey need to be, any idea how i would accomplish what i wrote above?

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

## RE: Splitting and 2 dimensional array

How about you split them like normal then the third 2 din array where you loop through the other to to fill this new array

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

## RE: Splitting and 2 dimensional array

You can&#039;t.<BR><BR>But, actually, you don&#039;t *WANT* to sort a 2D array, anyway!<BR><BR>After all, every time you need to swap a "row", you have to swap all the "columns" in that row, one at a time.<BR><BR>A much better way is to use an array OF arrays.<BR><BR>So...<BR><BR>&#060;%<BR>temp1 = Split(s1)<BR>temp2 = Split(s2)<BR>If UBound(temp1) &#060;&#062; UBound(temp2) Then <BR>&nbsp; &nbsp; ...you are hosed ...<BR>End If<BR>Dim result()<BR>ReDim result( UBound(temp1) )<BR>For i = 0 To UBound(result)<BR>&nbsp; &nbsp; result(i) = Array(temp1(i),temp2(i))<BR>Next<BR>%&#062;<BR><BR >*NOW* you can sort that one-dimensional result array much faster and cleaner than working with a 2D array.<BR><BR>

4. Ian
Senior Member
Join Date
Dec 1969
Posts
736

## RE: Splitting and 2 dimensional array

Ok here&#039;s my code<BR><BR> Dim objFile<BR> Dim objDirect<BR> for each objDirect in objFolder.subfolders<BR> if direct = "" then<BR> direct = objDirect.path<BR> else<BR> direct = direct & chr(8) & objDirect.path<BR> end if<BR> next<BR> For Each objFile in objFolder.files<BR> if files = "" then<BR> files = objFile.path<BR> strType = objFile.type<BR> else<BR> files = files & chr(8) & objFile.path<BR> strType = strType & chr(8) & objFile.type<BR> end if<BR> Next<BR>files = split(files, chr(8) )<BR>direct = split(direct, chr(8))<BR>strType = split(strType, chr(8))<BR>dim result()<BR>redim result(ubound(files))<BR>for iLoop = 0 to ubound(files)<BR> result(iLoop) = array(files(iLoop),strtype(iLoop))<BR>next<BR><BR> &#060;script language=JScript runat=server&#062;<BR> function SortVBArray(arrVBArray) {<BR> return arrVBArray.toArray().sort().join(&#039;&#039;);<B R> }<BR>&#060;/script&#062;<BR>&#060;%<BR> Function SortArray(arrInput)<BR> SortArray = Split(SortVBArray(arrInput), Chr(8))<BR> End Function<BR>%&#062;<BR><BR>direct = SortArray(direct)<BR>files = SortArray(files)<BR>result = sortarray(result)<BR><BR><BR><BR><BR>when i run the sort command it returns the error.<BR><BR>Microsoft JScript runtime error &#039;800a138d&#039; <BR><BR>String expected <BR><BR>/update/testpage/programmers/fsotest.asp, line 95 <BR>

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

## Sorry, but that code seems plain silly...

...you go to all the trouble to put the various items into strings and then split the strings! <BR><BR>Why not just put the items into an array of arrays, on the fly?<BR><BR>Or at least into a 2D array, on the fly???<BR><BR>Did you even look at my code in the ASPFAQs?<BR><BR>http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=118<BR><BR>

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

## And JS sort won't work...

...for *EITHER* an array of arrays *OR* a 2D array.<BR><BR>To see why, go read the docs on the VBArray.toArray() method in the JScript Reference manual (link on left side of this page).<BR><BR>So you are hosed on this one, so far as using that JS sort trick is concerned.<BR><BR>Besides, a directory typically holds few enough files that just doing a bubble sort in VBS code is *plenty* fast enough!<BR><BR>And if it isn&#039;t, then write your code in pure JScript, creating an array of *OBJECTS* (not of plain strings), and then implement your own compare method for the objects and then use JS sort.<BR><BR>But the mix of VBS and JS for this purpose is a very bad one.<BR><BR>

#### Posting Permissions

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