Jump to content

Reaching the end of an array


Recommended Posts

Hi guys

I have setup a FOR loop that check's through the contents of a text file. The contents of this text file is held in an ARRAY and if the contents of the file is the same as the contents of the ARRAY my script will return true, otherwise it will return false.

What I would like to do is for my little script to return an error message if the contents of the text file exceeds the amount of data that has been allocated to the ARRAY.

I can post my code if you like but if somebody could point me in the right direction I would be very greatful...

Thanks in advance ^_^

Link to comment
Share on other sites

Hi guys

Thanks for your responses, still not sure how to do this. The code I have so far is shown below:

Local $line
Local $find[2] = ["\b1\b", "\b2\b"]

    For $i = 1 To _FileCountLines("C:\test.txt")
    $line = FileReadLine("C:\test.txt", $i)
    If StringRegExp($line, $find[$i-1]) Then 
        ConsoleWrite("Check passed successfully at line " & $i & @CRLF)
    Else
        MsgBox(4096, "ERROR", "An erronous value was found at line " & $i & @CRLF)
    EndIf
Next

I simply do not how to implement Ubound()...

Link to comment
Share on other sites

By implementing you need to write a mechanism to avoid a hard crash of the known error "Array variable...." so you should check in this case if $i is greater than the last subscript of the array, in this case $i = 1 is the last iterate before you should terminate the loop.

Local $line
Local $find[2] = ["\b1\b", "\b2\b"]

For $i = 1 To _FileCountLines("C:\test.txt")
    If $i > UBound($find) Then ExitLoop ; Should be  If $i > UBound($find)-1 but you're using $i-1 anyway...
    $line = FileReadLine("C:\test.txt", $i)
    If StringRegExp($line, $find[$i-1]) Then
        ConsoleWrite("Check passed successfully at line " & $i & @CRLF)
    Else
        MsgBox(4096, "ERROR", "An erronous value was found at line " & $i & @CRLF)
    EndIf
Next
Edited by Authenticity
Link to comment
Share on other sites

Quick and dirty example

;
#include<file.au3>
$File = @ScriptFullPath
Global $MyArray = ""
_FileReadToArray($File, $myArray)
$hResult = (Ubound($MyArray)-1 = _FileCountLinesEx($File))
MsgBox(0,"Results", $hResult)

Func _FileCountLinesEx($sFile)
   If FileExists($sFile) Then $sFile = FileRead($sFile)
   StringRegExpReplace($sFile,"(?m:^)\V*(\v|\z)+?", "$1")
   Return @Extended
EndFunc
;

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

By implementing you need to write a mechanism to avoid a hard crash of the known error "Array variable...." so you should check in this case if $i is greater than the last subscript of the array, in this case $i = 1 is the last iterate before you should terminate the loop.

Local $line
Local $find[2] = ["\b1\b", "\b2\b"]

For $i = 1 To _FileCountLines("C:\test.txt")
    If $i > UBound($find) Then ExitLoop ; Should be  If $i > UBound($find)-1 but you're using $i-1 anyway...
    $line = FileReadLine("C:\test.txt", $i)
    If StringRegExp($line, $find[$i-1]) Then
        ConsoleWrite("Check passed successfully at line " & $i & @CRLF)
    Else
        MsgBox(4096, "ERROR", "An erronous value was found at line " & $i & @CRLF)
    EndIf
Next
Thank you all for you replies. I have moved the line Authenticity entered into my code down a line and this works just fine...
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...