Why is this not working?
$data = "What is the STATUS of your mind right now?";
$data =~ tr/ +/ /;
print $data;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
$data =~ s/ +/ /;instead.Explanation:
The
tris the translation operator. An important thing to note about this is that regex modifiers do not apply in a translation statement (excepting-which still indicates a range). So when you usetr/ +/ /you’re saying “Take every instance of the characters space and+and translate them to a space”. In other words, thetrthinks of the space and+as individual characters, not a regular expression.Demonstration:
Using
sdoes what you’re looking for, by saying “match any number of consecutive spaces (at least one instance) and substitute them with a single space”. You may also want to use something likes/ +/ /g;if there’s more than one place you want the substitution to occur (gmeaning to apply globally).