unscramble words and write to file (have code)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • helpmesos
    New Member
    • Sep 2008
    • 6

    unscramble words and write to file (have code)

    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:

    Code:
    use strict;
    use warnings;
    #!/usr/local/bin/perl
    
    my $dict = 'wordlist.txt';
    my @Unscrambled = ();
    
    # Do not space-separate the word in this version.
    my $scrambled_word   = shift
      or die "Must specify a word\n";
    
    my $scrambled_length = length $scrambled_word;
    my $scrambled_sorted = join '', sort split '', $scrambled_word;
    
    my $pattern = qr{
        \A
            (?:
                [$scrambled_word]{$scrambled_length}
            )
            \n
        \z
    }x;
    
    open DICT, '<', $dict
      or die "Cannot open '$dict': $!";
    
    while () {
        next unless /$pattern/o;
    
        chomp;
        my $sorted = join '', sort split '', $_;
        next unless $sorted eq $scrambled_sorted;
    
                push @Unscrambled, $_;
    }
    
    close DICT or warn;
    
    
    open (MYFILE, '>>data.txt');
            foreach (@Unscrambled) {
                      print MYFILE "$_";
    }
    close (MYFILE);
    And I'm pretty new to perl..

    thanks allot!
    Last edited by helpmesos; Sep 26 '08, 01:20 PM. Reason: it just suddenly stopped working .. :/
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    If you're going to copy code off a forum at least copy it right:

    Code:
    while () {
    should be:

    Code:
    while (<DICT>) {

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Also, this:

      Code:
      #!/usr/bin/perl
      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

      Comment

      • helpmesos
        New Member
        • Sep 2008
        • 6

        #4
        ok so I modified it:

        Code:
        #!/usr/local/bin/perl
        use strict;
        use warnings;
        
        my $dict = 'wordlist.txt';
        my @scrambled=();
        
        open (FILE, "input.txt") or die "Couldn't open location file: $!";
        @scrambled = <FILE>;
        close FILE;
        
        open FILE, ">output.txt" or die $!; 
        
        foreach my $scrambled_word(@scrambled) {
        
        # Do not space-separate the word in this version.
        
        my $scrambled_length = length $scrambled_word;
        my $scrambled_sorted = join '', sort split '', $scrambled_word;
        
        my $pattern = qr{
            \A
                (?:
                    [$scrambled_word]{$scrambled_length}
                )
                \n
            \z
        }x;
        
        open DICT, '<', $dict
          or die "Cannot open '$dict': $!";
        
        while (<DICT>) {
            next unless /$pattern/o;
        
            chomp;
            my $sorted = join '', sort split '', $_;
            next unless $sorted eq $scrambled_sorted;
        
            print "$scrambled_word";
            print FILE "$_\n"; 
        }
        }
        close FILE;
        close DICT or warn;
        Still no luck, It's not printing anything into the output file.

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          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?

          Comment

          • helpmesos
            New Member
            • Sep 2008
            • 6

            #6
            Originally posted by KevinADC
            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.

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              are the words in both files all lower case or all upper case? They need to match exactly for your script to find matches.

              Comment

              • helpmesos
                New Member
                • Sep 2008
                • 6

                #8
                Originally posted by KevinADC
                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.

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  Try removing the \n from the regexp:

                  Code:
                  my $pattern = qr{
                  \A
                  (?:
                  [$scrambled_word]{$scrambled_length}
                  )
                  \n<--- remove this
                  \z
                  }x;
                  and see if that helps.

                  Comment

                  • helpmesos
                    New Member
                    • Sep 2008
                    • 6

                    #10
                    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_sort ed;

                    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..

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      Replace the "while" block with this code:

                      Code:
                      while (<DICT>) {
                          next unless /$pattern/o;
                          print "Passed regexp: $_";
                          chomp;
                          my $sorted = join '', sort split '', $_;
                          print "\$sorted = [$sorted]\n \$scrambled_sorted = [$scrambled_sorted]\n";
                          next unless $sorted eq $scrambled_sorted;
                       
                          print "$scrambled_word";
                          print FILE "$_\n"; 
                      }
                      and see what gets printed to the screen while it runs. The square brackets are there so you can see if $sorted or $scrambled_sort ed have any spaces or other characters inthem that are affecting the comparison.

                      Comment

                      • helpmesos
                        New Member
                        • Sep 2008
                        • 6

                        #12
                        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.

                        Comment

                        • KevinADC
                          Recognized Expert Specialist
                          • Jan 2007
                          • 4092

                          #13
                          Well, with no data to run I can't really help anymore.

                          Comment

                          • Icecrack
                            Recognized Expert New Member
                            • Sep 2008
                            • 174

                            #14
                            May i ask this isn't for the HackT***S*** Website is it?

                            Comment

                            Working...