xmen Posted May 13, 2005 Posted May 13, 2005 given 13>7>4>14>3>41, where > is a TAB. Here my code, what i want to ask is, how read the integer faster into array and then sort it? #include <Array.au3> $file = FileOpen("c:\abc", 0) ; Check if file opened for reading OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read in 1 character at a time until the EOF is reached dim $avDigit [6] $i = 0 for $j = 0 to 5 $avDigit [$j] = "" next While 1 $chars = FileRead($file, 1) If @error = -1 Then ExitLoop ; if a char if StringIsDigit($chars) then $avDigit [$i] = $avDigit [$i] & $chars elseif ($chars == chr(9)) then ;tab $i = $i + 1 endif wend ;sort the number for $k = 0 to 4 for $l = 0 to 4 if Int($avDigit[$l]) > Int($avDigit[$l+1]) then _ArraySwap( $avDigit[$l], $avDigit[$l+1] ) endif next next ;open new file to write the sort number $file = FileOpen("c:\abc2", 1) ; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ;write them for $x =0 to 5 FileWrite($file, $avDigit[$x] & chr(9)) Next FileClose($file) exit
buzz44 Posted May 13, 2005 Posted May 13, 2005 Example code and function using the numbers you provided. Shows unsorted, then sorted decending then ascending. expandcollapse popup#include <Array.au3> Dim $avArray[6] $avArray[0] = "13" $avArray[1] = "7" $avArray[2] = "4" $avArray[3] = "14" $avArray[4] = "3" $avArray[5] = "41" _ArrayDisplay($avArray, "Unsorted") $Array = _SortArray($avArray) _ArrayDisplay($Array, "Sort Decending") $Array = _SortArray($avArray, 1) _ArrayDisplay($Array , "Sort Ascending" ) Func _SortArray($Array, $Reverse = 0) Local $Array_Sort, $Temp_Array $Temp_Array = $Array For $I = 0 To UBound($Array) - 1 $Max = _ArrayMax($Array, 1) $Max_Index = _ArrayMaxIndex($Array, 1) If $I = 0 Then $Array_Sort = _ArrayCreate ($Max) Else _ArrayAdd($Array_Sort, $Max) EndIf _ArrayDelete($Array, $Max_Index) Next If ($Reverse == 1) Then _ArrayReverse ($Array_Sort) Return $Array_Sort ElseIf ($Reverse == 0) Then Return $Array_Sort EndIf EndFunc qq
xmen Posted May 13, 2005 Author Posted May 13, 2005 Thanks man. The array read faster. But the numbers are in the file, i wish to read them faster, not a char ana a char.
buzz44 Posted May 14, 2005 Posted May 14, 2005 (edited) expandcollapse popup#include <Array.au3> Dim $avArray[6] $FN = "C:\abc.txt" $File = FileOpen($FN, 0) If $File = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf $File_Num = FileRead($FN, FileGetSize($FN)) $Replace = StringReplace ($File_Num, chr(9), "|") $Split = StringSplit ($Replace, "|") For $I = 1 To $Split[0] $avArray[$I - 1] = $Split[$I] Next _ArrayDisplay($avArray, "Unsorted") $Array = _SortArray($avArray) _ArrayDisplay($Array, "Sort Decending") $Array = _SortArray($avArray, 1) _ArrayDisplay($Array , "Sort Ascending" ) Func _SortArray($Array, $Reverse = 0) Local $Array_Sort For $I = 0 To UBound($Array) - 1 $Max = _ArrayMax($Array, 1) $Max_Index = _ArrayMaxIndex($Array, 1) If $I = 0 Then $Array_Sort = _ArrayCreate ($Max) Else _ArrayAdd($Array_Sort, $Max) EndIf _ArrayDelete($Array, $Max_Index) Next If ($Reverse == 1) Then _ArrayReverse ($Array_Sort) Return $Array_Sort ElseIf ($Reverse == 0) Then Return $Array_Sort EndIf EndFunc Edited May 14, 2005 by Burrup qq
xmen Posted May 15, 2005 Author Posted May 15, 2005 Oh, too cool, really thanx. Now i have learnt how to use those split, replace ..etc in the example. You help me a lot. Again, thanx.
JSThePatriot Posted May 15, 2005 Posted May 15, 2005 Shouldnt this be in the support forum? Just a thought. JS AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)
buzz44 Posted May 15, 2005 Posted May 15, 2005 Yeah, I figured we could let him off being new here and all. And anyways, seems all problems are solved . qq
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now