Home: Perl Programming Help: Intermediate:
Regex to compare (if) two strings (Uniquely - I will explain inside)

New User

Nov 24, 2012, 9:08 AM

Views: 3570
Regex to compare (if) two strings (Uniquely - I will explain inside)

Obtained 8 random chars in a sub. Called it $random_string.

Input a word from user (that they make FROM the 8 random chars such as in Scrabble).

Gotta validate the inputted word by checking that it indeed consists of the chars in $random_string.

The KEY BIT I CANNOT FIGURE OUT IS: I need to check that any letters used in the randomly generated for the inputted word are only used ONCE.

Basically a simple version of Scrabble, this is what i have so far:

print "Please enter a word from the letters above: ";
chomp(my $word = <STDIN>); #INPUT WORD

if ($word =~ /[$random_string]$/i) {
print "Good word\n";

else {
print "try again\n";
print "\n";

Veteran / Moderator

Nov 24, 2012, 10:50 AM

Views: 3566
Re: [psynt555] Regex to compare (if) two strings (Uniquely - I will explain inside)

There are a number of ways you could do that.

- Build a hash with each unique letter of the random input as a key and the number of times is appears asd a value (random hash). Do the same with the letters of the word suggested by the user (word hash) and check that the values in the word hash are always smaller than or equal to the values in the random hash.

- Sort all the random letters in alphabetical order, sort the letters of the word similarly, and compare the sorted arrays, one letter at a time.

You may also split the letters in arrays and use some Perl modules to compare lists, such as List::Utils, etc.


Nov 26, 2012, 1:17 AM

Views: 3556
Re: [psynt555] Regex to compare (if) two strings (Uniquely - I will explain inside)

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:

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)

Veteran / Moderator

Nov 26, 2012, 10:30 AM

Views: 3542
Re: [rovf] Regex to compare (if) two strings (Uniquely - I will explain inside)

I don't think that is what the OP needs.

Assume you are selecting 8 letters randomly (such as in the Scrabble game) and try to make up an existing word from some of the letters. The OP wants to check that all the letters in the word are in the random set of letters and that the word thus constructed is not using twice one of the ramdom letters. But if you have twice the same letter in the random set of letters, then that letter may be used twice (because you are using one copy of each existing letters), but not a third time.

For example, if the random letters contain "d e e g i n n v", you are entitled to compose the word EVENING, even though E and N both come twice in that word, because those two letters also come twice in the random set of letters. But you could not use the word EVENED, because you are entitled to 2 E's, not 3.


Nov 26, 2012, 1:53 PM

Views: 3539
Re: [Laurent_R] Regex to compare (if) two strings (Uniquely - I will explain inside)

Ah, Scrabble I understand! Did not thought about this possibility ...