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.

In reply to Guess the output. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.