Results 1 to 11 of 11

Thread: If statement [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Location
    Chicago
    Posts
    136

    If statement [RESOLVED]

    I have a loop that goes through a logfile line by line and does certain events if certain strings are in that line. My question is how do I get it to skip to the next line if it contains a certain string? I'm trying to use If statements like below, I'm just not sure how to do it.



    VB Code:
    1. Do While Not EOF(1)
    2.     Line Input #1, strReadLine
    3.  
    4.     If InStr(blah blah) Then
    5.         'Do some stuff
    6.     End If
    7.  
    8.     If InStr(other blah blah) Then
    9.         'Go to next Line
    10.     End If
    11. Loop
    Last edited by bat711; Apr 19th, 2005 at 01:49 PM.

  2. #2
    Lively Member
    Join Date
    Apr 2005
    Posts
    68

    Re: If statement

    Just allow it to go to the loop which will move on to the next line. If I misunderstood you, please post a better explanation

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Location
    Chicago
    Posts
    136

    Re: If statement

    Actually now that I look at my example it was a bad one, sorry...

    It's nested actually, would look more like this:


    VB Code:
    1. Do While Not EOF(1)
    2.     Line Input #1, strReadLine
    3.  
    4.     If InStr(1, strReadLine, "S ") Then
    5.         If InStr(1, strReadLine, "grep") Then
    6.             'Go to next Line
    7.         End If
    8.  
    9.         If InStr(1, strReadLine, "/") Then
    10.             'Do some events
    11.         End If
    12.     End If
    13. Loop

    So how do I get it out of that nested If?

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: If statement

    If I understand you correctly, you can just use an ELSE

    VB Code:
    1. Do While Not EOF(1)
    2.     Line Input #1, strReadLine
    3.  
    4.     If InStr(1, strReadLine, "S ") Then
    5.         If InStr(1, strReadLine, "grep") Then
    6.             'Go to next Line
    7.         else
    8.           If InStr(1, strReadLine, "/") Then
    9.               'Do some events
    10.           End If
    11.         endif
    12.     End If
    13. Loop

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: If statement

    Why not just do
    VB Code:
    1. Do While Not EOF(1)
    2.     Line Input #1, strReadLine
    3.  
    4.     If InStr(1, strReadLine, "S ") Then
    5.         If InStr(1, strReadLine, "grep") Then
    6.             'Go to next Line
    7.              Loop
    8.         End If
    9.  
    10.         If InStr(1, strReadLine, "/") Then
    11.             'Do some events
    12.         End If
    13.     End If
    14. Loop

  6. #6
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Re: If statement

    I'm not sure if I get you either, but if I do, something like this would work:

    VB Code:
    1. Do While Not EOF(1)
    2.     Line Input #1, strReadLine
    3.     If InStr(1, strReadLine, "S ") Then
    4.         If InStr(1, strReadLine, "/") Then
    5.             'Do some events
    6.         ElseIf InStr(1, strReadLine, "grep") <= 0 Then
    7.             Exit Do
    8.             'So unless there's "grep", it won't continue.
    9.         End If
    10.     End If
    11. Loop

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Location
    Chicago
    Posts
    136

    Re: If statement

    Ok, using the else statement worked, thanks. Hack, I'm not sure that would work for my case because if "grep" is in the line I don't want to do anything with it or than go to the next line and it shouldn't be in the file more than once.

    Thanks guys.

  8. #8
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011

    Re: If statement

    Quote Originally Posted by bat711
    I have a loop that goes through a logfile line by line and does certain events if certain strings are in that line. My question is how do I get it to skip to the next line if it contains a certain string? I'm trying to use If statements like below, I'm just not sure how to do it.



    VB Code:
    1. Do While Not EOF(1)
    2.     Line Input #1, strReadLine
    3.  
    4.     If InStr(blah blah) Then
    5.         'Do some stuff
    6.     End If
    7.  
    8.     If InStr(other blah blah) Then
    9.         'Go to next Line
    10.     End If
    11. Loop

    Or keeping your structure the same...
    VB Code:
    1. Do While Not EOF(1)
    2.     Line Input #1, strReadLine
    3.  
    4.     If InStr(blah blah) Then
    5.         'Do some stuff
    6.         GoTo LoopEnd
    7.     End If
    8.  
    9.     If InStr(other blah blah) Then
    10.         'Go to next Line
    11.         GoTo LoopEnd
    12.     End If
    13. LoopEnd:
    14. Loop


    P.S. well goto statements are not very good to be used often...

  9. #9
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: If statement

    Quote Originally Posted by Hack
    Why not just do
    VB Code:
    1. Do While Not EOF(1)
    2.     Line Input #1, strReadLine
    3.  
    4.     If InStr(1, strReadLine, "S ") Then
    5.         If InStr(1, strReadLine, "grep") Then
    6.             'Go to next Line
    7.              Loop
    8.         End If
    9.  
    10.         If InStr(1, strReadLine, "/") Then
    11.             'Do some events
    12.         End If
    13.     End If
    14. Loop
    I think that will only work in C. Are you telling that this code compiles? (I've not tested).
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Location
    Chicago
    Posts
    136

    Re: If statement [RESOLVED]

    Yes that is what I was trying to figure out a sort of Go to statement. I Haven't used that since regular BASIC I think.

  11. #11
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: If statement

    I believe the easier way is:

    VB Code:
    1. Do While Not EOF(1)
    2.     Line Input #1, strReadLine
    3.  
    4.     If InStr(1, strReadLine, "S ") > 0 And InStr(1, strReadLine, "grep") = 0 Then
    5.         If InStr(1, strReadLine, "/") Then
    6.             'Do some events
    7.         End If
    8.     End If
    9. Loop
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

Posting Permissions

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



Click Here to Expand Forum to Full Width