in reply to Unique character in a string?

You can use a look-ahead like this

use strict; use warnings; my $str = q{abcdceefghfty}; for my $ch (qw(a b c d e f g)) { print qq{"$ch" Unique in "$str"\n} if $str !~ m{($ch)(?=.*\1)}; }

which produces

"a" Unique in "abcdceefghfty" "b" Unique in "abcdceefghfty" "d" Unique in "abcdceefghfty" "g" Unique in "abcdceefghfty"

Cheers,

JohnGG

Update: The above approach is flawed in that testing for a character that doesn't occur in the string at all would still succeed and show as unique. This is because testing that the string doesn't match multiple characters is also true if there are no characters.

The amended script below changes by looking for positive matches of a single character. It is a touch more complex because of the need to interpolate the character being sought into a negated character class, hence the qr{...}. Looking for a hard-coded character is simpler, e.g. to check that "c" was unique, $str =~ m{\A[^c]*(c)(?!.*\1)} would work. Here's the amended script.

use strict; use warnings; my $str = q{abcdceefghfty}; for my $ch (q{a} .. q{z}) { my $matchTxt = q {\A} . qq{[^$ch]*($ch)} . q {(?!.*\1)}; my $rxUniq = qr{$matchTxt}; print qq{"$ch" unique in "$str"\n} if $str =~ $rxUniq; }