Click to See Complete Forum and Search --> : weird perl sort function


syn
07-28-2001, 03:02 PM
Hi all, can anyone help me out? Ive got a script Im writing to join wordlists together, lowercase everything, sort in alphabetical order, then eleminate duplicate words (all but one of course) and then write to a out.txt file, (its not finished yet, i havnt implemented the duplicate words part but Im damn close) the problem Im having is that the sort function isnt working right with my script for some reason, its probably because I dont fully understand regular expressions but still Im stumped on what is causing the problem. If I write my own array
@something = qw(apple orange grape bananna);
and then @something = sort @something; it works great printing out all the words in the array in alphabetical order but for some reason it doesnt work in my script. I dont want anyone to do it for me since this is a great learning experience for me, just a push in the right direction :) thanx a million for anyone who helps
ps heres the nasty script got me pulling out my hair:
#!/usr/bin/perl
#
# Dictionary Helper Application Script
#
# first see what we have in the current directory and read in all files

# trap so we dont cream any already created wordlists
$name = "out.txt";
if (-e $name) {
print "Out.txt exists, move or rename it."
#exit 1; # this doesnt compile right for some reason
}

print "Reading in some files\n";
open(FILE,"1.txt") ||
die "I cant read: $!";
@wordlist1 = <FILE>;
close (FILE);

open(FILE,"2.txt") ||
die "I cant read: $!";
@wordlist2 = <FILE>;
close (FILE);

#join files into a single array
print "Joining files\n";
@wordlist1 =(@wordlist1, @wordlist2);


#now we should go through and remove caps

print "Converting to lowercase\n";

@wordlist1 = ("\L@wordlist1\n");
print @wordlist1;

print "Alphebetizing wordlist\n";

@wordlist1 = (sort @wordlist1);

print @wordlist1; #this doesnt sort for some reason

takshaka
07-28-2001, 05:25 PM
Originally posted by syn:
@wordlist1 = ("\L@wordlist1\n");

@wordlist1 now contains one entry, a space-separated list of the original array entries (in lowercase, though, and followed by a newline).

That is, the following produce equivalent results:
$scalar = "@array";
$scalar = join $", @array;
($" is the list-separator variable, set to a single space by default)

You want to perform the lowercase for each element of the array individually instead. I'm sure Perl has a way to do that.

syn
07-28-2001, 09:59 PM
cool thanx for the tip, Im sure I just have to create a loop to step through all the array elements, I hope it works out, wish me luck! :)

TheLinuxDuck
07-29-2001, 02:49 PM
Syn:

There are two things I will suggest to you just in general perl style..

1. Always use -w on the shebang line. -w give you better warning messages, to help you really get a handle on what errors are occuring. (#!/usr/bin/perl -w)
2. Always use 'use strict;'. This forces the coder to follow a few procedural steps, but makes the code a little cleaner, and helps the coders keep track of variable use, etc. Using this requires that all variables, before they can be used, be declared with 'my'. (such as my($temp); or my($firstFile)="/tmp/tempfile" ;)

One common error with coding is to forget semicolons at the ends of lines. I do it all the time. Sometimes, errors are merely a matter of a forgotten semicolon, or in some cases, a print that is missing a quote. (I'm notorious for that one, too)

Good luck with the coding!

YaRness
07-31-2001, 08:38 AM
look into the lc() command for lowercase.

hashes can be used to eliminate duplicates, though it's not the most conservative approach.