I came across the following Perl example on the web.
#!/usr/bin/perl
$string = 'the cat sat on the mat.';
$string =~ tr/a-z/b/d;
print "$string\n";
result:
b b b.
Can someone please explain how ?
On
d is used to delete found but not replaced characters.
To remove the characters which are not in the matching list can be done by appending d to the end of the tr operator.
#!/usr/bin/perl
my $string = 'my name is serenesat';
$string =~ tr/a-z/bcd/d;
print "$string\n";
Prints:
b b
Not matching characters in the string is removed, and only the matching character replaced (a replaced with b).
/ddenotesdelete.It's quite unusual to do a
trlike that because it's confusing.would delete all 'a-z' characters.
would transliterate all
a-zcharacters tob.What's happening here though is - because your transliteration doesn't map an equal number of character on each side - anything that doesn't map is deleted.
So what you're effectively doing is:
E.g. transliterating all the
as tobs and then deleting anything else (except spaces and dots).To illustrate:
Warns:
and prints:
If you change that to:
You get instead:
And thus:
t->xandh->y.ejust gets deleted.