Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 为什么Perl是“给予”;Can';t修改标量输出中的字符串“;错误?_Arrays_Loops_Perl_If Statement_Hash - Fatal编程技术网

Arrays 为什么Perl是“给予”;Can';t修改标量输出中的字符串“;错误?

Arrays 为什么Perl是“给予”;Can';t修改标量输出中的字符串“;错误?,arrays,loops,perl,if-statement,hash,Arrays,Loops,Perl,If Statement,Hash,我对Perl非常陌生,这是我迄今为止最复杂的项目。如果我的解释有任何部分不合理或遗漏了某些内容,请道歉-我很乐意提供进一步的澄清。只有一行代码引起了我的问题 目标: 我有一个包含一列数据的文本文件。它是这样写的: 0 a,a,b,a b,b,b,a 1 a,b,b,a b,b,b,a haplo frequency "|5,a,b,a,a|" 0.202493719 "|2,b,b,b,a|" 0.161139191 &quo

我对Perl非常陌生,这是我迄今为止最复杂的项目。如果我的解释有任何部分不合理或遗漏了某些内容,请道歉-我很乐意提供进一步的澄清。只有一行代码引起了我的问题

目标:

我有一个包含一列数据的文本文件。它是这样写的:

0
a,a,b,a
b,b,b,a
1
a,b,b,a
b,b,b,a
haplo           frequency
"|5,a,b,a,a|"   0.202493719
"|2,b,b,b,a|"   0.161139191
"|3,b,b,b,a|"   0.132602458
0   #this is the number in the @diplo file and they increase in order from 0 up to 15
0.011     0.0023    0.003    0.0532    0.163    0.3421    0.128    0.0972    0.0869    0.05514    0.0219    0.0172    0.00824    0.00886    0.00196 #these are the frequencies associated with x,a,a,b,a where x is any number from 0 to 14.
它继续以升序排列,最多15个数字,每个数字后面的两行是四个a或b的组合,用逗号分隔。我已将此文件绑定到一个数组
@diplo
,以便指定它的特定行

我还得到了一个文件,其中包含两列带有标题的数据,我已将其转换为数组散列(两列中的每一列都是一个数组)。散列的名称是$lookup,数组名称是标题的名称。实际数组仅从每列中不是标题的第一个值开始。此文件如下所示:

0
a,a,b,a
b,b,b,a
1
a,b,b,a
b,b,b,a
haplo           frequency
"|5,a,b,a,a|"   0.202493719
"|2,b,b,b,a|"   0.161139191
"|3,b,b,b,a|"   0.132602458
0   #this is the number in the @diplo file and they increase in order from 0 up to 15
0.011     0.0023    0.003    0.0532    0.163    0.3421    0.128    0.0972    0.0869    0.05514    0.0219    0.0172    0.00824    0.00886    0.00196 #these are the frequencies associated with x,a,a,b,a where x is any number from 0 to 14.
该文件包含四个位置a或b的所有可能组合以及所有数字0-14及其相关频率。换句话说,它包括从“|0,a,a,a,a |”到“|13,b,b,b,b |”和“|14,b,b,b |”的所有可能组合

我希望我的Perl代码遍历@diplo中以a、a、b、a开头的每个字母组合,并记录与包含0-14之间每个数字的haplo数组行相关联的频率,例如,首先记录与“|0、a、a、b、a |”相关联的频率,然后记录“|1、a、a、b、a |”等

希望输出如下所示:

0
a,a,b,a
b,b,b,a
1
a,b,b,a
b,b,b,a
haplo           frequency
"|5,a,b,a,a|"   0.202493719
"|2,b,b,b,a|"   0.161139191
"|3,b,b,b,a|"   0.132602458
0   #this is the number in the @diplo file and they increase in order from 0 up to 15
0.011     0.0023    0.003    0.0532    0.163    0.3421    0.128    0.0972    0.0869    0.05514    0.0219    0.0172    0.00824    0.00886    0.00196 #these are the frequencies associated with x,a,a,b,a where x is any number from 0 to 14.
我的代码: 下面是我创建的Perl代码,希望能解决这个问题(创建数组还有很多,如果需要,我可以发布这些代码,但如果不需要,我不想发布大量代码):

my$irow=1#这是@diplo中的行/元素编号
my$lrow=0#这是$lookup{'haplo'}中的行/元素
我的$copynumber=0;
#打印“$copynumber,$diplo[2]”;
而($irow<$diplolines-1){
而($copynumber<15){
而($lrow<$uplines-1){
如果(“|$copynumber,$diplo[$irow]|”=$lookup{'haplo'}[$lrow]){{###这是导致错误的唯一一行
如果($copynumber==0){
打印“$diplo[$irow-1]\n”;
#打印“$lookup{'frequency'}[$lrow]\t”;
}
打印“$lookup{'frequency'}[$lrow]\t”;
}
$lrow=$lrow+1;
}
$lrow=0;
$copynumber=$copynumber+1;
}
$lrow=0;
$copynumber=0;
$irow=$irow+1;
}
但是,如果(“|$copynumber,$diplo[$irow]|“=$lookup{'haplo'}[$lrow])导致错误,
无法修改“]”附近标量赋值中的字符串“
。 我试着在这一行的各个元素周围加上语音标记、圆括号和撇号,但我还是得到了一些关于这个错误的变体。我不知道如何避免这个错误

对于这个冗长的问题,我深表歉意,任何帮助都将不胜感激


编辑:感谢您对
eq
的建议,它消除了错误,现在我对Perl的了解比以前多一点。但是,即使我现在没有收到错误,如果我在这一行的
if
循环中放入任何内容,例如打印一个数字,它也不会被执行。如果我将相同的命令放在
while
循环中,但放在
If
之外,它确实会被执行。我有严格的警告。有什么想法吗?

=
是赋值,
=
是数值比较,
eq
是字符串比较

不能修改字符串:

$ perl -e 'use strict; use warnings; my $foo="def"; 
           if ("abc$foo" = "abcdef") { print "match\n"; } '      
Found = in conditional, should be == at -e line 1.
Can't modify string in scalar assignment at -e line 1, near ""abcdef") "
Execution of -e aborted due to compilation errors.
非数值字符串在数值比较中的作用类似于零:

$ perl -e 'use strict; use warnings; my $foo="def"; 
           if ("abc$foo" == 0) { print "match\n"; } '
Argument "abcdef" isn't numeric in numeric eq (==) at -e line 1.
match
"|$copynumber,$diplo[$irow]|" eq $lookup{'haplo'}[$lrow]
字符串比较可能是您想要的:

$ perl -e 'use strict; use warnings; my $foo="def"; 
           if ("abc$foo" eq "abcdef") { print "match\n"; } '
match

=
是赋值,
=
是数值比较,
eq
是字符串比较

不能修改字符串:

$ perl -e 'use strict; use warnings; my $foo="def"; 
           if ("abc$foo" = "abcdef") { print "match\n"; } '      
Found = in conditional, should be == at -e line 1.
Can't modify string in scalar assignment at -e line 1, near ""abcdef") "
Execution of -e aborted due to compilation errors.
非数值字符串在数值比较中的作用类似于零:

$ perl -e 'use strict; use warnings; my $foo="def"; 
           if ("abc$foo" == 0) { print "match\n"; } '
Argument "abcdef" isn't numeric in numeric eq (==) at -e line 1.
match
"|$copynumber,$diplo[$irow]|" eq $lookup{'haplo'}[$lrow]
字符串比较可能是您想要的:

$ perl -e 'use strict; use warnings; my $foo="def"; 
           if ("abc$foo" eq "abcdef") { print "match\n"; } '
match

这是一个有问题的表达:

"|$copynumber,$diplo[$irow]|" = $lookup{'haplo'}[$lrow]
等号(
=
)是赋值运算符。它将其右侧的值指定给其左侧的变量。因此,左边的操作数需要是一个变量,而不是像这里这样的字符串

我认为你根本不想在这里做作业。我想你是在检查是否平等。所以不要使用赋值运算符,而是使用比较运算符

Perl有两个相等比较运算符
=
执行数字比较以查看其操作数是否相等,
eq
执行字符串比较。为什么Perl需要两个操作符?Perl会自动在字符串和数字之间进行转换,因此它不可能知道要进行哪种比较。所以你需要说出来


这两种比较有什么区别?嗯,考虑一下这个代码。

$x = '0';
$y = '0.0';
$x
$y
是否相等?这取决于你所做的比较。如果将它们作为数字进行比较,那么,是的,它们是相同的值(无论是整数还是实数,零都是相同的值)。但如果将它们作为字符串进行比较,它们是不同的(它们的起始长度不同)

所以我们现在知道了

$x == $y # this is true as it's a numeric comparison
$x eq $y # this is false as it's a string comparison
让我们回到您的代码:

"|$copynumber,$diplo[$irow]|" = $lookup{'haplo'}[$lrow]
我猜你是从这里开始的

"|$copynumber,$diplo[$irow]|" == $lookup{'haplo'}[$lrow]
但这是不对的,因为
|$copynumber,$diplo[$irow]|
显然是字符串,而不是数字。如果您试图使用一个看起来不像数字的值进行数字比较,Perl会给您一个警告

因此,您将其更改为
=
,这也不起作用,因为您现在将其更改为作业

您真正需要的是字符串比较:

$ perl -e 'use strict; use warnings; my $foo="def"; 
           if ("abc$foo" == 0) { print "match\n"; } '
Argument "abcdef" isn't numeric in numeric eq (==) at -e line 1.
match
"|$copynumber,$diplo[$irow]|" eq $lookup{'haplo'}[$lrow]

这是一个有问题的表达:

"|$copynumber,$diplo[$irow]|" = $lookup{'haplo'}[$lrow]
等号(