+ -
当前位置:首页 → 问答吧 → perl条件语句

perl条件语句

时间:2010-11-21

来源:互联网

本帖最后由 susie_gan 于 2010-11-21 21:21 编辑

本人急用,想问一个程序:
我有一个文件要随机产生2个文件,如果满足条件则执行命令,如果不满足条件则继续随机,这样的语句要怎么写啊?
下面是我随机产生数的程序,条件就是第2列与第3列的比值的绝对值<0.03的输出,不满足的则继续对当前行随机,直到满足条件为止
#!/bin/perl

for (1..1000){
open AOUT,">a$_.txt" ;
open BOUT,">b$-.txt" ;
open IN,"<data.txt" ;
while (<IN>)  {
  /^\d+(\d+)\s+(\d+)/;
  #if($1==0){
          # next;
          #}
   my $temp=int((2/5+(rand($1)/$1)/5)*$1);
   my $temp2=int((2/5+(rand($2)/$2)/5)*$2);
   if(sqrt($temp/$temp2-($1-$temp)/($2-$temp2*($temp/$temp2-($1-$temp)/($2-$temp2)))<0.03){
   printf AOUT "%d %d %d\n",$_,$temp,$temp2;
   printf BOUT "%d %d %d\n",$_,$1-$temp,$2-$temp2;
   }
   else{
   my $temp=int((2/5+(rand($1)/$1)/5)*$1);
   my $temp2=int((2/5+(rand($2)/$2)/5)*$2);

close IN;
close AOUT;
close BOUT;
}
}
一直出错,说编译错误,实在找不出原因,希望各位帮忙,谢谢!!

作者: susie_gan   发布时间: 2010-11-21

sqrt这一行少了右括号

最后少了花括号

作者: iamlimeng   发布时间: 2010-11-21

本帖最后由 susie_gan 于 2010-11-21 21:41 编辑

回复 iamlimeng


    #!/bin/perl

for (1..1000){
open AOUT,">a$_.txt";
open BOUT,">b$-.txt";
open IN,"<data.txt" ;
while (<IN>)  {
  /^\d+(\d+)\s+(\d+)/;
  #if($1==0){
          # next;
          #}
   my $temp=int((2/5+(rand($1)/$1)/5)*$1);
   my $temp2=int((2/5+(rand($2)/$2)/5)*$2);
   if(sqrt(($temp/$temp2-($1-$temp)/($2-$temp2))*($temp/$temp2-($1-$temp)/($2-$temp2)))<0.03){
   printf AOUT "%d %d %d\n",$_,$temp,$temp2;
   printf BOUT "%d %d %d\n",$_,$1-$temp,$2-$temp2;
   }
   else{
   my $temp=int((2/5+(rand($1)/$1)/5)*$1);
   my $temp2=int((2/5+(rand($2)/$2)/5)*$2);

close IN;
close AOUT;
close BOUT;
}
}
修改后的
这是提示的错误:Missing right curly or square bracket at rand_reads-test.pl line 26, at end of line
syntax error at rand_reads-test.pl line 26, at EOF
Execution of rand_reads-test.pl aborted due to compilation errors.
还有我的那个条件没有写错吗,可以按我的要求做?

作者: susie_gan   发布时间: 2010-11-21