I have a text file and i want shuffle the lines using the Perl Script. I kept the whole file into array and used shuffle in util and i want to write that shuffled data into anther file and renamed it as the old file name.
I wrote the piece of code:
use List::Util qw(shuffle);
my @lines;
my @reordered;
my $filepath1 ="C:/Users/SravanthiBekkam/Desktop/pearl/data.txt";
my $filepath2 ="C:/Users/SravanthiBekkam/Desktop/pearl/temp.txt";
my $fhandle;
my $handle;
open ( $fhandle, "<", $filepath);
while (<$fhandle>) {
push( @lines, $_);
}
@reordered = shuffle(@lines);
open ( $handle, ">", $filepath2);
foreach (@reordered) {
print $handle "$_\n";
}
close $fhandle;
close $handle;
unlink $fhandle;
rename($handle, $fhandle);
In the above code I stored the file into the @lines
array and shuffled the array and rewriting into the another file and am removing the previous file and renaming the appended file as original file.
Expected to shuffle the lines in a same file or write into another at least.