Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

unscrambles scrambled words using a dictionary - Perl Code Bank


unscrambles scrambled words using a dictionary
uses a file of words to find unscrambled words from a file of scrambled words. algorithm is words should be the same length of scrambled words and have only its characters.
                #!/usr/bin/perl
# usage: unscramble.pl scrambled_list_file word_list_file
use strict;

main(@ARGV);

sub main{
	my $scrambled_file = shift;
	my $wordlist_file = shift;
	open my $fh,"<$wordlist_file" or die "cannot open the file: $!";
	my @wordlist = <$fh>;
	close $fh;
	open my $fh,"<$scrambled_file" or die "cannot open the file: $!";
	my @scrambled = <$fh>;
	close $fh;
	foreach my $scrambled (@scrambled){
		$scrambled =~ s/\r\n//g;
		$scrambled =~ s/\n//g;
		print $scrambled. " -> ";
		my @chars = split '',$scrambled;
		foreach my $word (@wordlist){
			$word =~ s/\r\n//g;
			$word =~ s/\n//g;
			my $flag = 1;
			if (length $word == length $scrambled){
				foreach my $char (@chars){
					if (not $word =~ /[$char]/){
						$flag = 0;
						next;
					}
				}
				my $allchars = "0123456789abcdefghijklmnopqrstuvwxyz";
				$allchars =~ s/[@chars]//g;
				my @nochars = split '',$allchars;
				foreach my $char (@nochars){
					if ($word =~ /[$char]/){
						$flag = 0;
						next;
					}
				}
				if ($flag == 1){
					print $word ."\n";
				}
			}
		}
	}
}
            
Comments
Sorry but there are no comments to display