Jump to content

Compare text file with array


 Share

Recommended Posts

Here is the part of the code I'm currently having trouble with

For $Ti = 1 To _FileCountLines($UsernameFilePath)
    $line = FileReadLine($UsernameFilePath, $Ti)
    For $Ui = 0 To UBound($TexansUsernames)
        If $TexansUsernames[$Ui] == $line Then
            ConsoleWrite("1")
        Else
            ConsoleWrite("0")
        EndIf
    Next
Next

 

The goal is to go through all the txt in UsernameFilePath and compare with the array TexansUsername.

I want it to get the first line of UsernameFilePath and check it through every TexansUsername, if there's a match, I want it to print 1, if there's no match, I would like it to print 0

As it is right now, this is what occurs in the console

 

0100000000000000000000000000000000"C:\Users\Daniel\Desktop\TexansPmer.au3" (38) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
If $TexansUsernames[$Ui] == $line Then
If ^ ERROR

Edited by Dgameman1
Link to comment
Share on other sites

I changed my code a bit and I got the error again

Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
If $GoneWildUsernames[$Ui] == $line Then
If ^ ERROR

$UsernameFileOpen = FileOpen($UsernameFilePath, 0)
_ArrayDisplay($TexansUsernames)
$TexansUsernames = _ArrayUnique($TexansUsernames)
_ArrayDisplay($TexansUsernames)
For $Ti = 1 To _FileCountLines($UsernameFilePath)
    $line = FileReadLine($UsernameFilePath, $Ti)
    For $Ui = 0 To UBound($TexansUsernames) - 1
        If $TexansUsernames[$Ui] == $line Then
            _ArrayDelete($TexansUsernames, $Ui)
        Else
            ConsoleWrite($TexansUsernames[$Ui] & @LF)
        EndIf

    Next
    ConsoleWrite(@LF)
Next

Here is a picture of the list of $TexansUsernames

Mo41S0R.png

And let's just say the text inside of $UsertnamesFilePath is

ichru4
jamt9000
flyryan
Ooer
ImNotJesus
verugan
canipaybycheck
noahjk

Edited by Dgameman1
Link to comment
Share on other sites

When using _Arraydelete you need to loop through the array backwards, ubound -1 to 0

Thank you very much

 

And just to make sure my code is working the way I want it.

What it should do is go through and the selected Username is inside the File, then I want it to delete the username from the array.

Does my code do that?

Link to comment
Share on other sites

Dgameman1,

Try this quick and dirty (no error checking...)

#include <array.au3>

Local $aUsernames = FileReadToArray(@ScriptDir & '\usernames.txt')
Local $aTexannames = FileReadToArray(@ScriptDir & '\texannames.txt')

_ArrayDisplay(_RemoveTexanNames($aUsernames, $aTexannames))

Func _RemoveTexanNames(ByRef $a1, ByRef $a2)

    $a1 = _ArrayUnique($a1)
    $a2 = _ArrayUnique($a2)

    for $i = $a1[0] to 1 step -1
        for $j = 1 to $a2[0]
            if $a1[$i] = $a2[$j] then
                _arraydelete($a1,$i)
                $a1[0] -= 1
                ExitLoop
            EndIf
        Next
    next

    return $a1

EndFunc   ;==>_RemoveTexanNames

kylomas

Edited by kylomas
code correction

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Assuming that there is a relationship with your previous topic, the fastest way by far is to use Scripting.Dictionary
This code gets the $TexansUsernames list from the site, then matches names in 'usernames.txt' which are present in the array $TexansUsernames and return them in a new array

#Include <Array.au3>

; read usernames.txt into an array $aUsernames
Local $aUsernames = FileReadToArray(@ScriptDir & '\usernames.txt')

; grab the TexansUsernames into an array $aTexansUsernames
$source = BinaryToString(InetRead("https://www.reddit.com/r/texans", 1))
$aTexansUsernames = StringRegExp($source,'user/(.*?)"', 3)
;_ArrayDisplay($TexansUsernames)

; create the SD objects
Local $sdUsernames = ObjCreate("Scripting.Dictionary")
Local $sdTexansUsernames = ObjCreate("Scripting.Dictionary")
Local $sdResult = ObjCreate("Scripting.Dictionary")

; fill the SD Usernames with the $aUsernames content
For $i In $aUsernames
    $sdUsernames.Item($i) 
Next
; fill the SD TexansUsernames with the $aTexansUsernames content
For $i In $aTexansUsernames
    $sdTexansUsernames.Item($i)
Next
; compare and fill the SD Result
For $i In $aUsernames
    If $sdTexansUsernames.Exists($i) Then $sdResult.Item($i)
Next
; Build the $aResult array
$aResult = $sdResult.Keys() 

_ArrayDisplay($aResult, "$aResult")

 

Edited by mikell
added comments
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

×
×
  • Create New...