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 | |
|
Re: shuffle a text file
by toolic (Bishop) on Oct 08, 2014 at 13:59 UTC | |
|
Re: shuffle a text file
by davido (Cardinal) on Oct 08, 2014 at 17:05 UTC | |
|
Re: shuffle a text file
by Loops (Curate) on Oct 08, 2014 at 14:13 UTC | |
|
Re: shuffle a text file
by AnomalousMonk (Archbishop) on Oct 08, 2014 at 16:27 UTC | |
|
Re: shuffle a text file
by ww (Archbishop) on Oct 08, 2014 at 16:24 UTC |