How to write a double quote " "
Solved
helmii
Posted messages
35
Status
Member
-
[Dal] Posted messages 6122 Registration date Status Contributor Last intervention -
[Dal] Posted messages 6122 Registration date Status Contributor Last intervention -
Hello everyone, I have an array @array and I want to assign the string between parentheses (user "aaa") to $array[12]
Here is my code $array[12]=" user "aaa" ";
Nothing changes for $array[12] and it's because of the two quotes (" ") I think because when I remove them like this $array[12]=" user aaa "; the change is made!! How can we write the two quotes without them being considered string delimiters?? Thank you for your help.
Here is my code $array[12]=" user "aaa" ";
Nothing changes for $array[12] and it's because of the two quotes (" ") I think because when I remove them like this $array[12]=" user aaa "; the change is made!! How can we write the two quotes without them being considered string delimiters?? Thank you for your help.
3 answers
Hi helmii,
1.
You can do as suggested by the previous responses, by escaping the quotes ("double quotes") contained within your quotes.
2.
Another way to do it, if your content does not need to be interpolated and does not include apostrophes, is simply to use the apostrophe as a string delimiter:
3.
If you need to interpolate the content, and you are too lazy to escape the quotes, you can use qq:
(note: "qq" is for "double quotes", there is also "q" for "single quote" which is equivalent to the apostrophe, to avoid interpolation)
4.
Finally, there is also heredoc if you need to include text with quotes, apostrophes, line breaks,...
In non-interpolated form with apostrophes around the closing marker:
In interpolated version, with quotes instead, or without anything:
EOT is an example, you can put whatever you want. By convention, this is written in uppercase. The ; alone after the block is optional, I like to include it to avoid the warning that could theoretically display as mentioned at the end of this section of Perldoc.
Sources for further reading:
https://perldoc.perl.org/perlop#Quote-and-Quote-like-Operators
https://perldoc.perl.org/perlop#Quote-Like-Operators
Dal
1.
You can do as suggested by the previous responses, by escaping the quotes ("double quotes") contained within your quotes.
$array[12]=" user \"aaa\" ";
2.
Another way to do it, if your content does not need to be interpolated and does not include apostrophes, is simply to use the apostrophe as a string delimiter:
$array[12]=' user "aaa" ';
3.
If you need to interpolate the content, and you are too lazy to escape the quotes, you can use qq:
$array[12]= qq( user "$username" );
(note: "qq" is for "double quotes", there is also "q" for "single quote" which is equivalent to the apostrophe, to avoid interpolation)
4.
Finally, there is also heredoc if you need to include text with quotes, apostrophes, line breaks,...
In non-interpolated form with apostrophes around the closing marker:
$array[12] = <<'EOT'; I have an array @array and I want to assign the string between parentheses (user "aaa") to $array[12] here is my code $array[12]=" user "aaa" "; Nothing changes for $array[12] and this is EOT ;
In interpolated version, with quotes instead, or without anything:
$array[12] = <<EOT; This user is called $prenom, $nom. He is $age years old. He lives at $adresse. EOT ;
EOT is an example, you can put whatever you want. By convention, this is written in uppercase. The ; alone after the block is optional, I like to include it to avoid the warning that could theoretically display as mentioned at the end of this section of Perldoc.
Sources for further reading:
https://perldoc.perl.org/perlop#Quote-and-Quote-like-Operators
https://perldoc.perl.org/perlop#Quote-Like-Operators
Dal