Jump to content

unique lines only


Recommended Posts

i'm trying to make a script that'll sort out only unique lines and for some reason i can't quite get it to work... this is what i have

$fil = "test.txt"
$fil = FileRead($fil)
$fil = StringSplit($fil, @CRLF)
dim $niqq = False
dim $niq[1]
for $i = 1 to $fil[0]
    $tempstr = $fil[$i]
    if $tempstr = "" Then
        ContinueLoop
    EndIf
    for $n = 0 to ubound($niq) - 1
        if $niqq = true Then
            $niqq = False
            ExitLoop
        EndIf
        if $tempstr = $niq[$n] Then
            ContinueLoop
        EndIf
        if $tempstr <> $niq[$n] Then
        redim $niq[ubound($niq)+1]
        $niq[Ubound($niq) - 1] = $tempstr
        $niqq = True
        EndIf
    Next
Next
for $i = 0 to ubound($niq) - 1
    if $niq[$i] <> "" Then
    filewrite("test22.txt", $niq[$i] & @CRLF)
EndIf

Next

the input file is

test
test
test
one
one
two
three

and the output comes out as

test
test
one
one
two
two
three
three

any help'd be much appreciated

~cdkid

AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!
Link to comment
Share on other sites

i'm trying to make a script that'll sort out only unique lines and for some reason i can't quite get it to work... this is what i have

; ...

the input file is

test
test
test
one
one
two
three
This is not the fastest, but illustrates the basic loop (note it goes backwards thru the array deleting duplicates):

#include <array.au3>

$fil = "test.txt"
$fil = FileRead($fil)
$fil = StringSplit($fil, @CRLF)

For $i = $fil[0] To 1 Step - 1
    $tempstr = StringStripWS($fil[$i], 3)
    If $tempstr = "" Then
        _ArrayDelete($fil, $i)
    Else
        If $i = 1 Then ExitLoop
        For $n = $i - 1 To 1 Step - 1
            If $tempstr = $fil[$n] Then
                _ArrayDelete($fil, $i)
                ExitLoop
            EndIf
        Next
    EndIf
Next
$fil[0] = UBound($fil) - 1
_ArrayDisplay($fil, "Debug: $fil output")

If $fil[0] > 0 Then
    _FileWriteFromArray("test22.txt", $fil, 1)
Else
    MsgBox(16, "Error", "No results left.")
EndIf

If it's really all single words, then a scripting dictionary would be much faster. But the above code is nice and simple, if speed is not a big issue.

:)

Edit: Typo.

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

thanks burt & smoke...

didn't know arrayunique existed

both of those worked

~cdkid

AutoIt Console written in C#. Write au3 code right at the console :D_FileWriteToLineWrite to a specific line in a file.My UDF Libraries: MySQL UDF Library version 1.6 MySQL Database UDF's for AutoItI have stopped updating the MySQL thread above, all future updates will be on my SVN. The svn location is:kan2.sytes.net/publicsvn/mysqlnote: This will still be available, but due to my new job, and school hours, am no longer developing this udf.My business: www.hirethebrain.com Hire The Brain HireTheBrain.com Computer Consulting, Design, Assembly and RepairOh no! I've commited Scriptocide!
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...