Sign in to follow this  
Followers 0
Dgameman1

Compare text file with array

12 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



For $Ui = 0 To UBound($TexansUsernames)

should be:

For $Ui = 0 To UBound($TexansUsernames) - 1

 

1 person likes this

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

For $Ui = 0 To UBound($TexansUsernames)

should be:

For $Ui = 0 To UBound($TexansUsernames) - 1

 

Thank you so much.

Much appreciated

Edited by Dgameman1

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

But when I use arraydisplay, nothing in has been deleted in the username array

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

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

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  
Followers 0

  • Similar Content

    • rootx
      By rootx
      I write this script to compare to array, well Do you have a better way to do it?
      #include <Constants.au3> #include <Array.au3> #include <File.au3> #include <MsgBoxConstants.au3> $a = FileSelectFolder("seleziona cartella A",@ScriptDir) $fold_A = _FileListToArrayRec($a,"*",2,1,0,1) $b = FileSelectFolder("seleziona cartella B",@ScriptDir) $fold_B = _FileListToArrayRec($b,"*",2,1,0,1) $tota = UBound($fold_A)-1 $totb = UBound($fold_B)-1 If $tota > $totb Then findx($fold_A,$fold_B) Else findx($fold_B,$fold_A) EndIf Func findx($min,$max) For $i = 0 To UBound($min)-1 $found = _ArraySearch($max, $min[$i]) If $found = -1 Then ConsoleWrite($min[$i]&@CRLF) EndIf Next EndFunc  
    • Jibberish
      By Jibberish
      I am trying to add data to an array and I keep getting the error "Subscript used on non-accessible variable ".
      #include <array.au3> ; Includes, etc ~ ~ ~ Local $aOptionsArray[3] $aOptionsArray = CheckboxesAndRadioButtons() _ArrayDisplay($aOptionsArray) Func CheckboxesAndRadioButtons() ; Create Checkboxes and Radio Buttons and read the results ~ ~ ~ ; Radio Buttons to Array ; $aOptions[0] = $bTestSelectForever ; $aOptions[1] = $bTestSelect3Times ; $aOptions[2] = $bTestSelectOnce If $bSelect1 = 1 Then $aOptions[0] = "True" Else $aOptions[0] = "False" <<< This is where the error occurs. EndIf If $bSelect2 = 1 Then $aOptions[1] = "True" Else $aOptions[1] = "False" EndIf If $bSelect3 = 1 Then $aOptions[2] = "True" Else $aOptions[2] = "False" EndIf Return $aOptions EndFunc Is putting data into an array while in a If - Then - Else illegal?
    • Nareshm
      By Nareshm
      How to repeat my script when my control class does not match ?
       
      My Script : winActivate ( " PS :: Version - 2.2.0.0  - [PS Bill]")                   ControlClick ( "PS :: Version - 2.2.0.0  - [PS Bill]", "", "[NAME:btnSavebyFP]")                   ControlSend(" PS :: Version - 2.2.0.0  - [PS Bill]", "", "[NAME:btnSavebyFP]", "{enter}")                   WinActive ("Verification Number Entry ::")                   ControlClick("Next >", "","[NAME:btnNext]") ;if   >>>> Control <<<<  does not match repeat my script, ;if match exit script  

      Class:    WindowsForm734a
      Instance:    15
      ClassnameNN:    WindowsForm34a15
      Name:    BillPrint
      Advanced (Class):    [NAME:BillPrint]
      ID:    7875
      Text:    
      Position:    39, 310
      Size:    1221, 306
      ControlClick Coords:    745, 20
      Style:    0x56010000
      ExStyle:    0x00000000
      Handle:    0x00000000000C0468
    • anoig
      By anoig
      Hi all, 

      First, I want to give a huge shout-out to the community. I'm completely self-taught, and have never had to actually ask a question before because the forum is that good at answering questions and explaining things. However, I'm kind of stumped here, and I've been stuck on this problem for almost a full day.

      I'm working on a script to populate drafts of deeds at work. I have the main GUI and  a function (ctrl($n) and read()) for adding fields to find and data to replace it with to an array for later use with _word_docfindreplace. All of that works. However, due to the way I have the forms set up, I need to create additional fields and info based on the data that's there. Specifically, if there's only one buyer, I need to add the field [Buyer1&2] and the data in $aArray_Base for [Buyer 1]. I also need to add a field [Buyer 2] and have a blank data set in the next column over in the array, and I need to do the same for the Seller. To this end, the function parties() sets boolean variables $2buyers and $2sellers accordingly. Then, I have buyers() and sellers() to populate the data. 

      The problem that I'm running into is that each function works... when ONLY the buyer 1 name field is filled, and when ONLY the seller 1 field is filled.

      So if I fill Buyer 1 Name and save it, the data is populated correctly. But when I fill Buyer 1 and Seller 1 name, only the buyer 1 data populates correctly. Worse, when I fill several fields, neither populate correctly. I have no idea why this happens. I've added messageboxes to debug throughout the entire process and can't pinpoint what's causing the issue. The entire script is below. The function(s) in question are buyers() and sellers(). Only Sellers() has messageboxes throughout.

      Can someone help walk me through what might be causing this and help me find a solution? Thanks a ton in advance, and sorry for the wall of text.
      -Anoig
       
    • SkysLastChance
      By SkysLastChance
      I am not sure why I am getting the this error on my second pass of the code.
      1 - $oWorkbook is not an object or not a workbook object
      Any help or advice on my code appreciated. 
      #include <Excel.au3> #include <Array.au3> #include <MsgBoxConstants.au3> Global $sExcelFile1 = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "(*.xlsm)") Global $sExcelFile2 = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "Excel Sheet (*.xlsx;*.xls)|All (*.*)") Global $vRow = 2 If FileExists($sExcelFile2) Then Global $oExcel2 = _Excel_Open () $oExcel2 = _Excel_BookOpen($oExcel2,$sExcelFile2) EndIF If FileExists($sExcelFile1) Then Global $oExcel1 = _Excel_Open () $oExcel1 = _Excel_BookOpen($oExcel1,$sExcelFile1,Default,Default,"2007") EndIF $oRead = _Excel_RangeRead ($oExcel2,"Untitled","A2",3) $oFind = _Excel_RangeFind ($oExcel1,$oRead,"E4:FD92",Default,$xlWhole) $Clip = _ArrayToClip($oFind,"",0,0,"",2,2) Send("{ScrollLock Off}") $hWnd = WinWait("[CLASS:XLMAIN]") ControlSend($hWnd, "", "", ("^g")) WinWait("[CLASS:bosa_sdm_XL9]") ; Go To ControlSend($hWnd, "", "", ("^v")) ControlSend($hWnd, "", "", ("{Enter}")) ControlSend($hWnD, "", "", "{Down " & $vRow & "}") Do $oTime = _Excel_RangeRead ($oExcel2,"Untitled","B2",3) If @error Then Exit MsgBox(0, "Error", "Error" & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox(0,"Test",$oTime) IF $oTime = "7:10:00 AM" Then $oCalls1 = _Excel_RangeRead ($oExcel2,Default,"C" & $vRow,3) $oCalls2 = _Excel_RangeRead ($oExcel2,Default,"D" & $vRow,3) ControlSend($hWnd, "", "", $oCalls1) ControlSend($hWnd, "", "", ("{RIGHT}")) ControlSend($hWnd, "", "", $oCalls2) $vRow = $vRow + 1 ContinueLoop Else $vRow = $vRow + 1 EndIf Until $vRow = 4 1.xlsm
      2.xlsx