Perl, Why it has a extra white space in output











up vote
0
down vote

favorite












input:
enter image description here



output:
enter image description here



but my out put have one extra white space on last two line.
my output:
enter image description here



my code:



@content = <FILE>; 

foreach $line (@content){
if($line =~ /^#(d+)/){
$number = $1;
$line =~ s/^#(d+)/$content[$number-1]/;
}
print "$line";
}









share|improve this question
























  • my code:@content = <FILE>; foreach $line (@content){ if($line =~ /^#(d+)/){ $number = $1; $line =~ s/^#(d+)/$content[$number-1]/; } print "$line"; }
    – Brian Li
    Nov 11 at 2:38










  • Idk why the output have extra white space
    – Brian Li
    Nov 11 at 2:43






  • 1




    Please edit your question and enter text (for input, output, and code) instead of images. If formatting is a problem there are detailed instructions on help pages (and I'll help now). I took care of the code you showed in the comment.
    – zdim
    Nov 11 at 3:04








  • 1




    By extra whitespace, do you mean the newlines your substitution ends up doubling up?
    – Shawn
    Nov 11 at 3:49










  • Please don't post images of text. Why do people thing that's useful? Please edit your question and replace the images with actual text.
    – Dave Cross
    Nov 11 at 5:27















up vote
0
down vote

favorite












input:
enter image description here



output:
enter image description here



but my out put have one extra white space on last two line.
my output:
enter image description here



my code:



@content = <FILE>; 

foreach $line (@content){
if($line =~ /^#(d+)/){
$number = $1;
$line =~ s/^#(d+)/$content[$number-1]/;
}
print "$line";
}









share|improve this question
























  • my code:@content = <FILE>; foreach $line (@content){ if($line =~ /^#(d+)/){ $number = $1; $line =~ s/^#(d+)/$content[$number-1]/; } print "$line"; }
    – Brian Li
    Nov 11 at 2:38










  • Idk why the output have extra white space
    – Brian Li
    Nov 11 at 2:43






  • 1




    Please edit your question and enter text (for input, output, and code) instead of images. If formatting is a problem there are detailed instructions on help pages (and I'll help now). I took care of the code you showed in the comment.
    – zdim
    Nov 11 at 3:04








  • 1




    By extra whitespace, do you mean the newlines your substitution ends up doubling up?
    – Shawn
    Nov 11 at 3:49










  • Please don't post images of text. Why do people thing that's useful? Please edit your question and replace the images with actual text.
    – Dave Cross
    Nov 11 at 5:27













up vote
0
down vote

favorite









up vote
0
down vote

favorite











input:
enter image description here



output:
enter image description here



but my out put have one extra white space on last two line.
my output:
enter image description here



my code:



@content = <FILE>; 

foreach $line (@content){
if($line =~ /^#(d+)/){
$number = $1;
$line =~ s/^#(d+)/$content[$number-1]/;
}
print "$line";
}









share|improve this question















input:
enter image description here



output:
enter image description here



but my out put have one extra white space on last two line.
my output:
enter image description here



my code:



@content = <FILE>; 

foreach $line (@content){
if($line =~ /^#(d+)/){
$number = $1;
$line =~ s/^#(d+)/$content[$number-1]/;
}
print "$line";
}






perl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 3:05









zdim

31.1k32040




31.1k32040










asked Nov 11 at 2:38









Brian Li

256




256












  • my code:@content = <FILE>; foreach $line (@content){ if($line =~ /^#(d+)/){ $number = $1; $line =~ s/^#(d+)/$content[$number-1]/; } print "$line"; }
    – Brian Li
    Nov 11 at 2:38










  • Idk why the output have extra white space
    – Brian Li
    Nov 11 at 2:43






  • 1




    Please edit your question and enter text (for input, output, and code) instead of images. If formatting is a problem there are detailed instructions on help pages (and I'll help now). I took care of the code you showed in the comment.
    – zdim
    Nov 11 at 3:04








  • 1




    By extra whitespace, do you mean the newlines your substitution ends up doubling up?
    – Shawn
    Nov 11 at 3:49










  • Please don't post images of text. Why do people thing that's useful? Please edit your question and replace the images with actual text.
    – Dave Cross
    Nov 11 at 5:27


















  • my code:@content = <FILE>; foreach $line (@content){ if($line =~ /^#(d+)/){ $number = $1; $line =~ s/^#(d+)/$content[$number-1]/; } print "$line"; }
    – Brian Li
    Nov 11 at 2:38










  • Idk why the output have extra white space
    – Brian Li
    Nov 11 at 2:43






  • 1




    Please edit your question and enter text (for input, output, and code) instead of images. If formatting is a problem there are detailed instructions on help pages (and I'll help now). I took care of the code you showed in the comment.
    – zdim
    Nov 11 at 3:04








  • 1




    By extra whitespace, do you mean the newlines your substitution ends up doubling up?
    – Shawn
    Nov 11 at 3:49










  • Please don't post images of text. Why do people thing that's useful? Please edit your question and replace the images with actual text.
    – Dave Cross
    Nov 11 at 5:27
















my code:@content = <FILE>; foreach $line (@content){ if($line =~ /^#(d+)/){ $number = $1; $line =~ s/^#(d+)/$content[$number-1]/; } print "$line"; }
– Brian Li
Nov 11 at 2:38




my code:@content = <FILE>; foreach $line (@content){ if($line =~ /^#(d+)/){ $number = $1; $line =~ s/^#(d+)/$content[$number-1]/; } print "$line"; }
– Brian Li
Nov 11 at 2:38












Idk why the output have extra white space
– Brian Li
Nov 11 at 2:43




Idk why the output have extra white space
– Brian Li
Nov 11 at 2:43




1




1




Please edit your question and enter text (for input, output, and code) instead of images. If formatting is a problem there are detailed instructions on help pages (and I'll help now). I took care of the code you showed in the comment.
– zdim
Nov 11 at 3:04






Please edit your question and enter text (for input, output, and code) instead of images. If formatting is a problem there are detailed instructions on help pages (and I'll help now). I took care of the code you showed in the comment.
– zdim
Nov 11 at 3:04






1




1




By extra whitespace, do you mean the newlines your substitution ends up doubling up?
– Shawn
Nov 11 at 3:49




By extra whitespace, do you mean the newlines your substitution ends up doubling up?
– Shawn
Nov 11 at 3:49












Please don't post images of text. Why do people thing that's useful? Please edit your question and replace the images with actual text.
– Dave Cross
Nov 11 at 5:27




Please don't post images of text. Why do people thing that's useful? Please edit your question and replace the images with actual text.
– Dave Cross
Nov 11 at 5:27












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Here's a version of your code with sample input data. If you want people to help you with problems like this, then it's a good idea to make it as easy as possible for them. Posting images of your input data does not make it easy. Also, it's a good development trick to store sample data in the DATA filehandle so that the code and data are together in the same file.



#!/usr/bin/perl

use strict;
use warnings;

my @content = <DATA>;

foreach my $line (@content){
if($line =~ /^#(d+)/){
my $number = $1;
$line =~ s/^#(d+)/$content[$number-1]/;
}
print "$line";
}

__DATA__
line A
line B
line C
#7
line D
#2
line E


I've also added use strict and use warnings to your code. In this case, they don't really help, but you should get into the habit of always including them in your Perl programs.



Your problem is here:



$line =~ s/^#(d+)/$content[$number-1]/;


Each of the lines in your @content array will include a newline character at the end. But in this line you're replacing the # symbol and the following digit with a complete other line from the array. You're not replacing the original newline and you're adding another newline (from the replacement string) so the line ends up containing two newlines.



The easiest fix is to add the newline to the pattern you are matching.



$line =~ s/^#(d+)n/$content[$number-1]/;


Note that an experienced Perl programmer would write your code like this:



#!/usr/bin/perl

use strict;
use warnings;

my @content = <DATA>;

for (@content){
s/^#(d+)n/$content[$1 - 1]/;
print;
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245380%2fperl-why-it-has-a-extra-white-space-in-output%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    Here's a version of your code with sample input data. If you want people to help you with problems like this, then it's a good idea to make it as easy as possible for them. Posting images of your input data does not make it easy. Also, it's a good development trick to store sample data in the DATA filehandle so that the code and data are together in the same file.



    #!/usr/bin/perl

    use strict;
    use warnings;

    my @content = <DATA>;

    foreach my $line (@content){
    if($line =~ /^#(d+)/){
    my $number = $1;
    $line =~ s/^#(d+)/$content[$number-1]/;
    }
    print "$line";
    }

    __DATA__
    line A
    line B
    line C
    #7
    line D
    #2
    line E


    I've also added use strict and use warnings to your code. In this case, they don't really help, but you should get into the habit of always including them in your Perl programs.



    Your problem is here:



    $line =~ s/^#(d+)/$content[$number-1]/;


    Each of the lines in your @content array will include a newline character at the end. But in this line you're replacing the # symbol and the following digit with a complete other line from the array. You're not replacing the original newline and you're adding another newline (from the replacement string) so the line ends up containing two newlines.



    The easiest fix is to add the newline to the pattern you are matching.



    $line =~ s/^#(d+)n/$content[$number-1]/;


    Note that an experienced Perl programmer would write your code like this:



    #!/usr/bin/perl

    use strict;
    use warnings;

    my @content = <DATA>;

    for (@content){
    s/^#(d+)n/$content[$1 - 1]/;
    print;
    }





    share|improve this answer

























      up vote
      2
      down vote



      accepted










      Here's a version of your code with sample input data. If you want people to help you with problems like this, then it's a good idea to make it as easy as possible for them. Posting images of your input data does not make it easy. Also, it's a good development trick to store sample data in the DATA filehandle so that the code and data are together in the same file.



      #!/usr/bin/perl

      use strict;
      use warnings;

      my @content = <DATA>;

      foreach my $line (@content){
      if($line =~ /^#(d+)/){
      my $number = $1;
      $line =~ s/^#(d+)/$content[$number-1]/;
      }
      print "$line";
      }

      __DATA__
      line A
      line B
      line C
      #7
      line D
      #2
      line E


      I've also added use strict and use warnings to your code. In this case, they don't really help, but you should get into the habit of always including them in your Perl programs.



      Your problem is here:



      $line =~ s/^#(d+)/$content[$number-1]/;


      Each of the lines in your @content array will include a newline character at the end. But in this line you're replacing the # symbol and the following digit with a complete other line from the array. You're not replacing the original newline and you're adding another newline (from the replacement string) so the line ends up containing two newlines.



      The easiest fix is to add the newline to the pattern you are matching.



      $line =~ s/^#(d+)n/$content[$number-1]/;


      Note that an experienced Perl programmer would write your code like this:



      #!/usr/bin/perl

      use strict;
      use warnings;

      my @content = <DATA>;

      for (@content){
      s/^#(d+)n/$content[$1 - 1]/;
      print;
      }





      share|improve this answer























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        Here's a version of your code with sample input data. If you want people to help you with problems like this, then it's a good idea to make it as easy as possible for them. Posting images of your input data does not make it easy. Also, it's a good development trick to store sample data in the DATA filehandle so that the code and data are together in the same file.



        #!/usr/bin/perl

        use strict;
        use warnings;

        my @content = <DATA>;

        foreach my $line (@content){
        if($line =~ /^#(d+)/){
        my $number = $1;
        $line =~ s/^#(d+)/$content[$number-1]/;
        }
        print "$line";
        }

        __DATA__
        line A
        line B
        line C
        #7
        line D
        #2
        line E


        I've also added use strict and use warnings to your code. In this case, they don't really help, but you should get into the habit of always including them in your Perl programs.



        Your problem is here:



        $line =~ s/^#(d+)/$content[$number-1]/;


        Each of the lines in your @content array will include a newline character at the end. But in this line you're replacing the # symbol and the following digit with a complete other line from the array. You're not replacing the original newline and you're adding another newline (from the replacement string) so the line ends up containing two newlines.



        The easiest fix is to add the newline to the pattern you are matching.



        $line =~ s/^#(d+)n/$content[$number-1]/;


        Note that an experienced Perl programmer would write your code like this:



        #!/usr/bin/perl

        use strict;
        use warnings;

        my @content = <DATA>;

        for (@content){
        s/^#(d+)n/$content[$1 - 1]/;
        print;
        }





        share|improve this answer












        Here's a version of your code with sample input data. If you want people to help you with problems like this, then it's a good idea to make it as easy as possible for them. Posting images of your input data does not make it easy. Also, it's a good development trick to store sample data in the DATA filehandle so that the code and data are together in the same file.



        #!/usr/bin/perl

        use strict;
        use warnings;

        my @content = <DATA>;

        foreach my $line (@content){
        if($line =~ /^#(d+)/){
        my $number = $1;
        $line =~ s/^#(d+)/$content[$number-1]/;
        }
        print "$line";
        }

        __DATA__
        line A
        line B
        line C
        #7
        line D
        #2
        line E


        I've also added use strict and use warnings to your code. In this case, they don't really help, but you should get into the habit of always including them in your Perl programs.



        Your problem is here:



        $line =~ s/^#(d+)/$content[$number-1]/;


        Each of the lines in your @content array will include a newline character at the end. But in this line you're replacing the # symbol and the following digit with a complete other line from the array. You're not replacing the original newline and you're adding another newline (from the replacement string) so the line ends up containing two newlines.



        The easiest fix is to add the newline to the pattern you are matching.



        $line =~ s/^#(d+)n/$content[$number-1]/;


        Note that an experienced Perl programmer would write your code like this:



        #!/usr/bin/perl

        use strict;
        use warnings;

        my @content = <DATA>;

        for (@content){
        s/^#(d+)n/$content[$1 - 1]/;
        print;
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 5:40









        Dave Cross

        45.7k33877




        45.7k33877






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245380%2fperl-why-it-has-a-extra-white-space-in-output%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma