Before the source code, there is no secret, directly on the source code:
If (@ argv! = 2 ){
Die "Usage: $0 sample_file sample_number \ n ";
}
My ($ sample_file, $ sample_number) = @ argv;
Open my $ file, "<$ sample_file" or die "can't open $ sample_file $! \ N ";
My @ content = <$ File>;
My $ all_number = @ content;
Print stderr "| $ all_number | \ n ";
My % hits;
My $ WL = $ sample_number;
For (my $ I = 0; $ I <$ all_number; ++ $ I ){
If (rand ($ all_number-$ I) <$ wl ){
$ Hits {$ I} = 1;
Print stderr "$ WL \ n ";
-- $ Wl;
}
}
My $ J = 0;
Print stderr "Start \ n ";
Foreach (@ content ){
Print "$ _" If $ hits {$ J };
+ + $ J;
}
Print stderr "done. \ n ";
Original article: http://blog.csdn.net/hongchangfirst/article/details/25126457
Author: hongchangfirst
Hongchangfirst home: http://blog.csdn.net/hongchangfirst
Perl randomly extracts n rows from a file