Jump to content

Finding a Word in a file from the bottom up and outputting the contents of the line that the word is on


Recommended Posts

13 hours ago, mikell said:

Is there a way of protecting against when a name contains 'in'. For instance in the same code below with some names entered with 'in' in them, even thought the answer should be 'Out', the code is reading past that and hitting the 'in' in 'Katherine'. This is then reporting that Lorry is checked in rather than out. Is there a way of limiting the code to stopping reading as soon as it has read the first in/out in the line? Either that or to stop it at the 'by'?

See below for example:

#include <String.au3>

;~ Local $sFile = "Cars In-Out.txt"
;~ Local $sText = FileRead($sFile)

; for testing purpose, say instead that the file content is:
Local $sText = _
    "Car 1 checked out by Ben" & @CRLF & _
    "Lorry checked out by Rob" & @CRLF & _
    "Car 3 checked In by Sonja" & @CRLF & _
    "Car 5 checked out by Sam" & @CRLF & _
    "Lorry checked out by Robin" & @CRLF & _
    "Car 4 checked out by Sam" & @CRLF & _
    "Lorry checked out by Katherine" & @CRLF & _
    "Car 1 checked in by Ben" & @CRLF & _
    "Car 123 checked Out by Sonja" & @CRLF

Local $sCarSearched = "lorry"  ; "Car 1"

$sFound = StringRegExpReplace($sText, '(?is).*' & $sCarSearched & '\h\N*(In|Out).*', "$1")
Msgbox(0,"", $sCarSearched & " is " & $sFound)

 

13 hours ago, mikell said:
#include <String.au3>

;~ Local $sFile = "Cars In-Out.txt"
;~ Local $sText = FileRead($sFile)

; for testing purpose, say instead that the file content is:
Local $sText = _
    "Car 1 checked out by Ben" & @CRLF & _
    "Lorry checked out by Rob" & @CRLF & _
    "Car 3 checked In by Sonja" & @CRLF & _
    "Car 5 checked out by Sam" & @CRLF & _
    "Lorry checked In by Rob" & @CRLF & _
    "Car 4 checked out by Sam" & @CRLF & _
    "Lorry checked out by Joe" & @CRLF & _
    "Car 1 checked in by Ben" & @CRLF & _
    "Car 123 checked Out by Sonja" & @CRLF

Local $sCarSearched = "lorry"  ; "Car 1"

$sFound = StringRegExpReplace($sText, '(?is).*' & $sCarSearched & '\h\N*(In|Out).*', "$1")
Msgbox(0,"", $sCarSearched & " is " & $sFound)

 

 

Link to comment
Share on other sites

Yes of course: change (In|Out) to \b(In|Out)\b

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Or you can make the \N* lazy :  \N*?

$sFound = StringRegExpReplace($sText, '(?is).*' & $sCarSearched & '\h\N*?(In|Out).*', "$1")

Or you can be stricly literal, as the lines seem all formatted in the same way

$sFound = StringRegExpReplace($sText, '(?is).*' & $sCarSearched & '\h+checked\h+([inout]+).*', "$1")
; add some error checking
Msgbox(0,"", (@extended=0) ? "error" : $sCarSearched & " is " & $sFound)

Many ways to skin this cat  :)

Link to comment
Share on other sites

  • 5 months later...

I'm replying back to this thread as I am looking back at this project and trying to make some corrections and would love some further help. I am attaching the two test files I use which are just kept in the root of C and my current code.

My final issue seems to be when I run the Audit button on laptop 1 I get the results for laptop 12. I realize that this is because the script is reading up the list until it finds laptop 1, but it does not account for the fact there is a 2 straight after it (laptop 12). How can I modify my current script to read the two digits rather than one?

 

 

 

Edited by boltonebob
Link to comment
Share on other sites

Changing line 102 to:

            If StringInStr($aMachines[$i], $sMachine & ' ') Then Return $aMachines[$i]

willdo. But your code structure is awful, sorry. First move functions outside loops. Then parametrize the machine number, else you'll have to rewrite code when/if it changes. Ask yourself "how would I do it if there were 950 laptops?"

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Thank you very much for your feedback. I agree my coding is probably awful. I am truly a novice at coding, and I get to the point that something works and then can't always see a way to 'tidy' it up. I still consider myself at the fumbling around in the dark period, where sometimes I come across a light switch. Perhaps at some point I will see if someone has the time to teach me how to do this.

Link to comment
Share on other sites

Here is a 5 minute try, to give you some ideas  (500 lines less, it's worth a try)  :)

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.12.0
 Date: 06/10/16
 Script Function: Loaner Laptop Log

#ce ----------------------------------------------------------------------------

#include <File.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <String.au3>


;GUI
#Region ### START Koda GUI section ### Form=c:\users\athenos\desktop\form1.kxf
$Form1_1 = GUICreate("Loaner Laptop Log", 361, 210, Default, Default)
$List = GUICtrlCreateCombo("", 128, 16, 90, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "|LAPTOP 1|LAPTOP 2|LAPTOP 3|LAPTOP 4|LAPTOP 5|LAPTOP 6|LAPTOP 7|LAPTOP 8|LAPTOP 9|LAPTOP 10|LAPTOP 11|LAPTOP 12")
$Label1 = GUICtrlCreateLabel("Machine:", 64, 21, 48, 17)
$Label2 = GUICtrlCreateLabel("Customer:", 64, 61, 51, 17)
$Username = GUICtrlCreateInput("", 128, 56, 137, 21)
$Button1 = GUICtrlCreateButton("Machine In", 36, 149, 153, 46)
$Button2 = GUICtrlCreateButton("Machine Out", 36, 96, 153, 46)
$Button3 = GUICtrlCreateButton("Audit", 216, 149, 105, 46)
$Button4 = GUICtrlCreateButton("Check on Status", 216, 96, 105, 46)
$Button5 = GUICtrlCreateButton("LAT?", 232, 15, 41, 25)
$Button6 = GUICtrlCreateButton("Update", 280, 15, 41, 25)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

;---------------------------------------------------------------------------------------------------------------------------------------

While 1
    $Msg = GUIGetMsg()
Select
        Case $Msg = $GUI_EVENT_CLOSE
            Exit

;---------------------------------------------------------------------------------------
        Case $Msg = $Button1
;---------------------------------------------------------------------------------------
            ;Open the file for writing
            $file = FileOpen("C:\TEST.txt", 1)

            ;Process after button press
            If GUICtrlRead($List) = ""  Then 
                MsgBox (16,"ERROR", "Please select a loaner laptop machine number")
            Else
                stateIn(GUICtrlRead($List))
            EndIf

;----------------------------------------------------------------------------------------
        Case $MSg = $Button2
;----------------------------------------------------------------------------------------
            ;Open the file for writing
            $file = FileOpen("C:\TEST.txt", 1)

            ;Process after button press
            If GUICtrlRead($List) = ""  Then 
                MsgBox (16,"ERROR", "Please select a loaner laptop machine number")
            Else
                stateOut(GUICtrlRead($List))
            EndIf

;----------------------------------------------------------------------------------------
         Case $MSg = $Button3
;----------------------------------------------------------------------------------------
            ;Open the file for writing
            $file = FileOpen("C:\TEST.txt", 1)

            ;Process after button press
            Global $aMachines, $sMachine = GUICtrlRead($List)
            _FileReadToArray('C:\TEST.txt', $aMachines)
            $sFound = _SearchLastState($sMachine)
            MsgBox($MB_ICONINFORMATION, "Last Status for "&$sMachine&" ", $sFound)


;-----------------------------------------------------------------------------------------
         Case $MSg = $Button4
;-----------------------------------------------------------------------------------------
        ;Open the file for writing
        $file = FileOpen("C:\TEST.txt", 1)

        ;Process after button press
        $sFile = "C:\TEST.txt"
        Local $sText = FileRead($sFile)
        $sMachineSearched = GUICtrlRead($List)

        $sFound = StringRegExpReplace($sText, '(?is).*' & $sMachineSearched & '\h\N*\b(In|Out)\b.*', "$1")
        Msgbox(0,"", $sMachineSearched & " is " & $sFound)

;-----------------------------------------------------------------------------------------
      Case $MSg = $Button5
;-----------------------------------------------------------------------------------------

;Open the file for writing
            $file = FileOpen("C:\TEST2.txt", 1)

;Process after button press
            Global $aLATs, $sLAT = GUICtrlRead($List)
            _FileReadToArray('C:\TEST2.txt', $aLATs)
            $sFound = _SearchLastLap($sLAT)
            MsgBox($MB_ICONINFORMATION, "Machine Number for "&$sLAT&" ", $sFound)


;-----------------------------------------------------------------------------------------
      Case $MSg = $Button6
;-----------------------------------------------------------------------------------------
;2nd GUI pop-up for updating LAT number when a machine changes
;-----------------------------------------------------------------------------------------

#Region ### START Koda GUI section ### Form=C:\Users\Athenos\Desktop\Prod\Link Program.kxf
$Form1_1 = GUICreate("Update LAT number", 267, 141, Default, Default)
$List = GUICtrlCreateCombo("", 160, 16, 89, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "|LAPTOP 1|LAPTOP 2|LAPTOP 3|LAPTOP 4|LAPTOP 5|LAPTOP 6|LAPTOP 7|LAPTOP 8|LAPTOP 9|LAPTOP 10|LAPTOP 11|LAPTOP 12")
$Label1 = GUICtrlCreateLabel("Confirm machine to change:", 16, 21, 136, 17)
$Label2 = GUICtrlCreateLabel("Enter new laptop number:", 16, 61, 125, 17)
$UserInput = GUICtrlCreateInput("", 160, 56, 89, 21)
$Button9 = GUICtrlCreateButton("Submit Change", 80, 92, 107, 33)

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $Msg = GUIGetMsg()
    Select
      Case $Msg = $GUI_EVENT_CLOSE
            Exit

Case $Msg = $Button9


    $file = FileOpen("C:\TEST2.txt", 1)

            ;Process after button press
            If GUICtrlRead($List) = ""  Then 
                MsgBox (16,"ERROR", "Please select a loaner laptop machine number")
            Else
                stateEdit(GUICtrlRead($List))
            EndIf
    EndSelect
WEnd

Exit
    EndSelect
WEnd


;------------------------------------------------------------------------------------------------------------------------------
;Functions
;------------------------------------------------------------------------------------------------------------------------------

Func _SearchLastState($sMachine)
    For $i = $aMachines[0] To 1 Step -1
        If StringInStr($aMachines[$i], $sMachine & ' ') Then Return $aMachines[$i] 
        ;If StringInStr($aMachines[$i], $sMachine) Then Return $aMachines[$i]
    Next
EndFunc

Func _SearchLastLap($sLAT)
    For $i = $aLATs[0] To 1 Step -1
        If StringInStr($aLATs[$i], $sLAT) Then Return $aLATs[$i]
    Next
EndFunc

;------------------------------------------------------------------------------------------------------------------------------
;Processing Check In of Machines
;------------------------------------------------------------------------------------------------------------------------------
Func stateIn($machine)
If $file = -1 Then
  MsgBox(0, "Error", "Unable to open file.")
Else
    If GuiCtrlRead($Username) = "" Then
    MsgBox (16,"ERROR", "Please enter the name of the person picking up or dropping off")
    Else
  FileWrite($file, $machine & " checked IN by " & GUICtrlRead($Username) & " on " & @mon & "/" & @mday & "/" & @year & " at " & @hour & ":" & @min & ". Entry logged by " & @UserName & "." & @CRLF)
  FileClose($file)
  MsgBox (64,"Processed", "Record Submitted")
Exit
  EndIF
EndIf
 EndFunc
;------------------------------------------------------------------------------------------------------------------------------
;Processing Check Out of Machines
;------------------------------------------------------------------------------------------------------------------------------
Func stateOut($machine)
If $file = -1 Then
  MsgBox(0, "Error", "Unable to open file.")
Else
    If GuiCtrlRead($Username) = "" Then
    MsgBox (16,"ERROR", "Please enter the name of the person picking up or dropping off")
    Else
  FileWrite($file, $machine & " checked OUT by " & GUICtrlRead($Username) & " on " & @mon & "/" & @mday & "/" & @year & " at " & @hour & ":" & @min & ". Entry logged by " & @UserName & "." & @CRLF)
  FileClose($file)
  MsgBox (64,"Processed", "Record Submitted")
Exit
  EndIF
EndIf
 EndFunc
;------------------------------------------------------------------------------------------------------------------------------
;Update Laptop number when machines are swapped out
;------------------------------------------------------------------------------------------------------------------------------
Func stateEdit($machine)
If $file = -1 Then
  MsgBox(0, "Error", "Unable to open file.")
Else
    If GuiCtrlRead($UserInput) = "" Then
    MsgBox (16,"ERROR", "Please enter the new LAT number")
    Else
  FileWrite($file, $machine & " = " & GUICtrlRead($UserInput) & @CRLF)
  FileClose($file)
  MsgBox (64,"Processed", "Update Submitted")
Exit
  EndIF
EndIf
 EndFunc

 

Edited by mikell
Link to comment
Share on other sites

That is so much simpler! Thank you mikell for taking the time to show me that. Lining the code up against each other helps me to see where I am going wrong with my chain of thought. I have only just started using functions with this little project, so this helps an immeasurable amount!

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...