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;
}
}
一直出错,说编译错误,实在找不出原因,希望各位帮忙,谢谢!!
本人急用,想问一个程序:
我有一个文件要随机产生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.
还有我的那个条件没有写错吗,可以按我的要求做?
回复 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
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28