Jump to content

Need help with my function (few codelines)


Recommended Posts

Func _GetStrline($path, $text)
For $i = 1 To _FileCountLines($path) Step +1
    If FileReadLine ($path, $i) = $text Then
        $line = $i
        ExitLoop
    EndIf
Next
    return $line
EndFunc

I think this should work, but i have small problem with this

eg. line from test.txt

jabadaba

and my scripts wont work if i write

_GetStrLine("test.txt", "jaba")

because he fail if my line dont have spaces

how to set this to something like that?

_GetStrLine("test.txt", "*.*" & "jaba" & "*.*")

This world is crazy

Link to comment
Share on other sites

Something like this? :mellow:

Local $Path = @ScriptDir & "\get_string_number.txt"
Local $String = Random(0, 50, 1)

For $i = 0 To 50
    FileWrite($Path, $i & @CRLF)
Next

$TestFunc = _GetStringLineNumber($String, $Path)

If $TestFunc = 0 Then
    MsgBox(0, "", "No results found.")
Else
    MsgBox(0, "", "String '" & $String & "' was found on linenumber: " & $TestFunc)
EndIf

Func _GetStringLineNumber($sString, $sFile, $iCase = 0)
    Local $iLineNumber = 0

    $hOpen = FileOpen($sFile, 0)
    $sRead = FileRead($hOpen)
    $aSplit = StringSplit($sRead, Chr(10))

    For $i = 1 To $aSplit[0]
        $iLineNumber += 1
        $sReadLine = FileReadLine($hOpen, $i)

        If $iCase = 0 Then
            If $sReadLine = $sString Then
                FileClose($hOpen)
                Return $iLineNumber
            EndIf

        EndIf

        If $iCase = 1 Then
            If $sReadLine == $sString Then
                FileClose($hOpen)
                Return $iLineNumber
            EndIf
        EndIf
    Next

    Return 0
EndFunc

EDIT: Wait, I missed something. :/

Nevermind this. :P

Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

Yes, but this wont show me substring :mellow:

I will edit this :P

Substring?

EDIT:

Just noticed I dont need any $iLineNumber

Local $Path = @ScriptDir & "\get_string_number.txt"
Local $String = Random(0, 50, 1)

For $i = 0 To 50
    FileWrite($Path, $i & @CRLF)
Next

$TestFunc = _GetStringLineNumber($String, $Path)

If $TestFunc = 0 Then
    MsgBox(0, "", "No results found.")
Else
    MsgBox(0, "", "String '" & $String & "' was found on linenumber: " & $TestFunc)
EndIf

Func _GetStringLineNumber($sString, $sFile, $iCase = 0)
    $hOpen = FileOpen($sFile, 0)
    $sRead = FileRead($hOpen)
    $aSplit = StringSplit($sRead, Chr(10))

    For $i = 1 To $aSplit[0]
        $sReadLine = FileReadLine($hOpen, $i)

        If $iCase = 0 Then
            If $sReadLine = $sString Then
                FileClose($hOpen)
                Return $i
            EndIf

        EndIf

        If $iCase = 1 Then
            If $sReadLine == $sString Then
                FileClose($hOpen)
                Return $i
            EndIf
        EndIf
    Next

    Return 0
EndFunc

Still, what do you mean by substring?

Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

If $sReadLine = StringInStr($sReadLine, $sString) Then

Hmmm whats wrong?

Substring is a "234" in eg. "12345" :mellow:

Ah so, hold on. *Writes*

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

If StringInStr($sReadLine, %sString) Then

If you would post a few lines of what the file contains and what you actually want returned from the example, it would help others help you.

EDIT: It is probably just a simple StringRegExp() or StringRegExpReplace() that you need but without seeing an example we can't tell you for sure.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

StringInStr() returns the integer location where the search string was found:

$sString = "jabadaba"

$iVal = StringInStr($sString, "jaba")
ConsoleWrite("$iVal = " & $iVal & @LF)

$iVal = StringInStr($sString, "daba")
ConsoleWrite("$iVal = " & $iVal & @LF)

$iVal = StringInStr($sString, "wxyz")
ConsoleWrite("$iVal = " & $iVal & @LF)

:mellow:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Local $Path = @ScriptDir & "\get_string_number.txt"
Local $String = ","

FileWrite($Path, "t13est" & @CRLF)
FileWrite($Path, "test2" & @CRLF)
FileWrite($Path, "te131st3" & @CRLF)
FileWrite($Path, "test65" & @CRLF)
FileWrite($Path, "tes43thihi" & @CRLF)
FileWrite($Path, "tesst" & @CRLF)
FileWrite($Path, "te,stad" & @CRLF)
FileWrite($Path, "te4st" & @CRLF)
FileWrite($Path, "tes35sdt" & @CRLF)
FileWrite($Path, "tes1231t" & @CRLF)

$TestFunc = _GetStringLineNumber($String, $Path)

If $TestFunc = 0 Then
    MsgBox(0, "", "No results found.")
Else
    MsgBox(0, "", "String '" & $String & "' was found on linenumber: " & $TestFunc)
EndIf

Func _GetStringLineNumber($sString, $sFile, $iCase = 0)
    $hOpen = FileOpen($sFile, 0)
    $sRead = FileRead($hOpen)
    $aSplit = StringSplit($sRead, Chr(10))

    For $i = 1 To $aSplit[0]
        $sReadLine = FileReadLine($hOpen, $i)

        If $iCase = 0 Then
            If $sReadLine = $sString Or StringInStr($sReadLine, $sString) Then
                FileClose($hOpen)
                Return $i
            EndIf

        EndIf

        If $iCase = 1 Then
            If $sReadLine == $sString Or StringInStr($sReadLine, $sString)Then
                FileClose($hOpen)
                Return $i
            EndIf
        EndIf
    Next

    Return 0
EndFunc

Like this?

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

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