|
| Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free! |
VBscript, concatenating arrays
|
Original Message
|
Name: Shr0Om
Date: May 2, 2006 at 06:06:17 Pacific
Subject: VBscript, concatenating arraysOS: Win Xp sp2CPU/Ram: amd 64 3200Model/Manufacturer: custom |
Comment: Hi.. im trying to figure out how to concatenating two arrays in VBscript, but i seem to be totally stuck. I cannot find any examples on concatenating except for simple string concats.. Im also totally new to VBscripting (started 2days ago, with only batch scripting experience) so i hope anyone can help me out. Here's my copy paste&somewhat written source code. You'll have to excuse me for the poor programming. What i would like to have array3 that contains array1&array2, then output array3 to a txt file. i.e if array1 contains 1 2 3 4 and array2 contains a b c d array3 should contain 1a 2b 3c 4d. Also, it seems that both arrays contains only the last line of the txt file.. :( 'source Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("C:\HELT NYESTE\test.csv", ForReading) Do Until objTextFile.AtEndOfStream strNextLine = objTextFile.Readline array1 = Split(strNextLine , ";") For i = 1 to Ubound(array1) Next Loop Set objTextFile = objFSO.OpenTextFile _ ("C:\HELT NYESTE\forbruk.csv", ForReading) Do Until objTextFile.AtEndOfStream strNextLine = objTextFile.Readline array2 = Split(strNextLine , ";") For i = 1 to Ubound(array2) Next Loop 'Just a write to file test. 'Here i would like to have the c Set objTextFile = objFSO.createTextFile _ ("C:\HELT NYESTE\output.txt", ForAppending) For i = 1 to Ubound(array1) objTextFile.Write array1(0) & " Forbruk 1a:" & array2(0) Next objTextFile.close
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: Michael J (by mjdamato)
Date: May 2, 2006 at 07:01:59 Pacific
|
Reply: (edit)Arrays 1 & 2 only contain the last line because you are recreating the array each time you read a new line. Also, these two lines do nothing: For i = 1 to Ubound(array1) Next This procedure should work to create arrays 1 & 2: 'Create a single string with 'entire content (add delimiter) Do Until objTextFile.AtEndOfStream strOneLine = objTextFile.Readline & ";" Loop 'Remove last delimiter strOneLine = Left(strOneLine,Len(strOneLine)-1) 'Create the array array1 = Split(strNextLine , ";") Then before creating array 3, need to determine which input array (1 or 2) has the least number of elements to avoid errors. Also, note array indecies start at 0 not 1. 'Determine minimum array length If UBound(array1) < UBound(array2) Then arrayLength = UBound(array1) Else arrayLength = UBound(array2) End If'Create array 3 Dim array3(arrayLength) Set objTextFile = objFSO.createTextFile _ ("C:\HELT NYESTE\output.txt", ForAppending) 'Populate array 3 and write to file For i = 0 to arrayLength array3(i) = array1(i)&array2(i) objTextFile.writeline array3(i) Next Michael J
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: Michael J (by mjdamato)
Date: May 2, 2006 at 12:07:44 Pacific
|
Reply: (edit)Note that I didn't test this. There could be some minor syntaxt errors, but it should be technically correct. Michael J
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: Shr0Om
Date: May 3, 2006 at 01:50:49 Pacific
|
Reply: (edit)Ok, i have gotten much further now. The files are being added to array1&2. If the txt files only contains one collumn tho, every row has to be ended by a ";" If i only have one collumn, Excel wont save a ";" on the end. Can it be fixed? Also, arrayLength is not accepted while trying to concatenate array1&2. Checking out arrayLenght it contains "1" while the txt files contains 9 rows.. So i guess arrayLength should contain 8, not 1?.. Here's the new source with some comments on it. I hope you can help me abit further Michael:]
c:\number.csv contains 1 2 3 . . 9 c:\letters.csv contains a b c . . i -----------------SOURCE--------------------- Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("C:\HELT NYESTE\Numbers.csv", ForReading) 'CREATING ARRAY1 FROM FILE 'Create a single string with 'entire content (add delimiter) Do Until objTextFile.AtEndOfStream strOneLine = objTextFile.Readline & ";" 'Remove last delimiter strOneLine = Left(strOneLine,Len(strOneLine)-1)
'Create the array array1 = Split(strOneLine , ";") For i = 1 to Ubound(array1) Wscript.Echo ": " & array1(0) Next Loop 'CREATING ARRAY2 FROM FILE Set objTextFile = objFSO.OpenTextFile _ ("C:\HELT NYESTE\Letters.csv", ForReading)
'Create a single string with 'entire content (add delimiter) Do Until objTextFile.AtEndOfStream strOneLine = objTextFile.Readline & ";" 'Remove last delimiter strOneLine = Left(strOneLine,Len(strOneLine)-1) 'Create the array array2 = Split(strOneLine , ";") For i = 1 to Ubound(array2) Wscript.Echo ": " & array2(0) Next Loop ''Then before creating array 3, need to determine which input array (1 or 2) has the least number of elements to avoid errors. Also, note array indecies start at 0 not 1. 'Determine minimum array length
If UBound(array1) < UBound(array2) Then arrayLength = UBound(array1) Else arrayLength = UBound(array2) End If
'Create array 3 'Testing what arrayLength contains MsgBox (arrayLength) 'arrayLength contains 1. Should be 9... Dim array3(arrayLength) 'Gives an error. Expected integer constant. Tried to declare a constant containing arrayLenght's value and use it instead. Got new error, expected literal constant.. Set objTextFile = objFSO.createTextFile _ ("C:\HELT NYESTE\output.txt", ForAppending)
'Populate array 3 and write to file For i = 0 to arrayLength array3(i) = array1(i)&array2(i) objTextFile.writeline array3(i) Next
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: Michael J (by mjdamato)
Date: May 3, 2006 at 08:52:46 Pacific
|
Reply: (edit)"If i only have one collumn, Excel wont save a ';' on the end. Can it be fixed?" I didn't realize that the input had a ";" at the end of each line already. I though it was only between each data item on a line. If that is the case, you can remove the red text in the two instances of this line (however, this line is incorrect - see below): strOneLine = objTextFile.Readline & ";" I don't think you want a ";" at the very end. Because when you split the string into an array it would create a null entry at the very end. As for the arrayLength variable not being correct, it may have something to do with the loops being incorrectly formatted. This was partially my fault for a mistake in one of the lines. Here is what you have: 'Create a single string with 'entire content (add delimiter) Do Until objTextFile.AtEndOfStream strOneLine = objTextFile.Readline & ";" 'Remove last delimiter strOneLine = Left(strOneLine,Len(strOneLine)-1) 'Create the array array2 = Split(strOneLine , ";") For i = 1 to Ubound(array2) Wscript.Echo ": " & array2(0) Next Loop The loop should ONLY encompass the reading of the file and creating a single string variable. Then AFTER the loop it will create the array. So, with the change to the delimiter noted above and the other corrections needed, that process should look more like this: 'Create a single string with 'entire content (add delimiter) Do Until objTextFile.AtEndOfStream strOneLine = strOneLine & objTextFile.Readline Loop'Create the array array2 = Split(strOneLine , ";") 'For debuggin only? For i = 1 to Ubound(array2) Wscript.Echo ": " & array2(0) Next
If you need more assistance, it would be helpful if I had an example of your input files to see what you are working with. Perhaps you can email me examples (just click my name) in the post header. Michael J
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: Shr0Om
Date: May 4, 2006 at 04:58:23 Pacific
|
Reply: (edit)Hi again Michael, Ive updated the source code, and it now works better as long as file1 and file2 ONLY contains 1 column. If there are 2 columns, 12|ab the ouput will result in 21ba for some reason.If 3 colums or more, i just get an error. Also the var arrayLenght still aint accepted for some mysterios reason. I'll paste the new source here. Hope you can help me abit further if you have time for it. Sorry to bother you this much;) Also i sent you an email with an more in depth exlanation of what the script is supposed to do.. For the moment both files consists of 1 column containing: 1 2 . . 9 and a b . . i -------------SOURCE--------------- Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("C:\HELT NYESTE\Numbers.csv", ForReading) 'CREATING ARRAY1 FROM FILE 'Create a single string Do Until objTextFile.AtEndOfStream strFile1 = strFile1 & objTextFile.Readline Loop
'Create the array array1 = Split(strFile1 , ";") 'Echo for debugging purposes For i = 1 to Ubound(array1) Wscript.Echo "array1: " & array1(i) Next 'CREATING ARRAY2 FROM FILE Set objTextFile = objFSO.OpenTextFile _ ("C:\HELT NYESTE\Letters.csv", ForReading) 'Create a single string with Do Until objTextFile.AtEndOfStream strFile2 = strFile2 & objTextFile.Readline Loop 'Create the array array2 = Split(strFile2 , ";") 'Echo for debugging purposes For i = 1 to Ubound(array2) Wscript.Echo "array2: " & array2(i) Next 'Determine minimum array length If UBound(array1) < UBound(array2) Then arrayLength = UBound(array1) Else arrayLength = UBound(array2) End If
'Create array 3 'Testing what arrayLength contains for debugging purposes MsgBox (arrayLength) 'arrayLength is now 9, which is correct Dim array3(9) 'Still gives an error if var arrayLength is used: Expected integer constant. Works if an integer is used. Using 9 for now.. Set objTextFile = objFSO.createTextFile _ ("C:\HELT NYESTE\output.txt", ForAppending)
'Populate array 3 and write to file For i = 0 to arrayLength array3(i) = array1(i)&array2(i) objTextFile.writeline array3(i) Next
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: Michael J (by mjdamato)
Date: May 4, 2006 at 07:44:17 Pacific
|
Reply: (edit)Well, that was sooo much easier with all the info. There's a bunch of error handling code that could be added to this such as handling input files with empty lines, deleting the output file if it already exists, etc. But, with the samples you gave me the below code will work (FYI: you set the constant ForReading, but then you use the variable ForAppending in your code which has not been set). I added code to strip the leading & trailing spaces from the input data, and I also added " / " between the two items when concatenating them in array3 for readability purposes. Change it as you see fit. Const ForReading = 1 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile _ ("numbers.csv", ForReading) 'CREATING ARRAY1 FROM FILE 'Create a single string Do Until objTextFile.AtEndOfStream strFile1 = strFile1 & objTextFile.Readline & ";" Loop
'Create the array strFile1 = Left(strFile1,Len(strFile1)-1) array1 = Split(strFile1 , ";") 'Remove leading and trailing spaces For i = 0 to Ubound(array1) array1(i) = Trim(array1(i)) Next 'CREATING ARRAY2 FROM FILE Set objTextFile = objFSO.OpenTextFile _ ("letters.csv", ForReading) 'Create a single string with Do Until objTextFile.AtEndOfStream strFile2 = strFile2 & objTextFile.Readline & ";" Loop 'Create the array strFile2 = Left(strFile2,Len(strFile2)-1) array2 = Split(strFile2 , ";") 'Remove leading and trailing spaces For i = 0 to Ubound(array2) array2(i) = Trim(array2(i)) Next 'Determine minimum array length If UBound(array1) < UBound(array2) Then arrayLength = UBound(array1) Else arrayLength = Int(UBound(array2)) End If 'Create array 3 ReDim array3(arrayLength) 'Create the optput file Set objTextFile = objFSO.createTextFile ("output.txt", ForAppending) 'Populate array 3 and write to file For i = 0 to arrayLength array3(i) = array1(i) & " / " & array2(i) objTextFile.writeline array3(i) Next Michael J
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: Shr0Om
Date: May 5, 2006 at 02:46:49 Pacific
|
Reply: (edit)Ive tested the new source, and now everything works as it should. Also the problem with arrayLenght is solved. Ive also declared ForAppending = 8. (i mean i have read somewhere 8 is for appending). Still the txt file gets overwritten everytime, so its not really appending.. But it doesnt matter, and if nececary i'll probably figure it out. My next step is to figure out how to search in array2 for values taken from array1. Also, i'll try to figure out how to discard columns i dont want. I'll give it a try myself first and see if i can figure it out. Thnx for all the help so far. I appreciate it:]
Report Offensive Follow Up For Removal
|
|
Response Number 9
|
Name: Michael J (by mjdamato)
Date: May 5, 2006 at 09:19:53 Pacific
|
Reply: (edit)It is creating a new file every time because you are using createTextFile and not openTextFile. See the tutorial here: http://computerperformance.co.uk/vbscript/vbscript_file_opentextfile.htm Michael J
Report Offensive Follow Up For Removal
|

Post Locked
This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
Go to Programming Forum Home
|
|
|