Jump to content
Sign in to follow this  
Eigensheep

Search a text file for specific lines

Recommended Posts

Eigensheep

Hi,

I am working with a .txt file that has lots of lines (unsure of how many) and would like to be able to pick out random lines that specify certain criteria.

I have tried generating a random number, reading the line, checking to see if the line has/ doen't have certain words and then adding it to a list if it "passes", but i'm just wondering if there's a faster and more efficient way.

For example if I only want to pick out lines without the text "test" in them, but 15 lines do, what's the best way to do this?

Thanks.

Share this post


Link to post
Share on other sites
Valuater

I use _FileReadToArray()

post your code and we might be able to help you

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
Eigensheep

Thanks, i'll give that (the function) a try.

What I havedone is to bring up a GUI with several checkboxes (values stored in themes.txt - simply a list) and then to read the value of those that are unchecked to an array. I then have another separate txt file (called items.txt) in the same directory that I would like to pick out a specified number of lines from that don't have those values, but not just the first 10, say.

Here is (the important part of) my code so far:

CODE
GUICtrlSetData($title,"Step 2 of 3")

GUICtrlSetData($instructions," Now select the themes you want to use. As before, select all, one or a combination of several. When finished, click next again.")

GUICtrlSetOnEvent($next,"_step3")

$file = FileOpen(@ScriptDir & "/themes.txt",0)

$x = 215

$y = 100

$xmax = 595

$ymax = 400

$xdist = 130

$ydist = 30

Global $themes[1][2] = [["CtrlId","Name"]]

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

$ctrl = GUICtrlCreateCheckBox($line,$x,$y,$xdist,$ydist)

GUICtrlSetState(-1,$GUI_CHECKED)

$x = $x + $xdist + 5

If $x > $xmax Then

$x = 215

$y = $y + $ydist + 5

EndIf

ReDim $themes[uBound($themes)+1][2]

$themes[uBound($themes)-1][0] = $ctrl

$themes[uBound($themes)-1][1] = $line

WEnd

FileClose($file)

Func _step3()

Global $exclude[1] = ["Text"]

For $x = $themes[1][0] To $catgs[uBound($themes)-1][0]

If GUICtrlRead ( $x ) = $GUI_UNCHECKED Then

ReDim $exclude[uBound($exclude)+1]

$exclude[uBound($exclude)-1] = $themes[$x-$themes[1][0]+1][1]

EndIf

Next

EndFunc

Share this post


Link to post
Share on other sites
FreeFry

I dunno.. maybe:

$aFile = StringSplit(FileRead("file.txt"), @CRLF, 1)
For $i = 1 To $aFile[0]
    If StringInStr($aFile[$i], "test") Then
        MsgBox(0, "wie", "File contains 'test'")
    EndIf
Next

Might work, and the simplest way I know of, except using the _FileReadToArray, as stated by Valuater. :mellow:

Edit:

But from what it seems you're doing, perhaps IniWrite, IniRead would work more efficiently?

Edited by FreeFry

Share this post


Link to post
Share on other sites
Valuater

Obviously this is a slightly sophisticated script... not enough info here to work on for me

A few things that look incorrect...

$file = FileOpen(@ScriptDir & "/themes.txt",0) ; should be reversed "\" ?

If GUICtrlRead ( $x ) = $GUI_UNCHECKED Then ; should use BitOr

Func _IsChecked($control)
    Return BitAnd(GUICtrlRead($control),$GUI_CHECKED) = $GUI_CHECKED
EndFunc

You can use random to select a string from the string count and build a string with useable by using StringInString()

... not sure of all those "exclude" statements either

Good Luck

8)

Edited by Valuater

NEWHeader1.png

Share this post


Link to post
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
Sign in to follow this  

×