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 ;)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Guess the output.
by ikegami (Patriarch) on Sep 27, 2006 at 03:33 UTC |