czardas Posted June 7, 2008 Share Posted June 7, 2008 (edited) 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{}ð+-å)u«^=êá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 June 7, 2008 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
jvanegmond Posted June 7, 2008 Share Posted June 7, 2008 Always happy to help, even though I have so little time. >_> expandcollapse popup#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 github.com/jvanegmond Link to comment Share on other sites More sharing options...
sandin Posted June 7, 2008 Share Posted June 7, 2008 #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) Some cool glass and image menu | WinLIRC remote controler | Happy Holidays to all... | Bounce the sun, a game in which you must save the sun from falling by bouncing it back into the sky | Hook Leadtek WinFast TV Card Remote Control Msges | GDI+ sliding toolbar | MIDI Keyboard (early alpha stage, with lots of bugs to fix) | Alt+Tab replacement | CPU Benchmark with pretty GUI | Ini Editor - Edit/Create your ini files with great ease | Window Manager (take total control of your windows) Pretty GUI! | Pop-Up window from a button | Box slider for toolbar | Display sound volume on desktop | Switch hotkeys with mouse scroll Link to comment Share on other sites More sharing options...
czardas Posted June 7, 2008 Author Share Posted June 7, 2008 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 operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
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