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

  • Home
  • SEARCH
  • 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 6921095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:13:56+00:00 2026-05-27T10:13:56+00:00

I have this snippet; print $score\n; for (my $i = 0; $i < $n;

  • 0

I have this snippet;

print "$score\n";
for (my $i = 0; $i < $n; $i++)
{
print $output[$i];
if($i == $center)
{
    print "*";
}
print "\n";
}

and it should print this:

-62
ttagggcccgg-a-tc---attaccgggc--caa
tta--gcgcgg-attcg-gatta-cggg---c-a*
gcg--gggcggcattagcaattt-gggg-atc-a
-ta--gcgc---a-----aataa-ccgg------

but instead it prints this:

-62
ttagggcccgg-a-tc---attaccgggc--caa
*ta--gcgcgg-attcg-gatta-cggg---c-a
gcg--gggcggcattagcaattt-gggg-atc-a
-ta--gcgc---a-----aataa-ccgg------

If i remove the “*”, the strings are correct (they are really the strings i want to print), the problem is its placing. this is printed:

-62
ttagggcccgg-a-tc---attaccgggc--caa
tta--gcgcgg-attcg-gatta-cggg---c-a
gcg--gggcggcattagcaattt-gggg-atc-a
-ta--gcgc---a-----aataa-ccgg------

Why is it printing on the beginning of the string, and not on te end as it is supposed to?

Thanks a lot.

  • 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-05-27T10:13:56+00:00Added an answer on May 27, 2026 at 10:13 am

    Your code works perfectly on a *nix system where \n is the only line terminator and the lines of text in @output do not end in \n.

    #!/usr/bin/perl
    
    my @output = ("ttagggcccgg-a-tc---attaccgggc--caa",
                  "tta--gcgcgg-attcg-gatta-cggg---c-a",
                  "gcg--gggcggcattagcaattt-gggg-atc-a",
                  "-ta--gcgc---a-----aataa-ccgg------");
    
    my $center = 1;
    my $score = -62;
    
    print "$score\n";
    for (my $i = 0; $i < 4; $i++)
    {
        print $output[$i];
        if($i == $center)
        {
            print "*";
        }
        print "\n";
    }
    

    Output:

    -62
    ttagggcccgg-a-tc---attaccgggc--caa
    tta--gcgcgg-attcg-gatta-cggg---c-a*
    gcg--gggcggcattagcaattt-gggg-atc-a
    -ta--gcgc---a-----aataa-ccgg------
    

    The only way I see your code generating the output you post is if each line of text contained in @output ends with a \r which would cause the carriage return to occur, then you’d print the * (which would overwrite the first character of the line you just output) followed by the \n at the bottom of your loop.

    If I change @output in my example to:

    my @output = ("ttagggcccgg-a-tc---attaccgggc--caa\r",
                  "tta--gcgcgg-attcg-gatta-cggg---c-a\r",
                  "gcg--gggcggcattagcaattt-gggg-atc-a\r",
                  "-ta--gcgc---a-----aataa-ccgg------\r");
    

    Then the output is:

    -62
    ttagggcccgg-a-tc---attaccgggc--caa
    *ta--gcgcgg-attcg-gatta-cggg---c-a
    gcg--gggcggcattagcaattt-gggg-atc-a
    -ta--gcgc---a-----aataa-ccgg------
    

    SO the final answer is: Get rid of the \r at the end of those. 🙂

    Edit: As TLP points out in the comments below, using a regex substitution to make sure that there’s nothing on the end of those lines should fix you up:

    for (my $i = 0; $i < 4; $i++)
    {
        $output[$i] =~ s/\s*$//;
        print $output[$i];
        ...
    

    Last Edit: From the comments below from TLP, I would guess this data is read from a file that came from a windows machine where lines end in \r\n. When reading that file in perl on OSX, you’re using chomp to remove newlines. In OSX, chomp is only going to remove \n (by default) and leave the \r intact.

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

Sidebar

Related Questions

I have this webservice snippet and i want to print the actual value returned.
I have this snippet I found. svn status | grep '\!' | awk '{print
I have this snippet of shell script: am start -n com.android.gallery3d/com.android.gallery3d.app.MovieActivity -d /sdcard/movie.mp4 sleep
I have this snippet of code: var ShopLogicOptions = {}; ShopLogicOptions.params = {orderId: '
I have this snippet of code private Templates retrieveFromCache(String name) { TemplatesWrapper t =
I have this snippet RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond $1
I have this snippet and there are 3 images and 3 folders in the
I have this snippet of code below. I want to pass the value of
I have this jQuery snippet, I have stored the variables to highlight them. var
I have this code snippet: $(function() { $('.toolbar [id^=button]').on('click', function () { $(this) .css('background-color',

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.