Jump to content

detect text line ?????


 Share

Recommended Posts

hello everybody

I have this text file :

----------------
Results:
----------------
sara:woman
john:man
micheal:boy
sona:girl

I wanna detect the line number of any word such as boy , girl ... etc

this is what I did but couldn't know how to detect

POP()

Func POP()
    While Not FileExists("POP.txt")
        Return "Please wait"
    WEnd
    $file = FileOpen("POP.txt", 0)
    
    $line = FileReadLine($file ,4)
    MsgBox(0, "message", $line)
    $name = StringSplit($line,":")
    MsgBox(0, "message", $name[2] )
EndFunc

please help me

thanks

Link to comment
Share on other sites

  • Moderators

Global $gs_data = _POP("POP.txt")

If $gs_data = "Please Wait" Then
    MsgBox(16, "Error", "File does not exist")
ElseIf $gs_data Then
    MsgBox(64, "Info", $gs_data)
Else
    MsgBox(16, "Error", "No data found!")
EndIf

Func _POP($s_file = "POP.txt")
    If Not FileExists($s_file) Then
        Return "Please Wait"
    EndIf
    
    Local $s_fread = FileRead($s_file)
    Local $a_lines = StringSplit(StringStripCR($s_fread), @LF)
    
    Local $a_split, $s_out
    For $iline = 1 To $a_lines[0]
        $a_split = StringSplit($a_lines[$iline], ":")
        If $a_split[0] < 2 Then ContinueLoop
        
        If StringInStr("|boy|girl|", "|" & $a_split[2] & "|") Then
            $s_out &= "Line: " & $iline & @TAB & @TAB & "Found: " & $a_split[2] & @CRLF
        EndIf
    Next
    
    Return StringTrimRight($s_out, 2)
    
EndFunc

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

#Include <File.au3>
POP()

Func POP()
    $file = FileOpen("c:\pop.txt", 0)
    $Max = _FileCountLines ("c:\pop.txt")
    for $i = 1 to $Max
    $line = FileReadLine($file , $i)
    If StringRegExp ($line , "(boy)|(girl)" , 0) = 1 Then
    MsgBox(0, "message", "Line " & $i & "= " & $line)
endif
next
EndFunc

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

thanks so so much for help

but the main target is to get the line for this situation :

----------------
Results:
----------------
  sara:woman
    john:man
 micheal:boy
     sona:girl

something like that coz not all the texts begin from the first of the line

thanks again

Link to comment
Share on other sites

  • Moderators

#Include <File.au3>
POP()

Func POP()
    $file = FileOpen("c:\pop.txt", 0)
    $Max = _FileCountLines ("c:\pop.txt")
    for $i = 1 to $Max
    $line = FileReadLine($file , $i)
    If StringRegExp ($line , "(boy)|(girl)" , 0) = 1 Then
    MsgBox(0, "message", "Line " & $i & "= " & $line)
endif
next
EndFunc

That code is wrong in almost as many ways as there is lines of code.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

thus the Avatar.

What are the many things? I am sincerely seeking to improve but your critique offers nothing of substance, and the results of the script are the desired results.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

  • Moderators

thus the Avatar.

What are the many things? I am sincerely seeking to improve but your critique offers nothing of substance, and the results of the script are the desired results.

I'll start with 2 things that seriously stuck out and avoid syntax and or structure entirely.

1. You open a file with a handle, but you never close it.

2. You use the udf for counting lines, which opens/closes the file again itself, yet you use filereadline.

For an example that would accomplish the same thing, in less steps as well as assure far less issues, look at how my function starts out with FileRead() + StringSplit().

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

fair enough, I dont close the handle :)

However, I use the UDF to get the max number (indeed, a bonus open/close with literal path, which i suppose then should happen prior to the fileopen). Then I use filereadline with the handle so I would not catch the added cost of opening and closing each line read.

Thus, I am not quite understanding item 2 and how the UDF and filereadline are mutually exclusive, unless you are just referencing the one-time additional cost of opening and closing the file to count the lines.

Many thanks for the detail.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

  • Moderators

Ok, see if I can say this without being confusing. I've seemed to confuse everyone in my life one time or another today.

The issue I was trying to show is this. To open the file twice is redundant.

Lets break down your real code:

(1)

FileOpen('file')

(2)

_FileCountLines('file')
Func _FileCountLines($sFilePath)

    Local $hFile = FileOpen($sFilePath, $FO_READ)
    If $hFile = -1 Then Return SetError(1, 0, 0)
    Local $sFileContent = StringStripWS(FileRead($hFile), 2)
    FileClose($hFile)
    Local $aTmp
    If StringInStr($sFileContent, @LF) Then
        $aTmp = StringSplit(StringStripCR($sFileContent), @LF)
    ElseIf StringInStr($sFileContent, @CR) Then
        $aTmp = StringSplit($sFileContent, @CR)
    Else
        If StringLen($sFileContent) Then
            Return 1
        Else
            Return SetError(2, 0, 0)
        EndIf
    EndIf
    Return $aTmp[0]
EndFunc   ;==>_FileCountLines

(3)

For $i = 1 To $Max
    $line = FileReadLine($file , $i)
    If StringRegExp ($line , "(boy)|(girl)" , 0) = 1 Then
        MsgBox(0, "message", "Line " & $i & "= " & $line)
    EndIf
Next

Now, how I see that code actually written is ( this is probably what I'd do, because most files are traditional, and I don't trust regex on huge files, others will say different, but I've seen it fail ):

Func _Pop()
    Local $h_open = FileOpen('file')
    If $h_open = -1 Then Return SetError(1, 0, "")

    ; $a_lines[0] will hold number of lines in file
    ; $a_lines[n] to $a_lines[0] are lines in file
    Local $a_lines = StringSplit(StringStripCR(FileRead($h_open)), @LF)

    ; We don't need the file handle anymore
    FileClose($h_open)

    For $iline = 1 To $a_lines[0]
        If StringRegExp($a_lines[$iline], "(?::boy|:girl)(?:\z|\v)") Then
            ; Something here
        EndIf
    Next
EndFunc

Or, if you wanted to be true to the _FileCountLines() and you're not scared of regex:

Func _Pop()
    Local $h_open = FileOpen('file')
    If $h_open = -1 Then Return SetError(1, 0, "")

    ; 0 to UBound($a_lines) - 1 are lines in file
    Local $a_lines, $s_fread = FileRead($h_open)

    ; We don't need the file handle anymore
    FileClose($h_open)
    
    Local $s_fread_strip = StringStripCR($s_fread)

    If StringInStr($s_fread_strip, @LF) Then
        $a_lines = StringRegExp($s_fread_strip, "(.*?)(?:\z|\v)", 3)
    Else
        $a_lines = StringRegExp($s_fread, "(.*?)(?:\z|\v)", 3)
    EndIf

    ; If @error then there were no lines
    If @error Then Return SetError(2, 0, "")

    For $iline = 0 To UBound($a_lines) - 1
        If StringRegExp($a_lines[$iline], "(?::boy|:girl)(?:\z|\v)") Then
            ; Something here
            ConsoleWrite("Line: " & $iline + 1 & @TAB & @TAB & $a_lines[$iline] & @CRLF)
        EndIf
    Next
EndFunc

Or, if you just want to return the entire line that contains that data and don't actually care about the line number

#include <array.au3>; Used only for _arraydisplay, nothing relevant to code

Func _Pop()
    Local $s_pattern = "(?m:)(.+?(?::boy|:girl))(?:\z|\v)"
    Local $a_sre = StringRegExp(FileRead('file'), $s_pattern, 3)
    ; merely to show data
    _ArrayDisplay($a_sre)
EndFunc

Edit:

Oops, forgot to make my point.

Sometimes it's not enough that the code just works.

Sometimes speed is an issue ( opening and reading the file twice which yours does is not faster ) .

Sometimes structure is important ( easily maintainable and easy for you and others to edit if need be ).

Sometimes etc etc and so on.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

very well explained.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

  • Moderators

I still don't see why anyone would ever need to close the handle.

I see it as a wast of time since the script works w/o it.

If you wish to count on garbage pickup 100%, you're more than welcome to.

Myself, I try to cleanup after my own self when I'm capable of doing so.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

If you wish to count on garbage pickup 100%, you're more than welcome to.

Myself, I try to cleanup after my own self when I'm capable of doing so.

So what are you trying to say?

A file is created when the "Fileopen" command is used?

And if you don't close the handle, the file remains wherever it was created?

Link to comment
Share on other sites

  • Moderators

So what are you trying to say?

A file is created when the "Fileopen" command is used?

And if you don't close the handle, the file remains wherever it was created?

A "handle" is created.

Which in turn could lock the file from other apps or your own app from accessing it ( read or write ).

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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