Jump to content

Array => using data from textfile?


Recommended Posts

Is there a way to array numbers from a text file and sort it in order largest number to smallest?

What I am trying to achieve, I have a text file with 1000s of numbers and trying to find the top 10 numbers using array?

I can achieve this by programming the numbers within AutoIT but unable to get it working from a textfile?

Maybe there is a quicker/easier way than an array?

Link to comment
Share on other sites

Were your textfile organized as one-number-per-line, I think you could do it with practically two commands; _FileReadToArray and _ArraySort. If your file is delimited it might require a couple more lines of code. What's your text file look like?

Link to comment
Share on other sites

use this:

#include<array.au3>
global $array[200]
for $i=1 to 500
    $var=FileReadLine("c:\file.txt",$i)
    If $var="" Then ExitLoop
    $array[$i]=$var
Next
$file=FileOpen("c:\file1.txt",2)

For $i=1 to 10
    FileWriteLine($file,_ArrayMax($array,1))
    _ArrayDelete($array,_ArrayMaxIndex($array,1))
Next
[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com
Link to comment
Share on other sites

  • Moderators

Why not just read the file to an array, and use _ArraySort() with descending parameter set to 1?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

That's what I was thinking, Smoke_N, but _ArraySort seems to treat them as strings, not numerics (and I'm going on the assumption that not all of his numbers are the same length?).

#include<file.au3>
#include<array.au3>
Dim $Array[1]
_FileReadToArray("numbers.txt", $Array)
_ArraySort($Array, 1, 1)
_ArrayDisplay($Array)

But then he doesn't need a second file if he doesn't have use for it...

#include<file.au3>
#include<array.au3>
Dim $FullArray[1], $MaxArray[10]
_FileReadToArray("numbers.txt", $FullArray)
For $i=0 to 9
    $j = _ArrayMaxIndex($FullArray,1)
    $MaxArray[$i] = $FullArray[$j]
    _ArrayDelete($FullArray, $j)
Next
_ArrayDisplay($MaxArray)

Edit: You might want to go with the coding in the for-next loop here. You're dealing with 1000's of numbers, and this scans your full array 10 times (_ArrayMaxIndex), rather than 20 (_ArrayMax and _ArrayMaxIndex) as in a prior example. Also _FileReadToArray will automatically dimension the FullArray to match your file size, so you don't have to worry about either overflowing the array, or allocating more memory than is required.

Edited by Spiff59
Link to comment
Share on other sites

  • Moderators

That's what I was thinking, Smoke_N, but _ArraySort seems to treat them as strings, not numerics (and I'm going on the assumption that not all of his numbers are the same length?).

#include<file.au3>
#include<array.au3>
Dim $Array[1]
_FileReadToArray("numbers.txt", $Array)
_ArraySort($Array, 1, 1)
_ArrayDisplay($Array)

But then he doesn't need a second file if he doesn't have use for it...

#include<file.au3>
#include<array.au3>
Dim $FullArray[1], $MaxArray[10]
_FileReadToArray("numbers.txt", $FullArray)
For $i=0 to 9
    $j = _ArrayMaxIndex($FullArray,1)
    $MaxArray[$i] = $FullArray[$j]
    _ArrayDelete($FullArray, $j)
Next
_ArrayDisplay($MaxArray)

Edit: You might want to go with the coding in the for-next loop here. You're dealing with 1000's of numbers, and this scans your full array 10 times (_ArrayMaxIndex), rather than 20 (_ArrayMax and _ArrayMaxIndex) as in a prior example. Also _FileReadToArray will automatically dimension the FullArray to match your file size, so you don't have to worry about either overflowing the array, or allocating more memory than is required.

I wrote this a while ago:
Func _ArraySortNum(ByRef $nArray, $iAscending = 0, $iStart = 1)
    For $iCount = $iStart To UBound($nArray) - 2
        Local $iSE = $iCount
        If $iAscending = 0 Then
            For $xCount = $iCount To UBound($nArray) - 1
                If Number($nArray[$iSE]) < Number($nArray[$xCount]) Then $iSE = $xCount
            Next
        Else
            For $xCount = $iCount To UBound($nArray) - 1
                If Number($nArray[$iSE]) > Number($nArray[$xCount]) Then $iSE = $xCount
            Next
        EndIf
        Local $iHLD = $nArray[$iCount]
        $nArray[$iCount] = $nArray[$iSE]
        $nArray[$iSE] = $iHLD
    Next
EndFunc
I think it's in one Valuaters script threads.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Tried the FileArray then Sort and as already mentioned it is not decending it correctly.

It also added the total lines in there not sure why it did that?

_FileReadToArray (as described in the help file) puts the array size into the first element ([0]) of the array it returns, so you just ignore that in processing, and start from [1], but's it's also very useful to have that count handed to you, rather than have to loop and manually count the size of the returned array. The ",1" parameter I stuck at the end of the _ArrayDisplay in that example tells _ArrayDisplay to start at element number 1, so the count doesn't appear in the list.

My first example doesn't work because it's sorting your file treating each line a a text string, rather than a numeric. I think my second example should work well for you.

Edited by Spiff59
Link to comment
Share on other sites

I wrote this a while ago:

Func _ArraySortNum(ByRef $nArray, $iAscending = 0, $iStart = 1)
    For $iCount = $iStart To UBound($nArray) - 2
        Local $iSE = $iCount
        If $iAscending = 0 Then
            For $xCount = $iCount To UBound($nArray) - 1
                If Number($nArray[$iSE]) < Number($nArray[$xCount]) Then $iSE = $xCount
            Next
        Else
            For $xCount = $iCount To UBound($nArray) - 1
                If Number($nArray[$iSE]) > Number($nArray[$xCount]) Then $iSE = $xCount
            Next
        EndIf
        Local $iHLD = $nArray[$iCount]
        $nArray[$iCount] = $nArray[$iSE]
        $nArray[$iSE] = $iHLD
    Next
EndFunc
I think it's in one Valuaters script threads.
I think that would be a very useful addition to array.au3, or possibly merged into _ArraySort while adding a "numeric" parameter.
Link to comment
Share on other sites

spiff59,

Good job modifying my script.. n i just wanted to show an example as to what can be done.. it was a very hastily written code.. so didn't think of optimizing it.. just wanted it to do the task at hand.. but thanks for optimizing it.. n ya abt the 2nd file, u don't need it.. u can re-write the same file.. but i wasn't very sure what DarkBoost's intention was.. so just wrote a new file, so as not to disturb the original..

Smoke_N,

Good way to do this using ur UDF.. as spiff said we need to include it in array.au3

DarkBoost,

I think a lot of good solutions have come ur way.. any 1 can do its job quite well.. go ahead with it..

Cheers,

[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com
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...