
rovf
Veteran
Nov 26, 2012, 1:17 AM
Post #3 of 5
(3596 views)
|
Re: [psynt555] Regex to compare (if) two strings (Uniquely - I will explain inside)
[In reply to]
|
Can't Post
|
|
To verify, that a string contains only unique letters, you can use an arcane feature found in the tr operator in Perl. Assume that the string to check is stored in variable $x and that $x doesn't contain a forward slash (how to deal with this special case, is left as an exercise for you ;-). Now calculate the following:
$_=$x; eval "tr/$x/./d"; say "The string /$x/ doesn't contain only of unique characters" if length < length($x); The basic idea is to make a copy of the string and delete duplicate characters in the copy. If the new string is shorter than the old one, at least one character must have been duplicated. Deleting duplicates is a side effect of the tr/// operator. tr is mainly used for translating one character set into another one, and the 'd' modifier causes duplicates to be removed. In our case, we are not interested in the translation proper, but only in the side effect done by 'd'. See perldoc perlop for details.
(This post was edited by rovf on Nov 26, 2012, 1:18 AM)
|