frew Posted March 27, 2009 Share Posted March 27, 2009 Hello, Is there a script that will randomly sort the lines of a text file? Thank you, frew Link to comment Share on other sites More sharing options...
TerarinK Posted March 27, 2009 Share Posted March 27, 2009 What do you mean by randomly? You can always read the lines and sort them into a array randomly then place them back 0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E Link to comment Share on other sites More sharing options...
frew Posted March 27, 2009 Author Share Posted March 27, 2009 By randomly I mean some like this12345would become something like this25413read the lines and sort them into a array randomly then place them backCould you give an example of how to do that?Sort of new to this, and have not worked with arrays yet.Thank you,frew Link to comment Share on other sites More sharing options...
Yashied Posted March 28, 2009 Share Posted March 28, 2009 #Include <Array.au3> dim $Array[9] for $i = 0 to UBound($Array) - 1 $Array[$i] = 'Example Text ' & ($i + 1) next _ArrayDisplay($Array) for $i = 1 to 10 * UBound($Array) $n1 = Random(0, UBound($Array) - 1, 1) $n2 = Random(0, UBound($Array) - 1, 1) $temp = $Array[$n1] $Array[$n1] = $Array[$n2] $Array[$n2] = $temp next _ArrayDisplay($Array) My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More... Link to comment Share on other sites More sharing options...
frew Posted March 28, 2009 Author Share Posted March 28, 2009 Thank you Yashied. I'm not sure what to do with this. I need to study some more about arrays, etc. I appreciate your code very much, but I do not know how to use it yet. I run the code can can see that it randomly sorts the text lines in the box that pops up, but how do I use this to sort a text file? I want to have a script I can run that will sort the lines of a text file randomly, where I can save the randomized list as a new text file. Thank you very much, frew Link to comment Share on other sites More sharing options...
Mat Posted March 28, 2009 Share Posted March 28, 2009 (edited) #Include <Array.au3> #Include <File.au3> $File = FileOpenDialog ("Select file to use", @HomeDrive, "Text files (*.txt) | All Files (*.*)") Dim $Array[_FileCountLines ($File)] _FileReadToArray ($File, $Array) _ArrayDisplay($Array) for $i = 1 to 10 * UBound($Array) $n1 = Random(0, UBound($Array) - 1, 1) $n2 = Random(0, UBound($Array) - 1, 1) $temp = $Array[$n1] $Array[$n1] = $Array[$n2] $Array[$n2] = $temp next _ArrayDisplay($Array) This is exactly the same code, only the array is generated by filrereadtoarray. Edited March 28, 2009 by mdiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
frew Posted April 27, 2009 Author Share Posted April 27, 2009 Thanks so much Yashied and mdiesel for your help with this a while back. I think I see how to use arrays now to get lines of text from a text file, then have them put back into another text file in random order, and without the first element of the array showing up as a sort of nuisance in the final text results. Here's just a couple changes made to the codes you've provided: #Include <Array.au3> #Include <File.au3> dim $Array[9] _FileReadToArray("C:\array_read_from_this_1.txt", $Array) _ArrayDisplay($Array) for $i = 1 to 10 * UBound($Array) $n1 = Random(1, UBound($Array) - 1, 1) ;was Random(0...but I change it to 1 so element [0] will $n2 = Random(1, UBound($Array) - 1, 1) ;not be read into the array (I think that's how it works $temp = $Array[$n1] ;because now text results show no [0] element showing up $Array[$n1] = $Array[$n2] $Array[$n2] = $temp next _ArrayDisplay($Array) _FileWriteFromArray("C:\array_write_to_this_1.txt", $Array, 1);I put 1 here so array does not read ;from the [0]...I want to have the first element [0] not show up in the text file results Oh well, if any other beginners are interested in sorting text randomly, I just wanted to post what seems to be working fine. Thanks again for all your help. frew 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