Anthony Akens wrote: > I'm attempting to use the following code to read a file > in the format of: > > directory name:displayname > > I want to sort the list by the "displayname", looking > at the first letter for each display name in the file. > If it's unique, I want to print it. This should result > in an alphabetical list of the letters for the display > names. However, the sort is not working quite right, > and I sometimes get letters out of order, missing, or > some that aren't even in the list. > > Can anyone tell me what I've done wrong? > > Tony > > --------------------------- > open (NAMES, $namefile) > or die "Could not open $namefile $!"; > > while(<NAMES>) > { > ($key, $value) = split /:/; > chomp $value; > $Names{$key} = $value; > } > close NAMES; > > foreach $dirname (sort { "\L$a" cmp "\L$b" } keys %Names){ > $displayname = $Names{$dirname}; > $item = substr($displayname, 0, 1); > push (@alpha, "\U$item") unless $seen{"\L$item"}++; > } > > print "<P>"; > > foreach $letter(@alpha) { > print "<A HREF=\"\#$letter\">$letter<\/a> "; > } > ----------------------------
if you want to sort by displayname, those are the values of the hash, not the keys. you might want to try something like: #!/usr/bin/perl -w use strict; my %Names; my %Seen; open(NAMES,$nameFile) || die $!; while(<NAMES>){ chmop; my($key,$value) = split(/:/); $Names{$key} = $value; } close(NAMES); foreach my $value (sort {lc $a cmp lc $b} values %Names){ next unless($value =~ /(.)/ && !$seen{lc $1}++); print "<A HREF=\"#\U$1\">$1</A>\n"; } __END__ untested. see if the above works better. david -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]