How can I replace multiple whitespace with a single space in Perl?

前端 未结 3 1598
心在旅途
心在旅途 2021-02-19 06:17

Why is this not working?

$data = \"What    is the STATUS of your mind right now?\";

$data =~ tr/ +/ /;

print $data;
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-19 06:37

    You can also use tr with the "squash" option, which will remove duplicate replaced characters. See perlop for details.

    my $s = "foo      bar   fubb";
    $s =~ tr/ //s;
    

提交回复
热议问题