By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,710 Members | 1,960 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,710 IT Pros & Developers. It's quick & easy.

unscramble words and write to file (have code)

P: 6
hihi

Firstl i got most of this code from another person on another forum but I have been trying like crazy to modify it so that it reads a list of scrambled words from a file and outputs the unscrambled words to another file.

scrambled.txt = input file
unscrambled.txt = output file

here is the code:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
  3. #!/usr/local/bin/perl
  4.  
  5. my $dict = 'wordlist.txt';
  6. my @Unscrambled = ();
  7.  
  8. # Do not space-separate the word in this version.
  9. my $scrambled_word   = shift
  10.   or die "Must specify a word\n";
  11.  
  12. my $scrambled_length = length $scrambled_word;
  13. my $scrambled_sorted = join '', sort split '', $scrambled_word;
  14.  
  15. my $pattern = qr{
  16.     \A
  17.         (?:
  18.             [$scrambled_word]{$scrambled_length}
  19.         )
  20.         \n
  21.     \z
  22. }x;
  23.  
  24. open DICT, '<', $dict
  25.   or die "Cannot open '$dict': $!";
  26.  
  27. while () {
  28.     next unless /$pattern/o;
  29.  
  30.     chomp;
  31.     my $sorted = join '', sort split '', $_;
  32.     next unless $sorted eq $scrambled_sorted;
  33.  
  34.             push @Unscrambled, $_;
  35. }
  36.  
  37. close DICT or warn;
  38.  
  39.  
  40. open (MYFILE, '>>data.txt');
  41.         foreach (@Unscrambled) {
  42.                   print MYFILE "$_";
  43. }
  44. close (MYFILE);
  45.  
And I'm pretty new to perl..

thanks allot!
Sep 26 '08 #1
Share this Question
Share on Google+
13 Replies


KevinADC
Expert 2.5K+
P: 4,059
If you're going to copy code off a forum at least copy it right:

Expand|Select|Wrap|Line Numbers
  1. while () {
should be:

Expand|Select|Wrap|Line Numbers
  1. while (<DICT>) {
Sep 26 '08 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
Also, this:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
should always be the very first line in your script. If it is not, it will not be processed as it will be considered a comment. On a Unix system, that means that you would not be able to just make the script executable, you would need to trigger it as an option to the Perl binary.

Regards,

Jeff
Sep 26 '08 #3

P: 6
ok so I modified it:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. my $dict = 'wordlist.txt';
  6. my @scrambled=();
  7.  
  8. open (FILE, "input.txt") or die "Couldn't open location file: $!";
  9. @scrambled = <FILE>;
  10. close FILE;
  11.  
  12. open FILE, ">output.txt" or die $!; 
  13.  
  14. foreach my $scrambled_word(@scrambled) {
  15.  
  16. # Do not space-separate the word in this version.
  17.  
  18. my $scrambled_length = length $scrambled_word;
  19. my $scrambled_sorted = join '', sort split '', $scrambled_word;
  20.  
  21. my $pattern = qr{
  22.     \A
  23.         (?:
  24.             [$scrambled_word]{$scrambled_length}
  25.         )
  26.         \n
  27.     \z
  28. }x;
  29.  
  30. open DICT, '<', $dict
  31.   or die "Cannot open '$dict': $!";
  32.  
  33. while (<DICT>) {
  34.     next unless /$pattern/o;
  35.  
  36.     chomp;
  37.     my $sorted = join '', sort split '', $_;
  38.     next unless $sorted eq $scrambled_sorted;
  39.  
  40.     print "$scrambled_word";
  41.     print FILE "$_\n"; 
  42. }
  43. }
  44. close FILE;
  45. close DICT or warn;
Still no luck, It's not printing anything into the output file.
Sep 26 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
You code should not even run, syntax errors. Line 22 is missing the semi-colon on the end. If nothing prints to the output file its because line 25 or line 28 never find a match. What is the input and what do you expect for output?
Sep 26 '08 #5

P: 6
You code should not even run, syntax errors. Line 22 is missing the semi-colon on the end. If nothing prints to the output file its because line 25 or line 28 never find a match. What is the input and what do you expect for output?
it finds a match, I have a huge word list, finding the match isnt the problem because it works fine without the file i/o stuff. just manually inputting a string via command prompt "unscr.pl word" works perfectly well and never fails. Just doesnt seem to want to work with files.

The format of the file is
asinspo
rithwg
eednis
raltce

the output should be those words but unscrambled and placed into another text file.
Sep 26 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
are the words in both files all lower case or all upper case? They need to match exactly for your script to find matches.
Sep 26 '08 #7

P: 6
are the words in both files all lower case or all upper case? They need to match exactly for your script to find matches.
all lower case, works fine before i attempt to do it with reading a list of words from a file.
Sep 26 '08 #8

KevinADC
Expert 2.5K+
P: 4,059
Try removing the \n from the regexp:

Expand|Select|Wrap|Line Numbers
  1. my $pattern = qr{
  2. \A
  3. (?:
  4. [$scrambled_word]{$scrambled_length}
  5. )
  6. \n<--- remove this
  7. \z
  8. }x;
  9.  
and see if that helps.
Sep 27 '08 #9

P: 6
nope :( still no luck with it.. hmmmmmm

it seems that nothing displays after these 2 "needed" lines:

my $sorted = join '', sort split '', $_;
next unless $sorted eq $scrambled_sorted;

but before these lines i get a response however not unscrambled. I have no idea why it doesn't work with reading a list of words from a file other than passing 1 word via command prompt.

if i use "print $_;" under those 2 lines like in the original script i get the unscrambled word, but when modifies to read a list from a file i get nothing with print, like its wiped clear..
Sep 27 '08 #10

KevinADC
Expert 2.5K+
P: 4,059
Replace the "while" block with this code:

Expand|Select|Wrap|Line Numbers
  1. while (<DICT>) {
  2.     next unless /$pattern/o;
  3.     print "Passed regexp: $_";
  4.     chomp;
  5.     my $sorted = join '', sort split '', $_;
  6.     print "\$sorted = [$sorted]\n \$scrambled_sorted = [$scrambled_sorted]\n";
  7.     next unless $sorted eq $scrambled_sorted;
  8.  
  9.     print "$scrambled_word";
  10.     print FILE "$_\n"; 
  11. }
and see what gets printed to the screen while it runs. The square brackets are there so you can see if $sorted or $scrambled_sorted have any spaces or other characters inthem that are affecting the comparison.
Sep 27 '08 #11

P: 6
hmm weird, prints out the words but unscrambled, each word is printed out 2 time's instead of once per word. also, i put just 1 word in the file "inandia"

inandia = indiana

and that word is in my dict.
Sep 28 '08 #12

KevinADC
Expert 2.5K+
P: 4,059
Well, with no data to run I can't really help anymore.
Sep 28 '08 #13

Icecrack
Expert 100+
P: 174
May i ask this isn't for the HackT***S*** Website is it?
Sep 28 '08 #14

Post your reply

Sign in to post your reply or Sign up for a free account.