CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



psynt555
New User

Nov 24, 2012, 9:08 AM

Post #1 of 5 (1982 views)
Regex to compare (if) two strings (Uniquely - I will explain inside) Can't Post

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";

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


Laurent_R
Veteran / Moderator

Nov 24, 2012, 10:50 AM

Post #2 of 5 (1978 views)
Re: [psynt555] Regex to compare (if) two strings (Uniquely - I will explain inside) [In reply to] Can't Post

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.


rovf
Veteran

Nov 26, 2012, 1:17 AM

Post #3 of 5 (1968 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:


Code
$_=$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)


Laurent_R
Veteran / Moderator

Nov 26, 2012, 10:30 AM

Post #4 of 5 (1954 views)
Re: [rovf] Regex to compare (if) two strings (Uniquely - I will explain inside) [In reply to] Can't Post

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.


rovf
Veteran

Nov 26, 2012, 1:53 PM

Post #5 of 5 (1951 views)
Re: [Laurent_R] Regex to compare (if) two strings (Uniquely - I will explain inside) [In reply to] Can't Post

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives