I am very sure the code below can be simplified. The exercise was to take a word list, rearrange each word's letter to be in alpha order (e.g. barter = aberrt). The rearranged letter would be the keys and the word be be assigned to that key. Similar keys could then have multiple words assigned to it. I've written below to go as far as splitting, alphabetizing and then creating a list of the rearranged word with the original word separated by a comma on each line I did this in steps to see what was happening at each while (learning Perl).

I was trying to create the hash in the 3rd while loop with something like this:

($temp1, $temp2) = split (/,/, $_);
$words[$temp1]= $temp2

Also, I kept count in the original loop and the last loop to make sure I ended up with the same number of words. For some reason, my 3rd loop's output never completes. I am missing words. I am guessing this is a memory issue.

#!/~/gdevars/perl -w

%words = ();
$windex = 0;
$i = 0;
$back = 0;
@alpha =();
@anagrams = ();
@keys = ();
$temp1 = 0;
$temp2 = 0;

open (LIST, '2of12inf.txt') or die "Can't open OUT file: $!\n";
open (OUTPUT2, '>Dictionary2.txt') or die "Can't open OUT file: $!\n";
open (LIST2, 'Dictionary2.txt') or die "Can't open OUT file: $!\n";
open (OUTPUT3, '>Dictionary3.txt') or die "Can't open OUT file: $!\n";
open (LIST3, 'Dictionary3.txt') or die "Can't open OUT file: $!\n";
open (OUTPUT4, '>Anagram.txt') or die "Can't open OUT file: $!\n";

while (<LIST>) {
    	chomp;                                 
	my @alpha = split(//,$_);
	my @alpha = sort @alpha;
	print OUTPUT2 "$i: @alpha : $_\n";
	$i++;
}

while (<LIST2>) {
	chomp;
	s/\s+//g;
	print OUTPUT3 "$_\n";
}

while (<LIST3>) {
	chomp;
	s/\s+/,/g;
	s/\n/,/g;
	s/:/,/g;
	print OUTPUT4 "$_\n"; 
	$i++;
}
close LIST;
close LIST2;
close LIST3;
close OUTPUT2;
close OUTPUT3;
close OUTPUT4;

Thanks to anyone for looking at this-

Recommended Answers

All 6 Replies

this creates an array, not a hash:

($temp1, $temp2) = split (/,/, $_);
$words[$temp1]= $temp2

actually unless $temp1 is a number it should not create anything.

This would create a hash:

($temp1, $temp2) = split (/,/, $_);
$words{$temp1}= $temp2

Your code used [] (array indices) instead of {} (hash keys).

KevinADC,
Thanks for responding. I thought no one looked at this as I never received a notification. I'll be taking another look at this. Thanks again.

Try to get in the habit of putting

use strict;
use warnings;

at the start of your scripts and it will save you a lot of frustration and time later on. Also, if you ever post to comp.lang.perl.misc, you will get flamed or ignored if you don't do this.

Also check result codes on any explicitly closed files.

commented: very good point +3

Thanks for the advice. I do run the scripts using -w at the command prompt for warnings. I'm still trying to get a handle on 'use strict' and how variables are defined. I know I would use 'my' to help with this but when I start reviewing some of the loops, I end up getting initializing errors.

the warnings pragma:

use warnings;

is better than the -w switch. It allows you to do this:

no warnings;

within a block of code that might cause warnings but you want the perl script to ignore them. Once you turn on the -w switch there is no way to turn it off. And it affects all modules that your program might use as well. The warnings pragma does not, it is only scoped to the program that loads it.

The use of 'strict' controls the scope of your variables. It forces you to practice good programming by not using global variables, and to declare all variables before use. There are some situations where you may want to turn this off in a block of code, and this can be done similarly to the 'no warnings' pragma.

When you use 'my $variable', you are telling Perl to use this version of the variable (in the current block or file) and not to confuse it with another variable of the same name, but outside the block. Generally you try to declare / initialize your variables as close to the point in your code where you begin to use them. So you could have several subroutines with a variable named '$filename', but if you 'use strict' at the beginning of your script, and then use 'my $filename' in each subroutine, they will all be treated as different variables.

Search the documentation for both pragmas - 'strict' and 'warnings'.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.