in reply to Remove Carriage Return
The regex answers already given are the best, most "Perlish" solutions.
Here are three other ways to remove or replace a single character, using index and substr.
my $data1 = "abc\ndef"; my $data2 = "foobar\nbaz"; my $data3 = "Albus\nPercival\nWulfric\nBrian\nDumbledore\n"; my $pos1 = index($data1, "\n"); # $pos1 is 3 my $pos2 = index($data2, "\n"); # $pos2 is 6 my $pos3 = index($data3, "\n"); # $pos3 is 5 # Put together the part before $pos1, # and the part after $pos1. $data1 = substr($data1, 0, $pos1) . substr($data1, $pos1+1 ); # Tell the part of the string that contains # the "\n" to resize itself to nothing, as an lvalue. substr($data2, $pos2, 1) = ''; # Replace the part of the string that contains # the "\n" with three spaces. substr($data3, $pos3, 1, ' '); use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper($data1, $data2, $data3); # prints: # $VAR1 = "abcdef"; # $VAR2 = "foobarbaz"; # $VAR3 = "Albus Percival\nWulfric\nBrian\nDumbledore\n";
|
|---|