Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
picking items in array
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
James  
View profile  
 More options Sep 24 2009, 6:05 am
Newsgroups: microsoft.public.scripting.vbscript
From: James <jwand...@lycos.com>
Date: Wed, 23 Sep 2009 23:05:26 -0700
Local: Thurs, Sep 24 2009 6:05 am
Subject: 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


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ekkehard.horner  
View profile  
 More options Sep 24 2009, 7:30 am
Newsgroups: microsoft.public.scripting.vbscript
From: "ekkehard.horner" <ekkehard.hor...@arcor.de>
Date: Thu, 24 Sep 2009 09:30:25 +0200
Local: Thurs, Sep 24 2009 7:30 am
Subject: Re: picking items in array
James schrieb:

[...]

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) ==========================================


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pegasus [MVP]  
View profile  
 More options Sep 24 2009, 7:54 am
Newsgroups: microsoft.public.scripting.vbscript
From: "Pegasus [MVP]" <n...@microsoft.com>
Date: Thu, 24 Sep 2009 09:54:24 +0200
Local: Thurs, Sep 24 2009 7:54 am
Subject: Re: picking items in array

"James" <jwand...@lycos.com> wrote in message

news:G_Dum.40$3G7.34@newsfe03.iad...

Further to Ekkehard's comments, there are a few minor problems that you need
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: \\.

    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James  
View profile  
 More options Nov 16 2009, 6:56 am
Newsgroups: microsoft.public.scripting.vbscript
From: James <jwand...@lycos.com>
Date: Sun, 15 Nov 2009 22:56:09 -0800
Local: Mon, Nov 16 2009 6:56 am
Subject: Re: picking items in array

Pegasus

Thanks for the advice. I meant to reply to this earlier.  With your
third bit of advice can you suggest how I can modify sSrcFSpec  and
sDstFSpec  so they don't end up with two slashes ('\\') if the data file
is in the root folder?  I explicitly put a slash ('\') in the variables
but is there a way around that?

Thanks,

James


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steve  
View profile  
 More options Nov 16 2009, 11:28 am
Newsgroups: microsoft.public.scripting.vbscript
From: "Steve" <cerberu...@gmail.com>
Date: Mon, 16 Nov 2009 06:28:03 -0500
Local: Mon, Nov 16 2009 11:28 am
Subject: Re: picking items in array

Unless you want to test all combinations (folder does/does not end with
a slash; file does/does not begin with a slash), the easiest way to
build paths is with the FileSystemObject's BuildPath method:

http://msdn.microsoft.com/en-us/library/z0z2z1zt%28VS.85%29.aspx

"The BuildPath method inserts an additional path separator between the
existing path and the name, only if necessary."

  Const PATH1 = "c:\temp\"
  Const PATH2 = "c:\temp"
  Const FOLDER1 = "\temp"
  Const FOLDER2 = "temp"

  Set FSO = CreateObject("Scripting.FileSystemObject")

  WScript.Echo FSO.BuildPath(PATH1, FOLDER1)
  WScript.Echo FSO.BuildPath(PATH1, FOLDER2)
  WScript.Echo FSO.BuildPath(PATH2, FOLDER1)
  WScript.Echo FSO.BuildPath(PATH2, FOLDER2)

Install the Windows Script Documentation:

http://www.microsoft.com/downloads/details.aspx?FamilyID=01592c48-207...

--
Steve

We should be taught not to wait for inspiration to start a thing. Action
always generates inspiration. Inspiration seldom generates action.
-Frank Tibolt


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google