Sign in to follow this  
Followers 0
xmen

How to read faster in a file?

9 posts in this topic

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

Share this post


Link to post
Share on other sites



Example code and function using the numbers you provided. Shows unsorted, then sorted decending then ascending.

#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

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

So the txt file looks like...?

13      7       4       14      3       41

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

yes, the format is as u post.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

#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 by Burrup

[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites

Yeah, I figured we could let him off being new here and all. And anyways, seems all problems are solved :(.


[u]Old Projects:[/u]A3MORGB2Hex[u]Old Functions:[/u]_TimeAdd/_TimeSub_AddComma_BubbleSort _RippleSort "He who does not understand your silence will probably not understand your words." - Elbert Hubbard.

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