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";
}
perl
add a comment |
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";
}
perl
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
add a comment |
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";
}
perl
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
perl
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
add a comment |
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
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 11 at 5:40
Dave Cross
45.7k33877
45.7k33877
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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