Arrays 我需要一个单线阵列。我使用嵌套循环将元素推入数组。该数组包含多行
我正在使用perl生成一个元素数组,这些元素被排列成一条以制表符分隔的单行。然而,我的数组中只有一部分是这样的。其他部分作为单独的行打印 下面是代码的相关部分 它是一个foreach循环,其中嵌入了三个条件Arrays 我需要一个单线阵列。我使用嵌套循环将元素推入数组。该数组包含多行,arrays,perl,push,Arrays,Perl,Push,我正在使用perl生成一个元素数组,这些元素被排列成一条以制表符分隔的单行。然而,我的数组中只有一部分是这样的。其他部分作为单独的行打印 下面是代码的相关部分 它是一个foreach循环,其中嵌入了三个条件for循环。push命令在四种不同的if/else条件下使用四次。我遇到问题的特定数组称为@imputed\u positions。在开头附近定义了几个可以忽略的变量。我认为这不是问题所在 当变量$distance为>1时,我得到的输出正确打印为单行,即使这些值由push命令的3个单独实例(前
for
循环。push
命令在四种不同的if
/else
条件下使用四次。我遇到问题的特定数组称为@imputed\u positions
。在开头附近定义了几个可以忽略的变量。我认为这不是问题所在
当变量$distance
为>1
时,我得到的输出正确打印为单行,即使这些值由push命令的3个单独实例(前三个)处理。$distance
的值为>1
,并且$distance的浮点值也将打印为一行。当$distance
为<1
或=1
时,它们将作为单独的行打印。这些行对应于通过四个push命令中的最后一个推入@imputed_位置的元素
我无法在“类似问题”中识别出类似的问题,可能是因为我没有足够准确的线索知道问题是什么
谢谢
foreach my $distance ( @distances ) {
if ( $distance > 1 &&
( int ( $distance ) != $distance ) ) { ###just asking whether $variable is an integer and whether it is > than 1.
my $rounded_up = rounding_up( $distance );
my $rounded_down = rounding_down( $distance );
my $up_distance = $distance/$rounded_up;
my $down_distance = $distance/$rounded_down;
my $abs_up = abs ( 1 - $up_distance );
my $abs_down = abs ( 1 - $down_distance );
if ( $abs_up < $abs_down ) {
for ( my $i = 0; $i < $rounded_up; $i++ ) {
push ( @imputed_positions, "IMP!$up_distance!A\tIMP!$up_distance!D\tIMP!$up_distance!I\t" );
}
}
else {
for ( my $i = 0; $i < $rounded_down; $i++ ){
push ( @imputed_positions, "IMP!$down_distance!A\tIMP!$down_distance!D\tIMP!$down_distance!I\t" );
}
}
}
else {
if ( $distance > 1 ){
for ( my $i = 1; $i <= $distance; $i++ ) {
push ( @imputed_positions, "IMP!1!A\tIMP!1!D\tIMP!1!I\t" );
}
}
else {
push ( @imputed_positions, "IMP!$distance!A\tIMP!$distance!D\tIMP!$distance!I\t" );
}
}
}
#print @imputed_positions;
#rounding down subroutine
sub rounding_down {
my ( $round_me ) = @_;
my $rounded_down = int( $round_me );
return $rounded_down;
}
#rounding up subroutine
sub rounding_up {
my ( $round_me ) = @_;
my $rounded_up = int( $round_me ) + 1;
return $rounded_up;
}
我的猜测是,错误发生在代码的更高层,在那里,你带来了距离。看起来您没有从输入行中剥离尾随的换行符 当您使用
$distance
执行某些计算时,Perl将其视为一个整数。但是,除非您已将$distance
显式转换为数字类型(例如,$distance+=0;
),否则尽可能将其视为字符串,因此当您将其插入到字符串中时,字符串中将包含换行符
如果您的代码与以下类似:
while(my $line = <INPUT>) {
# Do some stuff with $line
}
while(我的$line=){
#用$line做一些事情
}
将其更改为:
while(my $line = <INPUT>) {
chomp $line;
# Now do your stuff with $line
}
while(我的$line=){
chomp$行;
#现在用$line做你的东西
}
有关更多信息,请参阅。能否提供您期望的输出和实际获得的输出的示例?嗨,是的,我可以@WinnieNicklaus。将尝试正确设置此答复的格式。预期输出:
IMP!小淘气!1D小鬼!我是小鬼!0.01A小鬼!0.01D小鬼!0.001I…
对于$distance的任何值,都作为一行。打印时看到的是,每当$distance为1或<1时,“IMP!$distance!a\tIMP!$distance!D\tIMP!$distance!I\t”的每个块都有一行。谢谢@阿蒙对我的帖子进行了修改。我是这个网站的新手,正在学习如何格式化帖子。谢谢!我添加了chomp$距离代码>就在代码顶部的foreach命令之后。我只有一行。看起来是对的。谢谢我认为这是“push”的问题,而不是perl如何处理整数的问题。我不会在类似的问题中寻找这一点。我在哪里可以阅读更多关于这个问题的信息?谷歌“强类型”和“弱类型”来了解更多关于各种语言的行为。Perl是一种著名的弱类型语言,其中可以混合不同类型的变量,解释器将尝试做正确的事情。另一个值得回顾的好东西是Perl“上下文”。这是类似的——例如,如果在“标量上下文”中使用数组,Perl不会抱怨,它会尝试做一些合理的事情,即返回数组的大小。
while(my $line = <INPUT>) {
chomp $line;
# Now do your stuff with $line
}