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

simple cod for shuffling text file:

I have a text file and I want to shuffle it in rows and columns this file contains numbers which separate each other with space. How should i do?

Update

I wrote this code is it correct?
sub shuffle { my $array= shift; my $i; for ($i = @$array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$array[$i,$j] = @$array[$j,$i]; } } open(NEW, "A.txt") || die "Opening A.txt: $!" ; # the original file open(FH, ">B.txt") || die "Opening B.txt: $!" ; # shuffle file save i +n this while (<NEW>) { push(@lines, $_); } @reordered = shuffle(@lines); foreach (@reordered) { print FH $_; } close NEW; close FH;

Replies are listed 'Best First'.
Re: shuffle a text file
by Corion (Patriarch) on Oct 08, 2014 at 14:00 UTC

    This is done in three easy steps:

    1. Convert your items from the file to an array with one item per array entry
    2. Shuffle the array randomly
    3. Convert the array back to a file which has the same number of columns (and rows) as before
Re: shuffle a text file
by toolic (Bishop) on Oct 08, 2014 at 13:59 UTC
    • perlintro
    • List::Util::shuffle
    • Write some code and post back here if you have more specific questions.
Re: shuffle a text file
by davido (Cardinal) on Oct 08, 2014 at 17:05 UTC

    Your shuffle() implementation shuffles the array in-place; in other words, it modifies the array passed in as a parameter. It then returns something that isn't useful; the Boolean result of the final for-loop (keep-going?) test. It never returns a shuffled array because you never tell it to return the shuffled array.

    You probably wanted something like this:

    sub shuffle { my @array = @{shift()}; return unless @array; # Cannot be empty. my $i = @array; while( --$i ) { my $j = int rand($i+1); @array[$i,$j] = @array[$j,$i]; } return @array; }

    This implementation makes a copy and shuffles it. It then returns that shuffled copy of the original array, leaving the original in-tact. If an in-place shuffle is ok, then do that, but don't return anything. Just expect that the parameter will have been modified.

    But then there's really no reason to do that when a perfectly good implementation of the Fisher Yates shuffle already comes with Perl: List::Util has List::Util::shuffle.


    Dave

Re: shuffle a text file
by Loops (Curate) on Oct 08, 2014 at 14:13 UTC

    Something like the following will read each line of the standard input, and randomize the columns. After all the rows are processed, they're printed out in random order.

    use List::Util qw( shuffle ); my @lines; push @lines, join(' ', shuffle split) for <>; print "$_\n" for shuffle @lines;
Re: shuffle a text file
by AnomalousMonk (Archbishop) on Oct 08, 2014 at 16:27 UTC

    What happened when you ran your code? What happens when you run this simplified version:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "sub shuffle { my $array = shift; my $i; for ($i = @$array; --$i; ) { my $j = int rand ($i+1); next if $i == $j; @$array[$i,$j] = @$array[$j,$i]; } } ;; my @lines = qw(foo bar baz quux); my @reordered = shuffle(\@lines); dd \@reordered; " [0]
    Why does the  @reordered array always end up with a single element of 0? (Update: What do you get if you dump  @lines after shuffling, and why?)

Re: shuffle a text file
by ww (Archbishop) on Oct 08, 2014 at 16:24 UTC

    Did you try it?
        If so, what happened (output, error messages, computer spat blue flames, or something else)?
        And if not, why not?




    If you didn't program your executable by toggling in binary, it wasn't really programming!