Jump to content

Converting FileReadLine operations to use arrays. Please help!


 Share

Recommended Posts

My goal: Optimize my script to work faster. My assumption: arrays is a way to go.

Your help is appreciated. Thank you for your time.

Script details:

1. Read lines from allassets.txt file

2. Read lines from Installedassets.txt file

3. Compare if a line from allassets.txt file exists in Installedassets.txt file. If not, write line from allassets.txt to out.txt file

************************************************************************

#include <GUIConstants.au3>

#include <Constants.au3>

#include <Date.au3>

#include <file.au3>

#include <array.au3>

Global $all=@ScriptDir&'\allassets.txt'

Global $installed=@ScriptDir&'\Installedassets.txt'

Global $delta=@ScriptDir&'\out.txt'

FileOpen($all,16)

FileOpen($installed,16)

FileOpen($out,16)

$x = 1

While $x >= 1

$ReadVarall = FileReadLine($all, $x)

If @error = -1 Then

ExitLoop

EndIf

$y=1

While $y>=1

$ReadVarinstalled = FileReadLine($installed, $y)

If @error = -1 Then

FileWriteLine($delta,$ReadVarall)

ExitLoop

EndIf

If StringCompare($ReadVarall,$ReadVarinstalled)=0 Then

exitloop

Endif

$y=$y+1

ContinueLoop

WEnd

$x = $x + 1

WEnd

FileClose($all)

FileClose($installed)

FileClose($delta)

MsgBox(0,'','Completed')

Link to comment
Share on other sites

Thank you for your quick reaponse. I have never used it. Can you help me with arrays?

arrays are discussed in the help file, in Autoit they are basically groups of strings. It's not exactly that way, but for the purposes of what you need, you can think of it that way. Basically, the function will go through and select each line in your file and put them sequentially into an array, each line it's own string.

For example, if your text file (mytext.txt) contained the following:

abcdef

ghijklm

nopqrst

uvwxyz

then _FileReadToArray("mytext.txt", $myarray)

would produce an array with the following values:

$myarray[0] = 4

$myarray[1] = abcdef

$myarray[2] = ghijklm

$myarray[3] = nopqrst

$myarray[4] = uvwxyz

[u]You can download my projects at:[/u] Pulsar Software
Link to comment
Share on other sites

Here they are

Global $hAllAssets = @ScriptDir & '\allassets.txt'
Global $hInstalledAssets = @ScriptDir & '\Installedassets.txt'
Global $hDelta = @ScriptDir & '\out.txt'

FileOpen($hAllAssets, 16)
FileOpen($hInstalledAssets, 16)
FileOpen($hDelta, 16)

$x = 0
While 1
    $sAllVar = FileReadLine($hAllAssets, $x)
    If @error = -1 Then ExitLoop
    
    Msgbox(0, "", $sAllVar)
    
    $i = 0
    While 1
        $sInstalledVar = FileReadLine($hInstalledAssets, $i)
        If @error = -1 Then
            FileWriteLine($hDelta, $sAllVar)
            ExitLoop
        EndIf
        Msgbox(0, "", $sInstalledVar)
        If StringCompare($sAllVar, $sInstalledVar) = 0 Then ExitLoop
        $i += 1
    WEnd
    $x += 1
WEnd

FileClose($hAllAssets)
FileClose($hInstalledAssets)
FileClose($hDelta)oÝ÷ Ù©Ýjëh×6#include <File.au3>
Global $hAllAssets = @ScriptDir & '\allassets.txt'
Global $hInstalledAssets = @ScriptDir & '\Installedassets.txt'
Global $hDelta = @ScriptDir & '\out.txt'

Local $asAllAssets, $asInstalledAssets

If Not _FileReadToArray($hAllAssets, $asAllAssets) Then MsgBox(0, "Error", @error)
If Not _FileReadToArray($hInstalledAssets, $asInstalledAssets) Then MsgBox(0, "Error", @error)
$iUBoundInstalled = UBound($asInstalledAssets) - 1

For $sAllAssets = 1 To UBound($asAllAssets) - 1
    For $sInstalledAssets = 1 To $iUBoundInstalled
        If StringCompare($asAllAssets[$sAllAssets], $asInstalledAssets[$sInstalledAssets]) = 0 Then ExitLoop
        If $sInstalledAssets = $iUBoundInstalled Then _FileWriteLog($hDelta, $asAllAssets[$sAllAssets])
    Next
Next

I needed to Declare both $as_ and I only wanted one call to UBound so I preformed that only once since I'm calling the finished product twice, also do note the "- 1" after the UBound as it is one of the ways that it works. I tried both test and they work the same, except I have _FileWriteLog sending me the time with it, it is just as easy for you to _FileWriteToLine as that will keep the open the file stuff in the background.

Edited by TerarinK

0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E

Link to comment
Share on other sites

Thank you very much for your help!

Here they are

Global $hAllAssets = @ScriptDir & '\allassets.txt'
Global $hInstalledAssets = @ScriptDir & '\Installedassets.txt'
Global $hDelta = @ScriptDir & '\out.txt'

FileOpen($hAllAssets, 16)
FileOpen($hInstalledAssets, 16)
FileOpen($hDelta, 16)

$x = 0
While 1
    $sAllVar = FileReadLine($hAllAssets, $x)
    If @error = -1 Then ExitLoop
    
    Msgbox(0, "", $sAllVar)
    
    $i = 0
    While 1
        $sInstalledVar = FileReadLine($hInstalledAssets, $i)
        If @error = -1 Then
            FileWriteLine($hDelta, $sAllVar)
            ExitLoop
        EndIf
        Msgbox(0, "", $sInstalledVar)
        If StringCompare($sAllVar, $sInstalledVar) = 0 Then ExitLoop
        $i += 1
    WEnd
    $x += 1
WEnd

FileClose($hAllAssets)
FileClose($hInstalledAssets)
FileClose($hDelta)oÝ÷ Ù©Ýjëh×6#include <File.au3>
Global $hAllAssets = @ScriptDir & '\allassets.txt'
Global $hInstalledAssets = @ScriptDir & '\Installedassets.txt'
Global $hDelta = @ScriptDir & '\out.txt'

Local $asAllAssets, $asInstalledAssets

If Not _FileReadToArray($hAllAssets, $asAllAssets) Then MsgBox(0, "Error", @error)
If Not _FileReadToArray($hInstalledAssets, $asInstalledAssets) Then MsgBox(0, "Error", @error)
$iUBoundInstalled = UBound($asInstalledAssets) - 1

For $sAllAssets = 1 To UBound($asAllAssets) - 1
    For $sInstalledAssets = 1 To $iUBoundInstalled
        If StringCompare($asAllAssets[$sAllAssets], $asInstalledAssets[$sInstalledAssets]) = 0 Then ExitLoop
        If $sInstalledAssets = $iUBoundInstalled Then _FileWriteLog($hDelta, $asAllAssets[$sAllAssets])
    Next
Next

I needed to Declare both $as_ and I only wanted one call to UBound so I preformed that only once since I'm calling the finished product twice, also do note the "- 1" after the UBound as it is one of the ways that it works. I tried both test and they work the same, except I have _FileWriteLog sending me the time with it, it is just as easy for you to _FileWriteToLine as that will keep the open the file stuff in the background.

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