Jump to content
Sign in to follow this  
bobele

Check correct sequence of numbers in files

Recommended Posts

bobele

Hi

I have a file, which contains a lot of numbers line by line. Most of them are in correct order.

For example, the file contains the following data:

12

13

14

16

18

18

19

20

21

22

23

I need to check if the files contains duplicates and if the order of the numbers is correct. I only need to know if the order is correct, and if there are existing duplicates. It doesn't matter where. Until now, I've written the attached script. I'm a bit clueless how to continue.

Thank you

FindDuplicates_CheckOrder.au3

Share this post


Link to post
Share on other sites
Manjish

As I see u sorted the array in descending or ascending order (whichever u want).. Now simply write it to the file by filewriteline() and a for loop..

To check duplicate entries.. Take each element of this array and check if the consecutive element is same.. like this:

;$array is ur sorted array
for $i=1 to $array[0]
if $array[$i]=$array[$i+1] then msgbox(4096,"Message","Duplicate No." & $array[$i])

[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com

Share this post


Link to post
Share on other sites
rudi

Hi.

[howto check, if a file has numbers unique and in correct order]

Check out _filereadtoarray(),_arrayunique(), _arraysort().

If the numbers are all unique, ubound() of the original and the resulting array (_arrayunique) will be the same.

For the order just step through all array elements and error out, if the next value isn't greater than the last one.

_arraysort() will help to get the correct order in case you wanna save it out to a file.

Regards, Rudi.

Edited by rudi

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites
Manjish

Here's what I mean:

#include <Array.au3>

Dim $File,$ARRAY[1]
Dim $FileReadLine
$file = FileOpen("targetnums.txt", 0)

If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    _ArrayAdd($ARRAY,$line)
Wend

FileClose($file)

_ArraySort($ARRAY)
;_ArrayDisplay($ARRAY)
$file1=FileOpen("targetnums.txt",2)
For $i=1 to UBound($array)-1
    FileWriteLine($file1,$array[$i])    
    If ($i+1)>(UBound($array)-1) Then
        Exit
    Else
        If $array[$i]=$array[$i+1] Then MsgBox (4096,"message","duplicate No."& $array[$i])
    EndIf
Next

[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com

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  

×