sample_file sample_numbern"; } my($sampl"og:type" content="webpage"> sample_file sample_numbern"; } my($sampl" /> sample_file sample_numbern"; } my($sampl"> perl实现从一个文件中随机抽取n行_语言综合_内存溢出

阅读 38

perl实现从一个文件中随机抽取n行,第1张

概述源码之前,了无秘密,直接上源码: 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 @cont

源码之前,了无秘密,直接上源码:

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";

原文:http://www.voidcn.com/article/p-ozgsrdev-baq.html

作者:hongchangfirst

hongchangfirst的主页:http://blog.csdn.net/hongchangfirst

总结 以上是内存溢出为你收集整理的perl实现从一个文件中随机抽取

n行全部内容,希望文章能够帮你解决perl实现从一个文件中随机抽取n行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:

内存溢出

原文地址:

http://outofmemory.cn/langs/1275854.html
文件
(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
半条命2秘籍 半条命2秘籍 一级用户组
Perl学习资料
上一篇
2022-06-09
Perl中的程序注释:单行注释和多行注释
2022-06-09

发表评论
请登录后评论... 登录
提交

    评论列表(0条)
保存
{label} {label} sample_file sample_numbern"; } my($sample_file, $sample_numb', author : '半条命2秘籍', cat_name : '语言综合', time_y_m : '2022年06月', time_d : '09', site_motto : '内存溢出' }; {script} {script}