How to write a double quote " "

Solved
helmii Posted messages 35 Status Member -  
[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.

3 answers

[Dal] Posted messages 6122 Registration date   Status Contributor Last intervention   1 108
 
Hi helmii,

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
1
Lapourax Posted messages 2970 Registration date   Status Contributor Last intervention   336
 

 echo " \" " "

The \ is used to signal to the shell that the character is not a meta-character

In short, the shell will display your " like any other character =)

L@pourax
If you don't succeed on the first try, call it version 1.0
0
blux Posted messages 5021 Registration date   Status Moderator Last intervention   3 455
 
Hello,

you need to 'unspecialize' your double-quote by preceding it with a backslash (\), like this: \"

Cya blux
 "Les cons, ça ose tout. C'est même à ça qu'on les reconnait"
0