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: -
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!
13 6626 KevinADC 4,059
Recognized Expert Specialist
If you're going to copy code off a forum at least copy it right:
should be: numberwhun 3,509
Recognized Expert Moderator Specialist
Also, this:
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
ok so I modified it: - #!/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.
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?
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.
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.
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.
KevinADC 4,059
Recognized Expert Specialist
Try removing the \n from the regexp: -
my $pattern = qr{
-
\A
-
(?:
-
[$scrambled_word]{$scrambled_length}
-
)
-
\n<--- remove this
-
\z
-
}x;
-
and see if that helps.
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..
KevinADC 4,059
Recognized Expert Specialist
Replace the "while" block with this 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_sorted have any spaces or other characters inthem that are affecting the comparison.
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.
KevinADC 4,059
Recognized Expert Specialist
Well, with no data to run I can't really help anymore.
Icecrack 174
Recognized Expert New Member
May i ask this isn't for the HackT***S*** Website is it?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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--*...
|
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...
|
by: Benny Ng |
last post by:
Hi,All,
Export Method:
-------------------------------------------------------------------------
strFileNameExport = "Results"
Response.Clear()
Response.Buffer = True...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |