http://www.perlmonks.org?node_id=1044804

perlnoobster has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have a file called output.txt it contains thousands of rows of text separated by columns, all i need is for the output to be randomized, is this possible? (without installing a module) Thank you

Replies are listed 'Best First'.
Re: randomize a text file
by toolic (Bishop) on Jul 17, 2013 at 13:28 UTC
Re: randomize a text file
by BrowserUk (Patriarch) on Jul 17, 2013 at 13:46 UTC

    How do you want it randomised?

    Given:

    a b c d e f g h i j k l m n o p q r s t u v w x y

    Do you want (randomise rows; columns stay the same):

    k l m n o u v w x y f g h i j p q r s t a b c d e

    Or (randomise the columns; rosw stay the same):

    c b d e a h g i j f m l n o k r q s t p w v x y u

    Or (randomise both):

    h g i j f w v x y u c b d e a r q s t p m l n o k

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Hi BrowserUk, I want to randomize the rows, i.e. the columns stay the same, can you help me? thank you
        , can you help me?

        Depends. Post code that reads the file into an array, then maybe.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        If you want to randomize rows only, read your file into an array of rows, apply shuffle from List::Util and print shuffled array into new file. That's all, probably just a one-liner, don't even need to chomp.