dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Parsing string...

  1. #1
    Kevin Guest

    Parsing string...


    alright, still having the problems, so here we go. We type in (command prompt):
    example /s hhhhhhhhhhhh /f c:\test\test.dat
    Example is the name of the program (conveniently placed in the c drive)
    /s is supposed to say this is the variable that we will search for.
    /f is the location of the file in which we want this search.
    Hence we want to search for hhhhhhhhhhh in c:\test\test.dat
    Since we have not inputted in /m ,/r , or /p, we want the program to automatically
    enter in this data. When we try it it only gets 3 h's and only returns the
    c:\ and it only returns this in the case else clause. Please help, the code
    is placed below. We are writing to file (My Debug.txt) just for debugging
    purposes, that will be erased when we can get this to work. Thanks
    Kevin

    Dim astrArgv() As String
    Dim intArgc As Integer
    Dim intArgcIndex As Integer = 0
    Dim strArgv As String
    Dim strData, strData2, strData3, strData4, strData5 As String
    Dim strPrefix As String
    astrArgv = System.Environment.GetCommandLineArgs
    intArgc = astrArgv.GetUpperBound(0)
    ' WriteLine("Argc = {0}", intArgc)
    strp = ""
    strm = ""
    strr = ""

    For Each strArgv In astrArgv
    intArgcIndex += 1
    ' WriteLine("argv = '{0}'", strArgv)
    If (intArgcIndex <> 1) Then
    If (strArgv.Length > 3) Then
    strPrefix = strArgv.Substring(0, 3)
    Select Case strPrefix
    Case "/s "
    strData5 = strArgv.Substring(3)
    strSearchFor = strData5
    FileOpen(5, "C:\My DEBUG.txt", OpenMode.Append)
    WriteLine(5, "case S : " & strSearchFor)
    FileClose(5)
    Case "/f "
    strData = strArgv.Substring(3)
    strFile = strData
    FileOpen(5, "C:\My DEBUG.txt", OpenMode.Append)
    WriteLine(5, "case f : " & strFile)
    FileClose(5)
    Case "/r "
    strData2 = strArgv.Substring(3)
    strr = strData2
    FileOpen(5, "C:\My DEBUG.txt", OpenMode.Append)
    WriteLine(5, "case r : " & strr)
    FileClose(5)
    Case "/p "
    strData3 = strArgv.Substring(3)
    strp = strData3
    FileOpen(5, "C:\My DEBUG.txt", OpenMode.Append)
    WriteLine(5, "case p : " & strp)
    Case "/m "
    strData4 = strArgv.Substring(3)
    strTxnMerge = strData4
    FileOpen(5, "C:\My DEBUG.txt", OpenMode.Append)
    WriteLine(5, "case m : " & strm)
    Case Else
    FileOpen(5, "C:\My DEBUG.txt", OpenMode.Append)
    WriteLine(5, "case else : " & strSearchFor)
    FileClose(5)
    End Select
    End If
    End If
    Next

    If strr = "" Then strr = "r"
    If strp = "" Then strp = "p"
    If strm = "" Then strm = "m"

    Once again, thanks for the help!

  2. #2
    Frank Oquendo Guest

    Re: Parsing string...

    Kevin wrote:
    > alright, still having the problems, so here we go. We type in
    > (command prompt): example /s hhhhhhhhhhhh /f c:\test\test.dat
    > Example is the name of the program (conveniently placed in the c
    > drive) /s is supposed to say this is the variable that we will search
    > for. /f is the location of the file in which we want this search.
    > Hence we want to search for hhhhhhhhhhh in c:\test\test.dat
    > Since we have not inputted in /m ,/r , or /p, we want the program to
    > automatically enter in this data. When we try it it only gets 3 h's
    > and only returns the c:\ and it only returns this in the case else
    > clause. Please help, the code is placed below. We are writing to
    > file (My Debug.txt) just for debugging purposes, that will be erased
    > when we can get this to work.


    Drop the use of SubString. Instead, Split the string using a space as your
    delimiter. The first element of the returned array will the switch itself
    while the second element will be the value for that switch.

    --
    "If you want to be somebody else change your mind"
    http://www.acadx.com



  3. #3
    Kevin Guest

    Re: Parsing string...


    Hey Frank, thanks. Still having problems though. How do you use split and
    match each delimiter with a value. For example, with /s hhhhhhhhhhhh I want
    the hhhhhhhhhhhh to be the /s and thus the strSearchFor. Thanks for the
    help. (Currently, Split(strArgv, " ") brings an error).
    Kevin

    "Frank Oquendo" <nospam@please.com> wrote:
    >Kevin wrote:
    >> alright, still having the problems, so here we go. We type in
    >> (command prompt): example /s hhhhhhhhhhhh /f c:\test\test.dat
    >> Example is the name of the program (conveniently placed in the c
    >> drive) /s is supposed to say this is the variable that we will search
    >> for. /f is the location of the file in which we want this search.
    >> Hence we want to search for hhhhhhhhhhh in c:\test\test.dat
    >> Since we have not inputted in /m ,/r , or /p, we want the program to
    >> automatically enter in this data. When we try it it only gets 3 h's
    >> and only returns the c:\ and it only returns this in the case else
    >> clause. Please help, the code is placed below. We are writing to
    >> file (My Debug.txt) just for debugging purposes, that will be erased
    >> when we can get this to work.

    >
    >Drop the use of SubString. Instead, Split the string using a space as your
    >delimiter. The first element of the returned array will the switch itself
    >while the second element will be the value for that switch.
    >
    >--
    >"If you want to be somebody else change your mind"
    >http://www.acadx.com
    >
    >



  4. #4
    Frank Oquendo Guest

    Re: Parsing string...

    Kevin wrote:
    > Hey Frank, thanks. Still having problems though. How do you use
    > split and match each delimiter with a value. For example, with /s
    > hhhhhhhhhhhh I want the hhhhhhhhhhhh to be the /s and thus the
    > strSearchFor. Thanks for the help. (Currently, Split(strArgv, " ")
    > brings an error).


    Dim separators() As Char = {" "}
    Dim tokens() As String = strArgv.Split(separators)
    ' tokens(0) is the switch, tokens(1) is the value

    --
    "If you want to be somebody else change your mind"
    http://www.acadx.com



  5. #5
    Frank Oquendo Guest

    Re: Parsing string...

    Frank Oquendo wrote:

    > Dim separators() As Char = {" "}
    > Dim tokens() As String = strArgv.Split(separators)
    > ' tokens(0) is the switch, tokens(1) is the value


    Oops. Only use Split if your command line arguments look like this:
    /f:AnArg. GetCommandLinArgs will return the entire string as a single
    argument. If you're using spaces (/f AnArg), GetCommandLineArgs will see
    that as *two* arguments instead of one. Adjust your parsing loop
    accordingly.

    To be safe, you check to see if any switch has a length greater than two.

    --
    "If you want to be somebody else change your mind"
    http://www.acadx.com



  6. #6
    Larry Serflaten Guest

    Re: Parsing string...

    "Kevin" <kevin@kevin.com> wrote
    >
    > alright, still having the problems, so here we go.


    Unless you never touched a program before, I find that hard to believe.
    If you can't get by the simple tasks, how are you going to do the tough ones?

    Have a look at what it takes:

    Private strP, strM, strR As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
    Dim args() As String
    Dim arg As String
    Dim idx As Integer

    args = System.Environment.GetCommandLineArgs
    For idx = 0 To args.GetUpperBound(0)
    arg = args(idx)
    If arg.Length = 2 AndAlso arg.Substring(0, 1) = "/" Then
    AddToDebugFile(arg, args(idx + 1))
    Select Case arg.ToLower
    Case "/s"
    Case "/f"
    Case "/r"
    strR = args(idx + 1)
    Case "/p"
    strP = args(idx + 1)
    Case "/m"
    strM = args(idx + 1)
    End Select
    End If
    Next
    End Sub

    Private Sub AddToDebugFile(ByVal Name$, ByVal Value$)
    Dim F As Integer = FreeFile()
    FileOpen(F, "d:\temp\debug.txt", OpenMode.Append)
    PrintLine(F, Name & " = " & Value)
    FileClose(F)
    End Sub




  7. #7
    Patrick Ireland Guest

    Re: Parsing string...


    Kevin,

    I suggest that you check the REGEX class. The problem you may encounter
    is
    how to scan for a string hhhhhhhhhhh that contains embedded blanks. This
    is just the kind of problem that REGEX easily can solve.

    Pat

    "Frank Oquendo" <nospam@please.com> wrote:
    >Frank Oquendo wrote:
    >
    >> Dim separators() As Char = {" "}
    >> Dim tokens() As String = strArgv.Split(separators)
    >> ' tokens(0) is the switch, tokens(1) is the value

    >
    >Oops. Only use Split if your command line arguments look like this:
    >/f:AnArg. GetCommandLinArgs will return the entire string as a single
    >argument. If you're using spaces (/f AnArg), GetCommandLineArgs will see
    >that as *two* arguments instead of one. Adjust your parsing loop
    >accordingly.
    >
    >To be safe, you check to see if any switch has a length greater than two.
    >
    >--
    >"If you want to be somebody else change your mind"
    >http://www.acadx.com
    >
    >



  8. #8
    Patrick Ireland Guest

    Re: Parsing string...


    Kevin,

    A little explaination of command line arguments appears to be in order.

    Let's consider you example:

    /s hhhhhhhhhhhh /f c:\test\test.dat

    This has 5 command line arguments

    argv[0] = program name (of little interest in this example)
    argv[1] = /s
    argv[2] = hhhhhhhhhhhh
    argv[3] = /f
    argv[5] = c:\test\test.dat

    Using the code snippet

    For Each strArgv In astrArgv
    intArgcIndex += 1
    If (intArgcIndex <> 1) Then
    If (strArgv.Length > 3) Then
    strPrefix = strArgv.Substring(0, 3)
    Select Case strPrefix
    Case "/s "
    strSearchFor = astrArgv(intArgcIndex)
    Case "/f "
    strFile = astrArgv(intArgcIndex)

    etc...

    Note that the intArgcIndex is absolute argument number, NOT, the
    relative, i.e., the index into astrArgv.

    So if you find /s as argument absolute 2 (relative or index postion 1)
    then the very next entry in the astrArgv is the argument to the /s
    and so to for /f, /r, etc.

    This being said, you must guarantee that the next argument actual exists,
    i.e., in Case "/s" there exists a next element of the astrArgv array.
    If you don't then an command line of only /s while die an unpleasant death.

    Pat




  9. #9
    Larry Serflaten Guest

    Re: Parsing string...


    "Patrick Ireland" <ireland@airmail.net> wrote
    > Kevin,
    >
    > I suggest that you check the REGEX class. The problem you may encounter
    > is
    > how to scan for a string hhhhhhhhhhh that contains embedded blanks. This
    > is just the kind of problem that REGEX easily can solve.
    >
    > Pat



    Good point....

    LFS




  10. #10
    Patrick Ireland Guest

    Re: Parsing string...


    Kevin,

    For your example, the easiest way to determine if you have a following
    argument is to check if the argument count is odd (not even). For example:

    intArgc = astrArgv.GetUpperBound(0)
    If ((intArgc And 1) = 0) Then
    WriteLine("Even")
    Return
    Else
    WriteLine("Odd")
    End If

    Since the 1st argument is the file name and all your other entries are
    pairs, e.g., /s hhhhhhhhhhhhh, then the number of arguments must be odd.

    Pat

  11. #11
    Patrick Ireland Guest

    Re: Parsing string...


    Frank,

    Your solution is what I would do, however, since he only gave examples
    of argument pairs which I extended to require an odd number of command line
    arguments.

    As you state a better general solution is not to require pairs, in which
    case there may or may not be a next argument in the command line depending
    on the type of the current argument.

    It would be quite simple to incorporate this test, but given the difficulty
    that Kevin is having - KISS (Keep It Simple Stupid) is the rule to obey.

    Clearly you know what KISS is but I added the definition for a lot of the
    newbies that read this newsgroup.

    Pat


    "Frank Oquendo" <nospam@please.com> wrote:
    >Patrick Ireland wrote:
    >
    >> Since the 1st argument is the file name and all your other entries are
    >> pairs, e.g., /s hhhhhhhhhhhhh, then the number of arguments must be
    >> odd.

    >
    >What if a switch has no value (either by mistake or design)? e.g.: /s /f
    >SomeData.
    >
    >Wouldn't it be best to check the string to see if contains the switch
    >delimiter. If it doesn't then you can assume it's the value for the
    >preceding switch.
    >
    >--
    >"If you want to be somebody else change your mind"
    >http://www.acadx.com
    >
    >



  12. #12
    Frank Oquendo Guest

    Re: Parsing string...

    Patrick Ireland wrote:

    > Since the 1st argument is the file name and all your other entries are
    > pairs, e.g., /s hhhhhhhhhhhhh, then the number of arguments must be
    > odd.


    What if a switch has no value (either by mistake or design)? e.g.: /s /f
    SomeData.

    Wouldn't it be best to check the string to see if contains the switch
    delimiter. If it doesn't then you can assume it's the value for the
    preceding switch.

    --
    "If you want to be somebody else change your mind"
    http://www.acadx.com



  13. #13
    Larry Serflaten Guest

    Re: Parsing string...

    "Patrick Ireland" <ireland@airmail.net> wrote
    > It would be quite simple to incorporate this test, but given the difficulty
    > that Kevin is having - KISS (Keep It Simple Stupid) is the rule to obey.
    >
    > Clearly you know what KISS is but I added the definition for a lot of the
    > newbies that read this newsgroup.


    Gee, thanks!

    <g>
    LFS



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center