I was looking for something in perlop when I noticed this in the description for the tr/// operator:

If no string is specified via the =~ or !~ operator, the $_ string is + transliterated.

Never having thought of using !~ in conjunction with tr///, I wondered what it did, so I tried a few things:

C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[a][]" C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[b][]" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[bb][]" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[bc][]" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[b][]c" C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[b][]d" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[b][]" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[a][]c" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[ab][]" C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[a][]" C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[b][]" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[c][]" 1 C:\test>perl -wle"$_ = 'aaa'; print $_ !~ tr[bc][]" 1 C:\test>perl -wle"$_ = 'aab'; print $_ !~ tr[bc][]"

From which I concluded that in this 'inverted count-the-stars' mode, it returns a true or false value indicating whether the string doesn't contain any of the characters in the searchlist. Not exactly intuative, but could be useful sometime. So then I thought I'd try the delete flag, and the result surprised me no end.

Can you guess what this would do before you try it?

perl -wle"$_ = 'aaa'; $_ !~ tr[a][]d; print"

Were you right?

Can you explain it ;)


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re: Guess the output.
by ikegami (Patriarch) on Sep 27, 2006 at 03:33 UTC

    Can you guess what this would do before you try it?

    Yes.

    Update: Rearranged the post (including earlier updates) so more would be behind the spoiler tag.