$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.
$? 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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With