Jump to content

Read file and show in reverse order


Recommended Posts

Hello,

Is there a function available, once a file is read to an array.

That the result is shown in reverse order (I mean bottum up).

This is the code

; Read in lines of text until the EOF is reached
While 1
    $i = 0
    Do
    $line = FileReadLine($file)
    $i = $i + 1
    Until $i = 6
    If @error = -1 Then ExitLoop
    $string = StringSplit($line, " ", 0)
        
    Dim $avArray[10]
    $avArray[0] = $string[1]
    $avArray[1] = $string[2]
    $avArray[2] = $string[3]
    $avArray[3] = $string[4]
    $avArray[4] = $string[5]
    $avArray[5] = $string[6]
    $avArray[6] = $string[7]
    $avArray[7] = $string[8]
    $avArray[8] = $string[9]
    $avArray[9] = $string[10]
    
;_ArrayReverse($avArray)
    
    $data=$avArray[0]&"|"&$avArray[1]&"|"&$avArray[2]&"|"&$avArray[3]&"|"&$avArray[4]&"|"&$avArray[5]&"|"&$avArray[6]&"|"&$avArray[7]&"|"&$avArray[8]&"|"&$avArray[9]
    $dataCol=GUICtrlCreateListViewItem($data,$listview)
    
        
Wend

I experimented with "Array Sort Decending/Ascending".

But is is not showing what I want.

Once the data is in the 10 dimentions of the array. I would like to sort it by $avArray[0] and that all the other dimensions follow accordingly.

A work around could be that I read the file bottem up, instead of top down.

Is that the solution ?

Link to comment
Share on other sites

I have created a UDF that you are more than welcome to use. Let me know if it is what you want. It reverses the contents of any file you input.

Just copy and paste below into SciTE and run it. The message box at the top is just an example of how to call the function.

MsgBox(0, "Test Reversing", _FileReadReverse("output.txt"))


Func _FileReadReverse($s_File = "")
    Local $file, $s_Chars, $s_bChars
    Local $a_Chars
    Local $i
    
    If $s_File = "" Then
        MsgBox(0, "Error", "Please supply a path when using this function")
        SetError(1)
        Return -1
    EndIf
    
    $file = FileOpen($s_File, 0)
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file: output.txt")
        SetError(1)
        Return -1
    EndIf
    $s_Chars = FileRead($file, FileGetSize($s_File))
    FileClose($file)
    
    $a_Chars = StringSplit($s_Chars, "")
    
    For $i = $a_Chars[0] To 1 Step -1
        $s_bChars = $s_bChars & $a_Chars[$i]
    Next
    
    If $s_bChars = "" Then
        Return -1
    Else
        Return $s_bChars
    EndIf
EndFunc

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)

Link to comment
Share on other sites

#include <Array.au3>
$filename = "C:\test.txt"
$file = FileOpen($filename,0)
$lines = FileRead($file,FileGetSize($filename))
FileClose($file)
$newlines = StringReplace($lines, CHR(10), " ")
$rev = StringSplit($newlines, " ")
_ArrayDisplay($rev,"")
_ArrayReverse($rev)
_ArrayDisplay($rev,"")

or this

#include <Array.au3>
$filename = "C:\test.txt"
$file = FileOpen($filename,0)
$lines = FileRead($file,FileGetSize($filename))
FileClose($file)

$rev = StringSplit($newlines, CHR(13))
_ArrayDisplay($rev,"")
_ArrayReverse($rev)
_ArrayDisplay($rev,"")

might need some "adjustment"

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

#include <Array.au3>
$filename = "C:\test.txt"
$file = FileOpen($filename,0)
$lines = FileRead($file,FileGetSize($filename))
FileClose($file)
$newlines = StringReplace($lines, CHR(10), " ")
$rev = StringSplit($newlines, " ")
_ArrayDisplay($rev,"")
_ArrayReverse($rev)
_ArrayDisplay($rev,"")

or this

#include <Array.au3>
$filename = "C:\test.txt"
$file = FileOpen($filename,0)
$lines = FileRead($file,FileGetSize($filename))
FileClose($file)

$rev = StringSplit($newlines, CHR(13))
_ArrayDisplay($rev,"")
_ArrayReverse($rev)
_ArrayDisplay($rev,"")

might need some "adjustment"

Cheers

Kurt

<{POST_SNAPBACK}>

Nice. I didnt know there was an _ArrayRevers() function as I havent even used any of the _Array*() functions.

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)

Link to comment
Share on other sites

@all

Thanks for the replies and support.

I have tested all of them and non of them run smoothly.

this means when starting it will use 100% of the CPU for a long time and the PC is practically locked.

So I will have find a way that the script is in some way limited in using all the recources.

Link to comment
Share on other sites

I have tested all of them and non of them run smoothly.

this means when starting it will use 100% of the CPU for a long time and the PC is practically locked.

Erm... How large ist the file exactly you are reading?

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

@dev/null/

The file is about 3,5 Mb large.

<{POST_SNAPBACK}>

Hrm... you might try...

MsgBox(0, "Test Reversing", _FileReadReverse("output.txt"))


Func _FileReadReverse($s_File = "")
    Local $file, $s_Chars, $s_bChars
    Local $a_Chars
    Local $i
    
    If $s_File = "" Then
        MsgBox(0, "Error", "Please supply a path when using this function")
        SetError(1)
        Return -1
    EndIf
    
    $file = FileOpen($s_File, 0)
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file: output.txt")
        SetError(1)
        Return -1
    EndIf
    $s_Chars = FileRead($file, FileGetSize($s_File))
    FileClose($file)
    
    $a_Chars = StringSplit($s_Chars, "")
    
    For $i = $a_Chars[0] To 1 Step -1
        $s_bChars = $s_bChars & $a_Chars[$i]
        Sleep(100)
    Next
    
    If $s_bChars = "" Then
        Return -1
    Else
        Return $s_bChars
    EndIf
EndFunc

I added a sleep in the loop that reverses. I think it should reduce CPU usage. I am not sure that it can be optimized any other way. I will keep looking.

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)

Link to comment
Share on other sites

@dev/null/

The file is about 3,5 Mb large.

<{POST_SNAPBACK}>

3,5 MB?? :(:(: You should have mentioned that before. Obviously it does not make ANY sense to do it like this for such a large file. The only way to do it is to read the file line by line "backwards". That might help to speed up the process. Anyway, probably you want to explain why you want to reverse the file, maybe, there is another solution for your problem.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

I have tested all of them and non of them run smoothly.

this means when starting it will use 100% of the CPU for a long time and the PC is practically locked.

I tested my second alternative again, and it was quite fast, even with 4 MB (5 seconds). BUT, you cannot call _ArrayDisplay(), as this will run forever. Without _ArrayDisplay() it take ~5 seconds.

#include <Array.au3>
$filename = "C:\test.txt"
$file = FileOpen($filename,0)
$lines = FileRead($file,FileGetSize($filename))
FileClose($file)

$rev = StringSplit($newlines, CHR(13))
; _ArrayDisplay($rev,"")  ; DON'T do this with large arrays
_ArrayReverse($rev)
;_ArrayDisplay($rev,""); DON'T do this with large arrays

My first alternative takes forever, while executing the command StringReplace for such a large string.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

_ArrayReverse($rev) uses this loop to copy the array:

; Copy the elements in the array till $i_Base
For $iIndex1 = 0 To $i_Base - 1
   $avArray[$iIndex1] = $avNewArray[$iIndex1]
Next

The help file mentions this:

A unique feature is the ability to copy arrays like this:

$mycopy = $myarray

Is there a reason to use a loop?

Just wondering....

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

@all

I tested the _ArrayReverse($rev) but it is not helping me out.

Because it will reorder the data in the Array, and not reorder the sequence of the lines.

See Help:

"Takes the given array and reverses the order in which the elements appear in the array"

What I need is that after the lines are read, the sequence of the lines need to be reversed.

Last line need to come first, before last line needs to come second, ...

PS: The _arrayReverse runs indead fast, the is no CPU overload.

Link to comment
Share on other sites

I really do not know what you are attempting to do here, but hopefully you can find some code that is of use to you from the stuff below.

; for a text file containing these lines:

; this is line one of the file to be read
; this is the second line
; and the third line
; a forth line just for fun

$filename = "";<<<<<<<<<<<<<add filename and/or path
$file = FileOpen($filename,0)
$aArray = StringSplit(FileRead($file,FileGetSize($filename)),@LF)
FileClose($file)
; output of lines of code above
; $aArray [0] = 4
; $aArray [1] = this is line one of the file to be read
; $aArray [2] = this is the second line
; $aArray [3] = and the third line
; $aArray [4] = a forth line just for fun

;it can be displayed (used) in that order
For $i = 1 to (UBound($aArray) - 1)
   MsgBox(0,"$aArray [" & $i & "]",$aArray[$i])
Next

;it can be displayed (used) in reverse order
For $i = (UBound($aArray) - 1) to 1 Step -1
   MsgBox(0,"$aArray [" & $i & "] - shown reversed",$aArray[$i])
Next

; copy array to a new one - just to DIM the new array
$anewArray = $aArray

;it can be reversed within another array
For $i = 1 to (UBound($anewArray) - 1)
   $anewArray[UBound($anewArray) - $i] = $aArray[$i]
Next
; output of lines of code above
; $anewArray [0] = 4
; $aArray [1] = a forth line just for fun
; $aArray [2] = and the third line
; $aArray [3] = this is the second line
; $aArray [4] = this is line one of the file to be read

For $i = 1 to (UBound($anewArray) - 1)
   MsgBox(0,"$anewArray [" & $i & "]",$anewArray[$i])
Next

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

@herewasplato

The Ubound($array) option 2, did it !!

Since there is no SORT option available in the LISTVIEW GUI. I tried to simulate this by clicking a button and then sorting the data accordingly.

Maybe in the future this will integrated in the LISTVIEW control.

Thanks a lot !!

Link to comment
Share on other sites

@Rov

Thanks for your contribution.

I will give it a try later on, because my application needs to run that same with the same file.

Your trick is making first a new file and than use that reversed file to work on.

It might be good in for some other occations.

But it' s a nice tip anyway.

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