I am getting output but I'm getting 300 lines that are exactly the same.Of course you are.while (<IN>){ while ($count<=$size){ rand($.)<1 && ($line=$_); print OUT $line; $count++; } }
For each line in the file, $_ has a specific value. After that, for 300 times, you decide, based on a random number, to assign this one value to $line. Always the exact same string. And then you print it out.
Whether you assign a value to a variable or not to a variable that already has been set to this value, it doesn't change a thing.
So, where's your thinko... It's quite obvious to me where you got the basis for the algorithm, it is (or used to be) in the official Perl FAQ. It goes something like this:
So you loop through the file, assign or don't assign the current value to $line based on a random value, and in the end, you print out what you have got.while (<IN>){ rand($.)<1 && ($line=$_); } print OUT $line;
If you insist to do this 300 times, you will have to read through the file 300 times.
If you don't want to do that, and you've got memory to spare, you can first read the contents of the file into an array, and randomly pick a line from that array.
Using the same algorithm (for no good reason, it was chosen because it works without keeping everything in memory at once), this becomes:
but it'll be a lot shorter to just writemy @lines = <IN>; for my $c (1 .. 300) { my $line; for my $i (0 .. $#lines) { rnd($i+1)<1 and $line = $#lines[$i]; } print OUT $line; }
my @lines = <IN>; for my $c (1 .. 300) { print OUT $lines[int rand @lines]; }
That leaves in duplicates. If you don't want duplicates, simply import the shuffle function from List::Util, shuffle the lines array, and print out the first 300.
This assumes there are at least 300 lines in the file, or you'll get a bunch of undefined values at the end.use List::Util qw(shuffle); my @lines = shuffle(<IN>); print OUT @lines[0 .. 299];
In reply to Re: Picking Random Lines from a File
by bart
in thread Picking Random Lines from a File
by de2425
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |