Jump to content

How to sort text lines randomly?


frew
 Share

Recommended Posts

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

By randomly I mean some like this

1

2

3

4

5

would become something like this

2

5

4

1

3

read the lines and sort them into a array randomly then place them back

Could 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

#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)

Link to comment
Share on other sites

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

#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 by mdiesel
Link to comment
Share on other sites

  • 5 weeks later...

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

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