Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of \0 in perl?

Tags:

perl

$a = "aaata";
$b = "aataa";
$count = ($a ^ $b) =~tr/\0//c;  

output 2 (because of two miss matches perform by the c flag) without using c flag output is 3(matches)

Here what is the use of \0 in tr. Without using tr, script gives the some gibberish character. I don't know what is this and use of tr in here and use of the \0. Apart from this where we use the \0 in perl.

like image 415
mkHun Avatar asked Aug 06 '16 09:08

mkHun


People also ask

What is $? In Perl?

$? is the error code of the child process (perform_task.sh). In the case of your script the return code is shifted eight bits to the right and the result compared with 0. This means the run is considered a failure only if the returned code is > than 255.

What does '\ o Represent in C ++?

In a C-style string, '\0' denotes the null terminator; it's used to mark the end of the string. So any functions that process/display strings will stop as soon as they hit it (which is why your last string is truncated). The first example is creating a fully-formed C++ std::string object.

What is the '\ 0 used for at the end of a string?

Strings in C A C string is a sequence of characters followed by a null character ('\0'). Thus, a null character is used to mark the end of the string. A null character is a character in which all bits are 0s. Hence we use the escape sequence '\0' to refer to this character.


1 Answers

In general, an escape sequence consisting of up to three octal digits will insert a character with that code point, so \40 or \040 produce a space character, and \0 produces an ASCII NUL

The code is counting the number of characters that are different between $a and $b

It does a bitwise XOR on the two strings. Any characters that are identical will XOR together to zero, producing a NUL character. The tr/\0//c counts the number of characters in the resulting string that other than NULs (because of the /c modifier) so it will return 2 in this case because the two strings are different at the third and fourth character positions

Dumping the value of the expression $a ^ $b shows this clearly

"\0\0\25\25\0"

The tr/// counts the two \25 characters, ignoring all NULs

like image 150
Borodin Avatar answered Nov 11 '22 22:11

Borodin