Sign in to follow this  
Followers 0
JPF

Sorting Arrays from Readfile / Writefile

9 posts in this topic

#1 ·  Posted (edited)

Hi everybody,

As you can probably tell i'm quite new to Autoit and seek your expertise and guidance!

I have been given a challenge to read a set of numbers from a text file, have them sorted in descending order and write them to a seperate text file, or a message box

The problem i'm having seems to be with the _Arraysort function, maybe?

This is what I have tried so far.. dont laugh :)

Dim $temp[5] =[FileRead("numero.txt")]

_ArraySort($temp, 1)

FileWrite("dupe.txt", $temp)

Can anybody shed any light on this? It doesn't seem like enough but the help file hasn't been too kind to me either

Much appreciated! :)

Edited by JPF

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

#Include <File.au3>
#include <array.au3>
Global $aArray
_FileReadToArray("numero.txt", $aArray) 
_ArraySort($aArray, 1, 1)
_FileWriteFromArray("dupe.txt", $aArray, 1)

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#Include <File.au3>
#include <array.au3>
Global $aArray
_FileReadToArray("numero.txt", $aArray) 
_ArraySort($aArray, 1, 1)
_FileWriteFromArray("dupe.txt", $aArray, 1)

Hi Water, thanks for the code!

The only thing that happens there is the numbers that come back don't get sorted :)

In the 'Numero' text file, I have simply typed = 1 2 8 4 9

What comes back into the 'Dupe' file is = 1 2 8 4 9

Any suggestions? :)

Share this post


Link to post
Share on other sites

Try this:

#include <Array.au3>
$dest = ""
$hFile = FileOpen("numero.txt")
While True
    $line = FileReadLine($hFile)
    If @error = -1 Then ExitLoop
    $aNumbers = StringSplit($line, " ", 2)
    _ArraySort($aNumbers)
    $dest &= _ArrayToString($aNumbers, " ") & @CRLF
WEnd
FileClose($hFile)
$hFile = FileOpen("Dupe.txt", 2)
FileWrite($hFile, $dest)
FileClose($hFile)

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

What do you need to sort? The lines (rows) in the file or the numbers in each line?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

What do you need to sort? The lines (rows) in the file or the numbers in each line?

I need to sort the numbers in descending order :)

The previous poster has sorted it, it does work now! (Many thanks!!) I just dont really understand the code very well... :)

Water, if i add stringsplit to your code will that also work?

Share this post


Link to post
Share on other sites

Water, if i add stringsplit to your code will that also work?

No, my code sorts lines, UEZ's code sorts the "words" in a line.

In Excel: UEZ's code sorts the columns whereas my code sorts the lines.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

The question is how are the numbers saved in the text file?

If the numbers are in each line then you have to read each line and sort it! If the numbers are in each row then you can use Water's code.

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

The question is how are the numbers saved in the text file?

If the numbers are in each line then you have to read each line and sort it! If the numbers are in each row then you can use Water's code.

Br,

UEZ

Guys, totally understand it now! thats perfect

Thanks again! Understanding this has been abit of a mare :) :)

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  
Followers 0