Jump to content

Sorting Arrays from Readfile / Writefile


JPF
 Share

Recommended Posts

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
Link to comment
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)

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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? :)

Link to comment
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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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?

Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
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 :) :)

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...