5 ms·
I've always loved the one-liner of this in Perl: http://learn.perl.org/faq/perlfaq5.html#How-do-I-select-a-random-line-from-a-file- http://learn.perl.org/faq/pe
by decklin 13y ago
I've always loved the one-liner of this in Perl: http://learn.perl.org/faq/perlfaq5.html#How-do-I-select-a-random-line-from-a-file- http://learn.perl.org/faq/perlfaq5.html#How-do-I-select-a-ra...
- tantalor 13y agoLet's unpack that, rand($.) < 1 && ($line = $_) while <>; We all know rand and while, but if you don't know perl the rest is hard. <> is a common way to read stdin, and the value is assigned to the $_ special variable. The one I didn't know was $., the current input line number. In other words, its your loop index that automatically increments. http://www.kichwa.com/quik_ref/spec_variables.html http://www.kichwa.com/quik_ref/spec_variables.html
- ceautery 13y agoThat's good until your input stream exceeds 32768 lines [1]. There are better generators available from CPAN, like Math::TrulyRandom. [1] - http://www.perl.com/doc/FMTEYEWTK/random http://www.perl.com/doc/FMTEYEWTK/random