Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by AnomalousMonk (Chancellor)
on Nov 24, 2012 at 18:39 UTC ( #1005414=note: print w/replies, xml ) Need Help??


in reply to Regex to compare (if) two strings (Uniquely - I will explain inside)

When chickenlips makes reference to Scrabble, what I assume is meant is that a word formed from the random characters need not use all of the random characters, but must use only those characters. E.g., 'mad' can be formed from 'randam', but 'madam' cannot (two 'm' characters).

use warnings FATAL => 'all' ; use strict; use Data::Dump; use constant DEBUG => 0; use constant { DBPR_DD_1 => 1 && DEBUG, }; my $random = 'randam'; my %r_count; $r_count{$_}++ for split '', $random; dd \%r_count if DBPR_DD_1; for my $word (qw(a mad ran madnar madam ranx xxxxxx x)) { my %u_count = %r_count; my $ok = scrabblicious($word, %u_count); my $len = 2 + length $random; printf qq{%*s %sproperly formed from '$random' \n}, $len, qq{'$word'}, $ok ? ' ' : 'im'; } sub scrabblicious { my ($word, %u_count) = @_; for my $c (split '', $word) { return if --$u_count{$c} < 0; } return 1; }

Output:

'a' properly formed from 'randam' 'mad' properly formed from 'randam' 'ran' properly formed from 'randam' 'madnar' properly formed from 'randam' 'madam' improperly formed from 'randam' 'ranx' improperly formed from 'randam' 'xxxxxx' improperly formed from 'randam' 'x' improperly formed from 'randam'

Update: The definition of  scrabblicious() given above will accept an empty string as a match for a 'tray' of characters. So just add
    return if not length $word;
before the for-loop.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005414]
help
Chatterbox?
[karlgoethebier]: Cojones! We need cojones!
[karlgoethebier]: Ouch! Permissions! We need permissions!
[BarApp]: I can not use modules. I gain temporary access and still can not use modules.
[Cosmic37]: ta erix - this szabo geezer is pretty cool methinks and he writes about undef but I cannot see instructions for redefining the record separator after having undefined it
[Corion]: $/ = "wahtever";
[Corion]: (it's a magic variable)
[karlgoethebier]: BarApp: whoami
[Cosmic37]: ok fankyou - I was wondering about that but thought there might be a redefine command or something; peachy
[Lotus1]: Cosmic37 if you undef $/ in a local context to a block it won't affect the global version after the block finishes

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-06-29 16:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (672 votes). Check out past polls.