![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | picking items in array With previous help from here I have this script that splits each line into text components using an array. I want to modify the script so it assigns variables to the first text item in the line then assigns variables to the third and following text items. Each line is of variable length like this for example: 123 text 10/23 3/21 5/14 234 txt 01/31 543 text 02/28 05/15 I then want the variables inserted into a statement. So for the first line of text there would be three statements: upload data set date=aItem2 where id=aItem1 upload data set date=aItem3 where id=aItem1 upload data set date=aItem4 where id=aItem1 For the second line of text there would be only one statement: upload data set date=aItem2 where id=aItem1 I guess I need some looping structure that just picks out the first item in the line, skips the second, then picks the third and more items if there are any. This is my old script: Const csDlm = "," Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" ) DIM aComponents DIM aItem1 DIM aItem2 ' Check that a parameter was given: ' -------------------------------- if wscript.arguments.count < 1 then wscript.echo "This script requires 1 filename parameter" wscript.quit 1 end if ' Make sure the named file exists: ' ------------------------------- strOrigName = wscript.arguments(0) if oFS.FileExists( strOrigName ) = False then wscript.echo "File " & strOrigName & " does not exist!" wscript.quit 2 end if ' Construct a copy of the source file name ' -------------------------------------- strFolder = oFS.GetParentFolderName( strOrigName ) strFileName = oFS.GetFileName( strOrigName ) sSrcFSpec = strFolder & "/" & strFileName 'Construct a copy of the destination files '---------------------------------------- sDstFSpec = strFolder & "/" & "updateQuery.txt" Set tsDst = oFS.CreateTextFile( sDstFSpec, True ) 'Open new temporary text file for processing '-------------------------------------------- Set tsSrc = oFS.OpenTextFile(sSrcFSpec) 'Loop through file line by line '------------------------------- Do Until tsSrc.AtEndOfStream Dim sLine : sLine = Trim( tsSrc.ReadLine() ) aComponents=split(sLine, csDlm ) aItem1=aComponents(0) aItem2=aComponents(1) wscript.echo "String 1 = " & aItem1 wscript.echo "String 2 = " & aItem2 tsDst.writeline "update employee set employeeid=" & aItem2 & " where id=" & aItem2 Loop tsSrc.close tsDst.close |
My System Specs![]() |
| | #2 (permalink) |
| | Re: picking items in array James schrieb: Quote: > With previous help from here I have this script that splits each line > into text components using an array. I want to modify the script so it > assigns variables to the first text item in the line then assigns > variables to the third and following text items. Each line is of > variable length like this for example: > > 123 text 10/23 3/21 5/14 > 234 txt 01/31 > 543 text 02/28 05/15 > > I then want the variables inserted into a statement. So for the first > line of text there would be three statements: > > upload data set date=aItem2 where id=aItem1 > upload data set date=aItem3 where id=aItem1 > upload data set date=aItem4 where id=aItem1 > > For the second line of text there would be only one statement: > > upload data set date=aItem2 where id=aItem1 If you are tempted to use 'numbered' variables like aItem1, aItem2, ... just resist and use arrays (or dictionaries) and loops: Dim aLines : aLines = Array( _ "123 text 10/23 3/21 5/14" _ , "234 txt 01/31" _ , "543 text 02/28 05/15" _ ) Dim sLine For Each sLine In aLines Dim aParts : aParts = Split( sLine, " " ) If 2 <= UBound( aParts ) Then Dim nIdx For nIdx = 2 TO UBound( aParts ) WScript.Echo "upload data set date=" & aParts( nIdx ) & " where id=" & aParts( 0 ) Next End If WScript.Echo Next output: === demoLoopArray: use loops and arrays instead of 'numbered' variables ======= upload data set date=10/23 where id=123 upload data set date=3/21 where id=123 upload data set date=5/14 where id=123 upload data set date=01/31 where id=234 upload data set date=02/28 where id=543 upload data set date=05/15 where id=543 === demoLoopArray: 0 done (00:00:00) ========================================== |
My System Specs![]() |
| | #3 (permalink) |
| | Re: picking items in array "James" <jwanders@newsgroup> wrote in message news:G_Dum.40$3G7.34@newsgroup Quote: > With previous help from here I have this script that splits each line into > text components using an array. I want to modify the script so it assigns > variables to the first text item in the line then assigns variables to the > third and following text items. Each line is of variable length like this > for example: > > 123 text 10/23 3/21 5/14 > 234 txt 01/31 > 543 text 02/28 05/15 > > I then want the variables inserted into a statement. So for the first > line of text there would be three statements: > > upload data set date=aItem2 where id=aItem1 > upload data set date=aItem3 where id=aItem1 > upload data set date=aItem4 where id=aItem1 > > For the second line of text there would be only one statement: > > upload data set date=aItem2 where id=aItem1 > > I guess I need some looping structure that just picks out the first item > in the line, skips the second, then picks the third and more items if > there are any. This is my old script: > > > Const csDlm = "," > Dim oFS : Set oFS = CreateObject( > "Scripting.FileSystemObject" ) > DIM aComponents > DIM aItem1 > DIM aItem2 > ' Check that a parameter was given: > ' -------------------------------- > > if wscript.arguments.count < 1 then > wscript.echo "This script requires 1 filename parameter" > wscript.quit 1 > end if > > ' Make sure the named file exists: > ' ------------------------------- > > strOrigName = wscript.arguments(0) > > if oFS.FileExists( strOrigName ) = False then > wscript.echo "File " & strOrigName & " does not exist!" > wscript.quit 2 > end if > > ' Construct a copy of the source file name > ' -------------------------------------- > > strFolder = oFS.GetParentFolderName( strOrigName ) > strFileName = oFS.GetFileName( strOrigName ) > sSrcFSpec = strFolder & "/" & strFileName > > > > 'Construct a copy of the destination files > '---------------------------------------- > > > sDstFSpec = strFolder & "/" & "updateQuery.txt" > Set tsDst = oFS.CreateTextFile( sDstFSpec, True ) > > 'Open new temporary text file for processing > '-------------------------------------------- > > Set tsSrc = oFS.OpenTextFile(sSrcFSpec) > > 'Loop through file line by line > '------------------------------- > > Do Until tsSrc.AtEndOfStream > Dim sLine : sLine = Trim( tsSrc.ReadLine() ) > aComponents=split(sLine, csDlm ) > aItem1=aComponents(0) > aItem2=aComponents(1) > wscript.echo "String 1 = " & aItem1 > wscript.echo "String 2 = " & aItem2 > tsDst.writeline "update employee set employeeid=" & aItem2 & " where id=" > & aItem2 > Loop > tsSrc.close > tsDst.close to fix, e.g. - You specify a comma as a delimiter but your data sample uses spaces. - You use forward slashes when constructing your file/folder names but Windows requires backslashes. - If your data file happens to be in the root of a partition (e.g. c:\MyData.txt) then sSrcFSpec and sDstFSpec will end up with two slashes like so: \\. |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Fast copy method of sub array (=array range) possible? | VB Script | |||
| Computer not picking up archos | .NET General | |||
| Stupid Array Tricks: Initializing an Array to a Certain Size | PowerShell | |||
| Microphone only picking up static | Sound & Audio | |||
| how to assign values to array and how to create array via variable | PowerShell | |||