I tried removing all newlines ( \\n
) from the values of a hash:
my %var_h = ( "ID" => " This is Test
This is new line TEST
newline Test end ");
How can I remove all the new lines from the values of %var_h
?
I tried s/\\\\n//g
but I could not get it to work.
s:\n::g for values %var_h;
应该可以。
What are you running the substitution against? You seem to want to fix only the values, so I would use the values
keyword:
for my $value (values %var_h) {
$value =~ s/\n//g;
}
An alternative (at OP's request) would be to use map and a slice though I find it a lot less clear:
@var_h{keys %var_h} = map { s/\n//g } values %var_h;
All other solutions remove \\n from all values of hash.
I'm not sure if this is required.
So to remove only from this one value you have:
$var_h{'ID'} =~ s/\r?\n//g;
Technically s/\\n//g should be sufficient, but I have the habit of adding \\r? before it, so it will handle Windows-styled new lines as well.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.