Jump to content

Searching a text file


Recommended Posts

Hiya guys,

HAPPY NEW YEAR to all.

I have a problem that I'm guessing as usual is easy but nevertheless I'm stumped!

I've tried to give a simply example of whats going wrong...

I'm writing records to a text file using FileWriteLine. Say it has the following records:-

Liverpool

Man Utd

Barcelona

Real Madrid

Liverpool

Arsenal

Spurs

Then using FileRead I am retrieving information underneath my specified search. (So if I search for Man Utd, FileReadLine will return 'Barcelona') I then use this information in a table which displays the information (in this case 'Barcelona')!

However if I search for Liverpool, because it appears twice in my text file, it returns 'Man Utd' and then 'Arsenal'. And its only Arsenal that I manage to return to my table (using $var = FileReadLine($file))

How can I work it so that I can capture the line after each time Liverpool appears. So Man Utd will the result of one variable and Arsenal te result of the other???

Hope you see what I mean, I'm not sure I've explained it too well. Would appreciate any help!

Cheers guys

Mark

P.S. If you cant understand my rubbish explanation then let me know and I'll try to make it clearer rather than just ignoring the post entirely.

Link to comment
Share on other sites

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$number = 1

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    
    If $line = "Liverpool" Then
        $read = FileReadLine($file)
    EndIf
Wend
                
FileClose($file)

#include <GuiConstants.au3>

; GUI
GuiCreate("Sample GUI", 400, 400)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

; LIST
GuiCtrlCreateList("", 5, 190, 100, 90)
GuiCtrlSetData(-1, "a.Sample|b.List|c.Control|d.Here", "b.List")

; LIST VIEW
$listView = GuiCtrlCreateListView("Club|ListView|", 110, 190, 110, 80)
GuiCtrlCreateListViewItem($read1 & "|One", $listView)

; GUI MESSAGE LOOP
GuiSetState()
While GuiGetMsg() <> $GUI_EVENT_CLOSE
WEnd

Hey Chris,

Thanks for letting me know my explanation was useless, I thought it may be the case!

As you can see from above, I am using a FileReadLine function at the top to get my data from my text file (using 'If you find this then I want the next line' in my IF function) and then creating a Gui at the bottom to display this data!

Normally this works fine, however if the value I'm looking for appears more than once in my list then I want it to return the first value as $read1, the second as $read2 etc.... Rather than my Gui only being able to show the last instance.

I don't know if this is any clearer, it feels as clear as mud to me. Perhaps with the football team example I gave in my first post you'll understand what I mean???

Cheers

Mark

And if you want me to hav another bash at explaining it I will gladly, cos its doing my head in at the moment!!!!

Link to comment
Share on other sites

$file = FileOpen("test.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

$number = 1

; Read in lines of text until the EOF is reached
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    
    If $line = "Liverpool" Then
        $read = FileReadLine($file)
    EndIf
Wend
                
FileClose($file)

#include <GuiConstants.au3>

; GUI
GuiCreate("Sample GUI", 400, 400)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

; LIST
GuiCtrlCreateList("", 5, 190, 100, 90)
GuiCtrlSetData(-1, "a.Sample|b.List|c.Control|d.Here", "b.List")

; LIST VIEW
$listView = GuiCtrlCreateListView("Club|ListView|", 110, 190, 110, 80)
GuiCtrlCreateListViewItem($read1 & "|One", $listView)

; GUI MESSAGE LOOP
GuiSetState()
While GuiGetMsg() <> $GUI_EVENT_CLOSE
WEnd

Hey Chris,

Thanks for letting me know my explanation was useless, I thought it may be the case!

As you can see from above, I am using a FileReadLine function at the top to get my data from my text file (using 'If you find this then I want the next line' in my IF function) and then creating a Gui at the bottom to display this data!

Normally this works fine, however if the value I'm looking for appears more than once in my list then I want it to return the first value as $read1, the second as $read2 etc.... Rather than my Gui only being able to show the last instance.

I don't know if this is any clearer, it feels as clear as mud to me. Perhaps with the football team example I gave in my first post you'll understand what I mean???

Cheers

Mark

And if you want me to hav another bash at explaining it I will gladly, cos its doing my head in at the moment!!!!

try this...

#include<file.au3>
dim $contents
_FileReadToArray("test.txt",$contents)
$ToBeReturned = ""
for $x = 1 to $contents[0]
    If $contents[$x] = "Liverpool" Then $ToBeReturned = $ToBeReturned & "|" & $contents[$x+1]
Next
    
#include <GuiConstants.au3>

; GUI
GuiCreate("Sample GUI", 400, 400)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

; LIST
$thelist = GuiCtrlCreateList("", 5, 190, 100, 90)
GuiCtrlSetData($thelist, $ToBeReturned, "like this?")

; LIST VIEW
$anotherexample = StringSplit($ToBeReturned,"|")
$listView = GuiCtrlCreateListView("Club|ListView|", 110, 190, 110, 80)
For $y = 1 to $anotherexample[0]
GuiCtrlCreateListViewItem($anotherexample[$y],$listView)
Next
; GUI MESSAGE LOOP
GuiSetState()
While GuiGetMsg() <> $GUI_EVENT_CLOSE
WEnd
Link to comment
Share on other sites

Hey Cameronsdad,

I've been using your code and although it works fine for what it does. I've been trying to make a small modification. With your script I can get the data on the line after the one I specify. But I can't fathom how to get the information from the next two lines, or the next three.

In the end I want to use the script to look for a name in the text files and then return the five lines that appear after each instance of that number in my script.

Is it possible to do this in the way you have done it using a ReadArray function??

Any help much appreciated

Mark

Link to comment
Share on other sites

Hey Cameronsdad,

I've been using your code and although it works fine for what it does. I've been trying to make a small modification. With your script I can get the data on the line after the one I specify. But I can't fathom how to get the information from the next two lines, or the next three.

In the end I want to use the script to look for a name in the text files and then return the five lines that appear after each instance of that number in my script.

Is it possible to do this in the way you have done it using a ReadArray function??

Any help much appreciated

Mark

Try..

for $x = 1 to $contents[0]

If $contents[$x] = "Liverpool" Then $ToBeReturned = $ToBeReturned & "|" & $contents[$x+1] & "|" & $contents[$x+2] & "|" & $contents[$x+3] & "|" & $contents[$x+4] & "|" & $contents[$x+5]

Next

Link to comment
Share on other sites

Hey Cameronsdad,

I've been using your code and although it works fine for what it does. I've been trying to make a small modification. With your script I can get the data on the line after the one I specify. But I can't fathom how to get the information from the next two lines, or the next three.

In the end I want to use the script to look for a name in the text files and then return the five lines that appear after each instance of that number in my script.

Is it possible to do this in the way you have done it using a ReadArray function??

Any help much appreciated

Mark

i'm sorry, i swear i typed up a response to this the other day, not sure how it didn't get posted. ChrisL's suggestion is correct, by concatenating in the other elements that you want, and including the delimiter, they'll be added to both the list control and the listview box.
Link to comment
Share on other sites

Thanks guys, I've managed to use your ideas to do what I want. However now every time I try and run the script it gives me the following error message:-

for $x = 1 to $contents[0]

for $x = 1 to $contents^ERROR

Error: Subscript used with non-Array variable.

I've used the function before and it did work fine. I don't understand why it won't work now. Anybody got any suggestions what could be causing this error message??

Thanks

Mark

Link to comment
Share on other sites

  • Moderators

The $contents[0] at that time of run is not in an array... you could protect from a crash like this:

If IsArray($contents) Then
    For $x = 1 To $contents[0]
       ;stuff you do
    Next
EndIf

Or you may choose to replace all the $contents[0]:

For $x = 1 To Ubound($contents) - 1
   ;stuff you do
Next

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

The $contents[0] at that time of run is not in an array... you could protect from a crash like this:

If IsArray($contents) Then
    For $x = 1 To $contents[0]
      ;stuff you do
    Next
EndIf

Or you may choose to replace all the $contents[0]:

For $x = 1 To Ubound($contents) - 1
  ;stuff you do
Next
that would fix the error, BUT i believe that's a mis-diagnosis, or more accurately a half diagnosis. For whatever reason the FileReadToArray() isn't returning something, maybe the file isn't there or is empty? :lmao:
Link to comment
Share on other sites

  • Moderators

that would fix the error, BUT i believe that's a mis-diagnosis, or more accurately a half diagnosis. For whatever reason the FileReadToArray() isn't returning something, maybe the file isn't there or is empty? :lmao:

It wasn't a 'diagnosis' that would imply that I read his/your/their code ;) I was just answering a 'specific' question. o:)

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

The $contents[0] at that time of run is not in an array

It wasn't a 'diagnosis' that would imply that I read his/your/their code :lmao: I was just answering a 'specific' question. o:)

not a diagnosis? ;) my post was meant to elaborate on yours, not find fault. isn't it a little early for you to be on?
Link to comment
Share on other sites

  • Moderators

not a diagnosis? ;) my post was meant to elaborate on yours, not find fault. isn't it a little early for you to be on?

Not if I hadn't been to bed :lmao:... I need to get off this forum!

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