Jump to content

if doesn't return every string every time


Recommended Posts

Hi,

i have this script

For $iii=1 To _FileCountLines("stat.txt")
    $sLine = FileReadLine("stat.txt",$iii)

 $GAhti = $iii - 3
 $GAati = $iii + 3
 $BPhti = $iii - 3
 $BPati = $iii + 3
 $SOGhti = $iii - 3
 $SOGati = $iii + 3
 $SOThti = $iii - 3
 $SOTati = $iii + 3
 $BShti = $iii - 3
 $BSati = $iii + 3

        If StringInStr($sLine,"Goal Attempts") <> 0 Then
        $GAht = FileReadLine("stat.txt",$GAhti)
        $GAat = FileReadLine("stat.txt",$GAati)
        EndIf
        If StringInStr($sLine,"Ball Possession") <> 0 Then
        $BPht = FileReadLine("stat.txt",$BPhti)
        $BPat = FileReadLine("stat.txt",$BPati)
        EndIf
        If StringInStr($sLine,"Shots on Goal") <> 0 Then
        $SOGht = FileReadLine("stat.txt",$SOGhti)
        $SOGat = FileReadLine("stat.txt",$SOGati)
        EndIf
        If StringInStr($sLine,"Shots off Goal") <> 0 Then
        $SOTht = FileReadLine("stat.txt",$SOThti)
        $SOTat = FileReadLine("stat.txt",$SOTati)
        EndIf
        If StringInStr($sLine,"Blocked Shots") <> 0 Then
        $BSht = FileReadLine("stat.txt",$BShti)
        $BSat = FileReadLine("stat.txt",$BSati)
        EndIf
        If $BPht <> "" Then
        MsgBox(1,"", "Ball possession HT" & ': ' & $BPht & @LF & "Goal attempts HT" & ': ' & $GAht & @LF & "Shots on Goal HT" & ': ' & $SOGht & @LF)
        EndIf
        MsgBox(1,"",$GAht)
        MsgBox(1,"",$SOGht)

        $GAht = ""
$GAat = ""
$BPht = ""
$BPat = ""
$SOGht = ""
$SOGat = ""
$SOTht = ""
$SOTat = ""
$BSht = ""
$BSat = ""

Next


;-----------------------------------------------------------------------------
;-----------------------------------------------------------------------------
EndIf
_IEQuit($aaaa)
Next

in this way, msgbox returns literally empty x times .... 
if i remove those lower 2 msgboxes, msgbox returns only the string that's in IF ....

which means 

If $BPht <> "" Then

returns only $BPht value in msgbox,

If $GAht <> "" Then

returns only $GAht in msbox,

Could you please help me with it?

Link to comment
Share on other sites

 

 

 

 

 

 


Match

1st Half

2nd Half

 

 

 

 

49%

 
Ball Possession 


51% 


13

 
Goal Attempts 


14 


3

 
Shots on Goal 



3

 
Shots off Goal 



7

 
Blocked Shots 



9

 
Free Kicks 


17 


1

 
Corner Kicks 



7

 
Offsides 



18

Link to comment
Share on other sites

You should first remove empty lines, then read the text to an array and loop through this array

#include <Array.au3>

$txt = StringRegExpReplace(FileRead("stats.txt"), '(?m)^\s*\R', "")
;Msgbox(0,"", $txt)   ; for preview
$array = StringSplit($txt, @crlf, 1)
;_ArrayDisplay($array)   ; for preview

Local $goalattempts    ; example
For $i = 1 to $array[0]
   If StringInStr($array[$i], "Goal Attempts") Then $goalattempts &= $array[$i-1]
Next
Msgbox(0,"", "goal attempts : " & $goalattempts)

Edit
BTW can't _IE* funcs give you a better result than this file ?

Edited by mikell
Link to comment
Share on other sites

You should first remove empty lines, then read the text to an array and loop through this array

#include <Array.au3>

$txt = StringRegExpReplace(FileRead("stats.txt"), '(?m)^\s*\R', "")
;Msgbox(0,"", $txt)   ; for preview
$array = StringSplit($txt, @crlf, 1)
;_ArrayDisplay($array)   ; for preview

Local $goalattempts    ; example
For $i = 1 to $array[0]
   If StringInStr($array[$i], "Goal Attempts") Then $goalattempts &= $array[$i-1]
Next
Msgbox(0,"", "goal attempts : " & $goalattempts)

Edit
BTW can't _IE* funcs give you a better result than this file ?

and i've got same outcome...
 

; find the line that has the search string
$txt = StringRegExpReplace(FileRead("stat.txt"), '\s{2,}', @crlf)
;Msgbox(0,"", $txt)   ; for preview
$array = StringSplit($txt, @crlf, 1)
;_ArrayDisplay($array)   ; for preview

    $goalattempts=""
    $BallPossession=""
    $ShotsonGoal=""
    $ShotsoffGoal=""
For $p = 1 to $array[0]
    If StringInStr($array[$p], "Goal Attempts") Then
    $goalattempts = $array[$p-1]
    Else
    EndIf
    If StringInStr($array[$p], "Ball Possession") Then
    $BallPossession = $array[$p-1]
    Else
    EndIf
    If StringInStr($array[$p], "Shots on Goal") Then
    $ShotsonGoal = $array[$p-1]
    Else
    EndIf
    If StringInStr($array[$p], "Shots off Goal") Then
    $ShotsoffGoal = $array[$p-1]
    Else
    EndIf

        If $BallPossession <> "" Then
        MsgBox(1,"", "Ball possession HT" & ': ' & $BallPossession & @LF & "Goal attempts HT" & ': ' & $goalattempts & @LF & "Shots on Goal HT" & ': ' & $ShotsonGoal & @LF)
        EndIf

Next
nvm, i just had to put msgbox outside of loop... :S Edited by Eggie6
Link to comment
Share on other sites

Anyway for better efficiency may I suggest this way

#include <Array.au3>

$txt = StringRegExpReplace(FileRead("stats.txt"), '(?m)^\s*\R', "")
$array = StringSplit($txt, @crlf, 1)
Local $Items[8] = ["Ball Possession", "Goal Attempts", "Shots on Goal", "Shots off Goal", "Blocked Shots", "Free Kicks", "Corner Kicks", "Offsides"]
_ArrayColInsert($Items, 1)
_ArrayColInsert($Items, 2)

For $i = 1 to $array[0]
   For $j = 0 to UBound($Items)-1
      If StringInStr($array[$i], $Items[$j][0]) Then 
             $Items[$j][1] = $array[$i-1]
             $Items[$j][2] = $array[$i+1]
      EndIf     
  Next
Next
_ArrayDisplay($Items)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...