Jump to content

Key finding/Match problem


Recommended Posts

Hello...

My Problem is i want my script to check if a $var matchs to a key of an .ini file

but right now it only works if it matchs 100%. Is it possible to make it work if its not 100% matching?

The script looks something like:

Dim $checkforkey = "TjTonnr 1   "
$foundkey = IniRead(@ScriptDir & "\myini.ini", "Main", "" & $checkforkey & "", "NotFound")
if $foundkey == "NotFound" Then
MsgBox(4096, "Minor Error", "The Key was not found.", 10)
Else
MsgBox(4096, "Found", "The Key's value is " & $foundkey & ".", 10)
EndIf

it returns an error/NotFound because the var does not match 100% to the key in the file

the file looks something like:

"TjTonnr" = "Torch"

So how can i make it that it would find this key without changing things in the .ini

Please dont just type $checkforkey = "TjTonnr"

because i got the var readed out from a window so that wont work that easy

my code up there with that var was just an example...

EDIT:

HMM i mean looking for that key even if it doesn't match 100% to the var and returning the keys value

Edited by Verox743

Started with AutoIt on 12th June, 2008.

Link to comment
Share on other sites

try the "Levenshtein" then to find similar strings

The Levenshtein distance is defined as the minimal number of characters you have to replace,

; insert or delete to transform sString1 into sString2.

;

; The greater the Levenshtein-Distance, the more different the strings are.

; If you can't spell or pronounce Levenshtein, the metric is also sometimes called 'edit distance'.

; The Levenshtein distance algorithm has been used in:

; - Spell checking, - Speech recognition, - DNA analysis, - Plagiarism detection .

Edited by nobbe
Link to comment
Share on other sites

try the "Levenshtein" then to find similar strings

So, I have to use "Levenshtein" function to compare the strings and then work with the return of that function but I don't really got what what is returned? Just how much characters of string1 are the same as in string2 ???

an example of something would help me out a lot... I'm sorry but I can'T figure out of any :S

Started with AutoIt on 12th June, 2008.

Link to comment
Share on other sites

Have you tried using StinginStr in combination with iniReadSection?

Dim $checkforkey = "TjTonnr 1   "
$keyArray = IniReadSection(@ScriptDir & "\myini.ini", "Main")
For $i = 1 To $keyArray[0][0]
    $foundkey = StringInStr($checkforkey, $keyArray[$i][0], 0)
    if $foundkey > 0 Then       
        ExitLoop    
    EndIf   
Next

Switch $foundkey
    Case 0
        MsgBox(4096, "Minor Error", "The Key was not found.", 10)
    Case Else
        MsgBox(4096, "Found", "The Key's value is " & $keyArray[$i][1] & ".", 10)
EndSwitch
Link to comment
Share on other sites

ignore this

Ok, my last post was crap...

i dont know why but the key is never found...

BUMP

Dim $testthis = "TjTonnr"
    Dim $ItemList = IniReadSection(@ScriptDir & "\Temp\ITEMDEFINE.ini", "ITEM DEFINITIONS")
    For $i = 1 To $ItemList[0][0]
    $founditem = StringInStr($testthis, $ItemList[$i][0], 0)
    if $founditem > 0 Then        
        ExitLoop    
    EndIf    
Next

Switch $founditem
    Case 0
        MsgBox(4096, "Minor Error", "The Key was not found.", 10)
        $noitemcount += 1
        $bugreportline += 1
        _FileWriteToLine(@ScriptDir & "\Temp\BugReport.txt", $bugreportline, @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " ~ ITEM FAILURE: The output was '" & $loggeditem & "' and it wasn't found.", 1)
        IniWrite(@ScriptDir & "\Temp\TB.STATS.ini", "T4CBOT STATISTICS", "BugReportLine", "" & $bugreportline & "")
    Case Else
        $wdwi = IniRead(@ScriptDir & "\Config\TB.ITEMIDS.ini", "Main item section", "" & $ItemList[$i][0] & "", "NotFound")
EndSwitch

and my ITEMDEFINE.ini

[ITEM DEFINITIONS]
"[Cloth pants 1" = "Cloth pants"
"joId pieces" = "Gold pieces"
"TjTonnr" = "Iron ring"

Case 0 Always occurs with the msg box that the key wasn't found...

Edited by Verox743

Started with AutoIt on 12th June, 2008.

Link to comment
Share on other sites

Ok, my last post was crap...

i dont know why but the key is never found...

BUMP

Dim $testthis = "TjTonnr"
    Dim $ItemList = IniReadSection(@ScriptDir & "\Temp\ITEMDEFINE.ini", "ITEM DEFINITIONS")
    For $i = 1 To $ItemList[0][0]
    $founditem = StringInStr($testthis, $ItemList[$i][0], 0)
    if $founditem > 0 Then        
        ExitLoop    
    EndIf    
Next

Switch $founditem
    Case 0
        MsgBox(4096, "Minor Error", "The Key was not found.", 10)
        $noitemcount += 1
        $bugreportline += 1
        _FileWriteToLine(@ScriptDir & "\Temp\BugReport.txt", $bugreportline, @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " ~ ITEM FAILURE: The output was '" & $loggeditem & "' and it wasn't found.", 1)
        IniWrite(@ScriptDir & "\Temp\TB.STATS.ini", "T4CBOT STATISTICS", "BugReportLine", "" & $bugreportline & "")
    Case Else
        $wdwi = IniRead(@ScriptDir & "\Config\TB.ITEMIDS.ini", "Main item section", "" & $ItemList[$i][0] & "", "NotFound")
EndSwitch

and my ITEMDEFINE.ini

[ITEM DEFINITIONS]
"[Cloth pants 1" = "Cloth pants"
"joId pieces" = "Gold pieces"
"TjTonnr" = "Iron ring"

Case 0 Always occurs with the msg box that the key wasn't found...

This is because I wrote my code going by the fact that what you were searching with was going to have weird spaces and numbers etc. You need to either switch the variables in the StringInStr, or you could use a regular expression to match them. Here is the code for both options just pick one of those lines.

Dim $testthis = "TjTonnr"
    Dim $ItemList = IniReadSection(@ScriptDir & "\ITEMDEFINE.ini", "ITEM DEFINITIONS")
    For $i = 1 To $ItemList[0][0]
        
    ;pick one of the next two lines
        $founditem = StringInStr($ItemList[$i][0], $testthis, 0)
        $founditem = StringRegExp($ItemList[$i][0], ".*" & $testthis & ".*", 0)
        if $founditem > 0 Then      
            ExitLoop    
        EndIf   
    Next

Switch $founditem
    Case 0
        MsgBox(4096, "Minor Error", "The Key was not found.", 10)
        $noitemcount += 1
        $bugreportline += 1
        _FileWriteToLine(@ScriptDir & "\Temp\BugReport.txt", $bugreportline, @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " ~ ITEM FAILURE: The output was '" & $loggeditem & "' and it wasn't found.", 1)
        IniWrite(@ScriptDir & "\TB.STATS.ini", "T4CBOT STATISTICS", "BugReportLine", "" & $bugreportline & "")
    Case Else
        $wdwi = IniRead(@ScriptDir & "\TB.ITEMIDS.ini", "Main item section", "" & $ItemList[$i][0] & "", "NotFound")
EndSwitch
Link to comment
Share on other sites

well i tried them both and its not really working still tells me that the key wasn't found

i think i need to change something at stringregexp to make it work but actually i dont really understand that function... im going to post my full script now so you might see whats wrong... and by the way the script is working damn slow without the start sleep time it needs like 6 seconds just til the part where it says key not found... that is way too slow since thats just for 1 slot in the inventory... i cant run it 1 hour to check the full inventory

#include <MyNew Functions.au3>
#include <File.au3>

Sleep(10000)

Dim $noitemscounted = IniRead(@ScriptDir & "\Config\TB.CONFIG.ini", "Main Configurations", "StopInvCheck", "15")
Dim $logitem
Dim $founditem
Dim $noitemcount = 0
Dim $bugreportline = IniRead(@ScriptDir & "\Temp\TB.STATS.ini", "T4CBOT STATISTICS", "BugReportLine", "0")
Dim $wdwi

;Do
;Until $noitemcount == $noitemscounted
    Send("^i")
    Sleep(500)
    MouseMove(275,400)
    $logitem=OCR_Region(440,500,700,530)
    Dim $loggeditem = _ArrayToString($logitem, " ", 0, 4)
    Dim $ItemList = IniReadSection(@ScriptDir & "\Temp\ITEMDEFINE.ini", "ITEM DEFINITIONS")
    For $i = 1 To $ItemList[0][0]
;    $founditem = StringInStr($loggeditem, $ItemList[$i][0], 0)
;    $founditem = StringInStr($ItemList[$i][0], $loggeditem, 0)
    $founditem = StringRegExp($ItemList[$i][0], ".*" & $loggeditem & ".*", 0)
    if $founditem > 0 Then        
        ExitLoop    
    EndIf    
Next

Switch $founditem
    Case 0
        MsgBox(4096, "Minor Error", "The Key was not found.", 10)
        $noitemcount += 1
        $bugreportline += 1
        _FileWriteToLine(@ScriptDir & "\Temp\BugReport.txt", $bugreportline, @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " ~ ITEM FAILURE: The output was '" & $loggeditem & "' and it wasn't found.", 1)
        IniWrite(@ScriptDir & "\Temp\TB.STATS.ini", "T4CBOT STATISTICS", "BugReportLine", "" & $bugreportline & "")
    Case Else
        $wdwi = IniRead(@ScriptDir & "\Config\TB.ITEMIDS.ini", "Main item section", "" & $ItemList[$i][0] & "", "NotFound")
EndSwitch
    If $wdwi == "Junk" Then
        MouseMove(275,400)
        MouseClick("left")
        Sleep(200)
        MouseMove(750,460)
        Sleep(150)
        MouseClick("left")
    EndIf

The BugReport's output:

06/22 18:46:02 ~ ITEM FAILURE: The output was 'TjTonnr 1   ' and it wasn't found.

The ITEMDEFINE.ini

[ITEM DEFINITIONS]
"[Cloth pants 1" = "Cloth pants"
"joId pieces" = "Gold pieces"
"TjTonnr" = "Iron ring"

The ITEMIDS.ini

[Main item section]
;####################################################################
;###############################JEWELRY###############################
;####################################################################

"Iron ring" = "Junk"

Edit:

about the speed would be nice if someone had an idea how i could make it faster

Edited by Verox743

Started with AutoIt on 12th June, 2008.

Link to comment
Share on other sites

well i tried them both and its not really working still tells me that the key wasn't found

i think i need to change something at stringregexp to make it work but actually i dont really understand that function... im going to post my full script now so you might see whats wrong... and by the way the script is working damn slow without the start sleep time it needs like 6 seconds just til the part where it says key not found... that is way too slow since thats just for 1 slot in the inventory... i cant run it 1 hour to check the full inventory

CODE SNIPPED

The BugReport's output:

06/22 18:46:02 ~ ITEM FAILURE: The output was 'TjTonnr 1   ' and it wasn't found.

The ITEMDEFINE.ini

[ITEM DEFINITIONS]
"[Cloth pants 1" = "Cloth pants"
"joId pieces" = "Gold pieces"
"TjTonnr" = "Iron ring"

The ITEMIDS.ini

[Main item section]
;####################################################################
;###############################JEWELRY###############################
;####################################################################

"Iron ring" = "Junk"

Edit:

about the speed would be nice if someone had an idea how i could make it faster

The code where you're outputting to the file you're not actually adding quotes unless you use a single quote surrounding it like the following:
IniWrite(@ScriptDir & "\Temp\TB.STATS.ini", "T4CBOT STATISTICS", "BugReportLine", '"' & $bugreportline & '"')
But it doesn't matter since there are already quotes areound it, that code would just add another set of quotes.

You don't need the messagebox when a key isn't found, since you already do a file write if it's not found. Also the problem with your code was that it wasn't searching for the right value. ItemList[$i][0] is the key name, ItemList[$i][1] is the value associated with that key so you would basically be looking for "TjTonnr" in the other ini file rather than "Iron ring".

If you want to make sure that the key you're searching for matches one way or the other, the thing you can do is do a check for both ways I showed you with the StringInStr.

One other thing I was curious about was why you kept using == instead of = since a = can be used for comparison. And the Do Until while it was commented out, I wasn't sure if you planned on using it or not, but it doesn't do anything unless you have code between the Do and Until. So I left it commented but moved the until down to where I think it should be, but I could be wrong on this one. If you want me to take a look at OCR_Region I could do that to make sure that it isn't part of the speed problem. Just PM me the code and I can take a look at it for you.

Here is some code, see how this works for you.

#include <MyNew Functions.au3>
#include <File.au3>

Local $noitemscounted = IniRead(@ScriptDir & "\Config\TB.CONFIG.ini", "Main Configurations", "StopInvCheck", "15")
Local $logitem[4]
Local $founditem
Local $noitemcount = 0
Local $bugreportline = IniRead(@ScriptDir & "\Temp\TB.STATS.ini", "T4CBOT STATISTICS", "BugReportLine", "0")
Local $wdwi
Local $loggeditem
Local $ItemList = IniReadSection(@ScriptDir & "\Temp\ITEMDEFINE.ini", "ITEM DEFINITIONS")


;Do
    Send("^i")
    Sleep(500)
    MouseMove(275,400)
    $logitem = OCR_Region(440,500,700,530)
    $loggeditem = _ArrayToString($logitem, " ", 0, 4)   
    For $i = 1 To $ItemList[0][0]
        $founditem = StringInStr($ItemList[$i][0], $loggeditem, 0)  
        $founditem2 = StringInStr($loggeditem, $ItemList[$i][0], 0)
        If ($founditem > 0) Then  
            _KeyFound($ItemList[$i][1])
            $noitemcount += 1
        ElseIf ($founditem2 > 0) Then
            _KeyFound($ItemList[$i][1])
            $noitemcount += 1
        Else
            $noitemcount += 1
            $bugreportline += 1
            _FileWriteToLine(@ScriptDir & "\Temp\BugReport.txt", $bugreportline, @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN & ":" & @SEC & " ~ ITEM FAILURE: The output was '" & $loggeditem & "' and it wasn't found.", 1)
            IniWrite(@ScriptDir & "\Temp\TB.STATS.ini", "T4CBOT STATISTICS", "BugReportLine", "" & $bugreportline & "")
        EndIf   
    Next
;Until $noitemcount = $noitemscounted


Func _KeyFound($item)
    $wdwi = IniRead(@ScriptDir & "\Config\TB.ITEMIDS.ini", "Main item section",  $item, "NotFound") 
    If $wdwi = '"Junk"' Then
        MouseMove(275,400)
        MouseClick("left")
        Sleep(200)
        MouseMove(750,460)
        Sleep(150)
        MouseClick("left")
    EndIf
EndFunc
Link to comment
Share on other sites

Hello,

I tried the Levenshtein distance algorithm, taken from wikipedia.

Seems to work as expected.

$s1='test'
$s2='jesT'
$r=LevenshteinDistance($s1,$s2)
ConsoleWrite($s1&'<->'&$s2&'='&$r&@LF)

$s1='test'
$s2='crest'
$r=LevenshteinDistance($s1,$s2)
ConsoleWrite($s1&'<->'&$s2&'='&$r&@LF)

$s1='test'
$s2='manifest'
$r=LevenshteinDistance($s1,$s2)
ConsoleWrite($s1&'<->'&$s2&'='&$r&@LF)

$s1='test'
$s2='***e'
$r=LevenshteinDistance($s1,$s2)
ConsoleWrite($s1&'<->'&$s2&'='&$r&@LF)

Func LevenshteinDistance($s1,$s2)
    Local $d[StringLen($s1)+1][StringLen($s2)+1]
    Local $cost
    For $i=0 To StringLen($s1)
        $d[$i][0] = $i
    Next
    For $i=0 To StringLen($s2)
        $d[0][$i] = $i
    Next
    For $i=1 To StringLen($s1)
        For $j=1 To StringLen($s2)
            If StringMid($s1,$i,1) == StringMid($s2,$j,1) Then
                $cost = 0
            Else
                $cost = 1
            EndIf
            $d[$i][$j] = minimum($d[$i-1][$j] + 1, _    ;deletion
                                $d[$i][$j-1] + 1, _     ;insertion
                                $d[$i-1][$j-1] + $cost) ;substitution
        Next
    Next
    Return $d[StringLen($s1)][StringLen($s2)]
EndFunc

Func minimum($i1,$i2,$i3)
    Local $min
    If $i1 <= $i2 Then
        $min = $i1
    Else
        $min = $i2
    EndIf
    If $min > $i3 Then $min = $i3
    Return $min
EndFunc

It returns the number of changes to get from string1 to string2.

Maybe this can help you.

ciao

Xandl

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