I am using perl to produce an array of elements arranged as a single, tab-delimited line. However, only a portion of my array is like this. Other parts get printed as separate lines.
Below is the relevant portion of the code.
It is a foreach loop that has three conditional for loops embedded in it. The push command is used four times at four different if/else conditions. The particular array I’m having the issue with is called @imputed_positions. Several variables are defined near the beginning, which can be ignored. I don’t think those are the problem.
The output I get is correctly printed as a single line when the variable $distance is> 1, even though those values get processed by 3 separate instances of the push command (the first three). Values for $distance that are > 1 and also floating point values for $distance get printed as a single line. When $distance is < 1 or = 1, they get printed as separate lines. These lines correspond to elements pushed into @imputed_positions by the last of the four push commands.
I can’t recognize an analogous problem within “Similar Questions,” probably because I don’t have a precise enough clue of what the issue is.
Thanks!!!
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;
}
Maybe it would help if I explained the input. The @distances is just a txt file, where each line is a number. The numbers are all positive, and can be 0, integers, or floating point numbers. For eg, @distance = ( 1, 3, 5.9999, 4.9, 3.1, 3.000001, 0, 0, 0.3 ).
When @distance contains the above elements, the output does not print as a single line, but rather looks like:
IMP!1
!A IMP!1
!D IMP!1
!I IMP!1!A IMP!1!D IMP!1!I IMP!1!A IMP!1!D IMP!1!I IMP!1!A IMP!1!D IMP!1!IIMP!0.999999998333333!A IMP!0.999999998333333!D IMP!0.999999998333333!I IMP!0.999999998333333!A IMP!0.999999998333333!D IMP!0.999999998333333!I IMP!0.999999998333333!A IMP!0.999999998333333!D IMP!0.999999998333333!I IMP!0.999999998333333!AIMP!0.999999998333333!D IMP!0.999999998333333!I IMP!0.999999998333333!A IMP!0.999999998333333!D IMP!0.999999998333333!I IMP!0.999999998333333!A IMP!0.999999998333333!D IMP!0.999999998333333!I IMP!0.98!A IMP!0.98!D IMP!0.98!I IMP!0.98!A IMP!0.98!D IMP!0.98!I IMP!0.98!A IMP!0.98!D IMP!0.98!I IMP!0.98!A IMP!0.98!D IMP!0.98!I IMP!0.98!A IMP!0.98!D IMP!0.98!I IMP!1.03333333333333!A IMP!1.03333333333333!D IMP!1.03333333333333!I IMP!1.03333333333333!A IMP!1.03333333333333!D IMP!1.03333333333333!I IMP!1.03333333333333!A IMP!1.03333333333333!D IMP!1.03333333333333!I IMP!1.00000003333333!A IMP!1.00000003333333!D IMP!1.00000003333333!I IMP!1.00000003333333!A IMP!1.00000003333333!D IMP!1.00000003333333!I IMP!1.00000003333333!A IMP!1.00000003333333!D IMP!1.00000003333333!I IMP!0
!A IMP!0
!D IMP!0
!I IMP!0
!A IMP!0
!D IMP!0
!I IMP!0.3
!A IMP!0.3
!D IMP!0.3
!I
My guess is that the error is happening further up in your code, where you bring in the distances. It appears that you are not stripping the trailing newline from your input lines.
When you perform some computation with
$distance, Perl treats it as an integer. But unless you have explicitly converted$distanceto a numeric type (e.g.,$distance += 0;), it is treated as a string whenever possible, and so when you insert it into a string, your string will have newlines in it.If you have code similar to the following:
Change it to:
See the perldoc for more information.