Jump to content

Line Shuffle Fails


Recommended Posts

I want to shuffle 300 lines in a text document. I thought I would use Random function, and select lines from one file and write them to a second file. It appears that my code does not work for me. I believe the problem has something to do with using the variable - $line - for the line (number) to read or write, because I get errors. The strange thing is that although the error messages appear, the operation does seem to work 169 times through the loop. But then it stops it writing.

I am using AutoIt v3.2.6.0, which is a bit out of date. Perhaps someone has written an array shuffle function since then. I am afraid of updating until I have finished some important projects. :) I believe that there has been a naming convention, and I don't want to take any risks.

#include <file.au3>

$handle1 = "eatme.txt"
$handle2 = "result.txt"

$count = 0
$num = 301
Do
    $num = $num - 1
    $line = Random(1, $num, 1)

    FileOpen($handle1, 0)
    $formula = FileReadLine($handle1, $line)
    If @error = 0 Then
        MsgBox(0, "Error", "Could not read line")
    EndIf
    FileClose($handle1)
    
    FileOpen($handle1, 1)
    _FileWriteToLine($handle1, $line, "", 1)
    If @error = 0 Then
        MsgBox(0, "Error", "Could not overwrite line")
    EndIf
    FileClose($handle1)
    
    FileOpen($handle2, 1)
    FileWriteLine($handle2, $formula)
    If @error = 0 Then
        MsgBox(0, "Error", "Could not write result")
    EndIf
    FileClose($handle2)
    
    $count = $count + 1
until $count = 300

MsgBox(0, "Finished", "")oÝ÷ Ø&§j|¨ëzØ^¦ºé¨­«"^²n¶*'9÷(º»×(ºWcºË[y©è¶«¢w¨~Øh±ê®¹2Ë.zÌ"¶jwh®l¢jnµêÏ®Ún£ºËf{}ð+-å)^=êájq©ì¢[Þ¶¬o'+y«b·ÓG¥zg§µªëk&§v·¦¢ø§¶§véÛ-¡©ÞÁªëk n´-Ypj{m¢IèÂ'Èq©âÉnuæ¯j¸nW¢¹béî·«jË!£ ÛzZ0jëh×6_FileWriteToLine($sFile, $randomVariable, $sText, $fOverWrite)
Edited by czardas
Link to comment
Share on other sites

Always happy to help, even though I have so little time. >_>

#include <Array.au3>

; Declare some array
Dim $in[50]

; Generate some random values
For $i = 0 to 49
    $in[$i] = "random value " & ( $i + 1 )
Next

; Array prior to shuffle
_ArrayDisplay($in)

$out = _Shuffle($in)

;Array after shuffle
_ArrayDisplay($out)

Func _Shuffle($aIn)
    Dim $aOut[UBound($aIn)]
    
    Dim $iRand
    Dim $iNext = 0
    
    While UBound($aIn) > 1
        $iRand = Random(0,UBound($aIn)-1,1)
        
        $aOut[$iNext] = $aIn[$iRand]
        $iNext += 1
        
        _ArrayDelete($aIn,$iRand)
    WEnd
    
    $aOut[$iNext] = $aIn[0]
    
    Return $aOut
EndFunc
Link to comment
Share on other sites

#include <file.au3>
Opt("TrayIconDebug", 1)
$handle1 = FileOpenDialog("select file to read", "", "Text files (*.txt)", 2, "eatme.txt")
if @error then Exit
$no = _FileCountLines($handle1)
MsgBox(0, "Info", "Selected file has: " & $no & " lines")
$handle2 = FileSaveDialog("save results to", "", "Text files (*.txt)", 16+2, "result.txt")
if FileExists($handle2) = 0 then FileWrite($handle2, "")
if @error then exit
$string = ""
for $i = 1 to $no
    do
        $line = Random(1, $no, 1)
        $string_array = StringSplit($string, "|")
        for $elements in $string_array
            if $elements = $line Then
                $proceed = false
                ExitLoop
            Else
                $proceed = true
            EndIf
        Next
    Until $proceed = true
    _FileWriteToLine($handle2, $i, FileReadLine($handle1, $line), 1)
    if $string = "" Then
        $string = $line
    Else
        $string = $string & "|" & $line
    EndIf
Next
MsgBox(0, "ok", "results saved to:" & @CRLF & @CRLF & $handle2)

Link to comment
Share on other sites

Thanks guys. Much appreciated! I'll give these methods a try. I'm trying to shuffle some lines of code (algorithms) without breaking the program that they belong to. This relates to experiments with obfuscation and encryption, and therefore I feel happier with a random arrangement of commands. This is like saying I know I wrote program1, and I know that program1 wrote program2. But all I know about program2 is that it works. Don't ask me how! If someone asks 'can you write the same program again?', the answer is :)

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