473,491 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

unscramble words and write to file (have code)

6 New Member
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
13 6626
KevinADC
4,059 Recognized Expert Specialist
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
3,509 Recognized Expert Moderator Specialist
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
helpmesos
6 New Member
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
4,059 Recognized Expert Specialist
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
helpmesos
6 New Member
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
4,059 Recognized Expert Specialist
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
helpmesos
6 New Member
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
4,059 Recognized Expert Specialist
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
helpmesos
6 New Member
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
4,059 Recognized Expert Specialist
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
helpmesos
6 New Member
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
4,059 Recognized Expert Specialist
Well, with no data to run I can't really help anymore.
Sep 28 '08 #13
Icecrack
174 Recognized Expert New Member
May i ask this isn't for the HackT***S*** Website is it?
Sep 28 '08 #14

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

Similar topics

4
18279
by: qwweeeit | last post by:
The standard split() can use only one delimiter. To split a text file into words you need multiple delimiters like blank, punctuation, math signs (+-*/), parenteses and so on. I didn't...
12
2168
by: teoryn | last post by:
I've been spending today learning python and as an exercise I've ported a program I wrote in java that unscrambles a word. Before describing the problem, here's the code: *--beginning of file--*...
12
12504
by: dough | last post by:
I want to read in lines from a file and then seperate the words so i can do a process on each of the words. Say the text file "readme.txt" contains the following: In the face of criticism from...
1
3245
by: Benny Ng | last post by:
Hi,All, Export Method: ------------------------------------------------------------------------- strFileNameExport = "Results" Response.Clear() Response.Buffer = True...
3
2942
by: Csaba Gabor | last post by:
I'm comparing the text of (snippets of) web pages which I expect to be quite different or quite similar. In the case where they are similar, I would like to display the more recent one and say...
20
5094
by: dmurray14 | last post by:
Hey guys, I'm a C++ newbie here - I've messed with VB, but I mostly stick to web languages, so I find C++ to be very confusing at times. Basically, I am trying to import a text file, but I want...
4
8419
by: bigbagy | last post by:
Notes The programs will be compiled and tested on the machine which runs the Linux operating system. V3.4 of the GNU C/C++ compiler (gcc ,g++) must be used. A significant amount coding is...
11
11796
by: elrondrules | last post by:
Hi Am pretty new to python and hence this question.. I have file with an output of a process. I need to search this file one line at a time and my pattern is that I am looking for the lines...
2
2214
by: Quentin | last post by:
I have a text file with lines that I would like to remove above a certain word and below another word. Example: 1231231231234123423434 2341234123412341242141 234512353764316236234...
0
7112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.