Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:10:37+00:00 2026-06-14T11:10:37+00:00

I am using perl to produce an array of elements arranged as a single,

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:10:39+00:00Added an answer on June 14, 2026 at 11:10 am

    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 $distance to 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:

    while(my $line = <INPUT>) {
        # Do some stuff with $line
    }
    

    Change it to:

    while(my $line = <INPUT>) {
        chomp $line;
        # Now do your stuff with $line
    }
    

    See the perldoc for more information.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Moose class that i would like to store using Apache::Session::File. However,
I was using Perl to read through each line of a file. I used
So, in Perl and PHP, you can produce a block of text using a
This question has been rephrased. I am using CPAN Perl modules WWW::Mechanize to navigate
Using Perl XML::Twig, how can I loop on each sibling until reaching the last
Using Perl, how do I check if a particular Windows process is running or
Compare using perl -w -Mstrict : # case Alpha print $c; ... # case
While using perl debugger, is there any way to step out of the current
I am using Perl and SOAP::Lite to pull ticket information from a system called
I've been using perl, MongoDB, and GridFS for some pet development, and wonder -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.