Gossamer Forum
Home : General : Perl Programming :

sorting values

Quote Reply
sorting values
Hi. I want to sort a list of values. Basically they are all taken from a 'Name' field in my db and put into a list box. Right now the sub that does this already puts them into alphabetical order. I would like to go one step further though. First off, I would like it to ignore anything other than letters and numbers. Just because someone starts their Name with a quotation mark it shouldn't be listed at the top. Also I would like it to ignore certain words. Mainly words like "The". Anyone know how to do this?
Quote Reply
Re: sorting values In reply to
There's a bit of redundancy in that solution that doesn't need to be there.

Just needs a little optimizing. for example:

Code:
while ($sort_name =~ /^\W/) {
$sort_name =~ s/^\W//;
}

doesn't need the while loop. Just the substitution. Why use two regexs (match then substitute)? The substition will not take place if the pattern doesnt match, so no need to check it first.

Smile

--mark
Quote Reply
Re: sorting values In reply to
The reason I did that was in case there were two non-word characters at the beginning. When I tested it, I tried

""Goldilocks and the Three Bears"

It still sorted that title above

The Brave Little Tailor

until I put in the "while" loop.

Is there a better way to do it?


------------------
JPD


PS. The reason I thought of that was something that IT wrote about a title sorting first just because someone put quotation marks there. I got to thinking that someone might put extra non-word characters just to make their name sort to the top.







[This message has been edited by JPDeni (edited April 20, 2000).]
Quote Reply
Re: sorting values In reply to
This is the sort of puzzle I enjoy. (You don't need to tell me I'm a strange person. Smile )

Assuming that all of your names are in an array called @names:

Code:
foreach $name (@names) {
$sort_name = $name;
# Remove any non-word character(s) at the beginning of the string
while ($sort_name =~ /^\W/) {
$sort_name =~ s/^\W//;
}
# Remove initial The, A or An
$sort_name =~ s/^the |^a |^an //i;
push (@sort_names,$sort_name);
}
foreach $sorted (sort { lc($a) cmp lc ($b)} @sort_names) {
foreach $name (@names) {
if ($name =~ $sorted) {
print "$name\n";
}
}
}

I went a little further than you asked, also removing "A" and "An" from the beginning of names and sorting without regard to case.

If you need help in putting this together with the select field in DBMan, just ask over in the DBMan Forum.


------------------
JPD






Quote Reply
Re: sorting values In reply to
Hi,

I need a solution (code) for opening up a text file which contains email sites like - exicte.com, hotmail.com... each on a seperate line. These are the free email sites I compiled to ban from posting. The list has gotten too big and when I input new sites, I want to make sure I am not inputing a duplicate and hence want to arrange the list alphabetically. I need a script to open up this text file and print the list alphabetically.

Thanks

[This message has been edited by socrates (edited April 20, 2000).]
Quote Reply
Re: sorting values In reply to
#!/usr/bin/perl -w

use strict;

open (FILEHANDLE, '/path/to/file.txt') or die "Can't open file: $!";
my @list = <FILEHANDLE>;
close (FILEHANDLE) or die "Can't close file: $!";

print 'Content-type: text/html\n\n';
print foreach sort @list;

--mark
Quote Reply
Re: sorting values In reply to
Mark,

Thanks a lot. I will give it a try.
Quote Reply
Re: sorting values In reply to
Back to the original post. My solution:

Code:
#!/usr/bin/perl -w


use strict;


my @names = ('Mark Badolato', 'Gordon', 'the sandman', 'Sandy', '"Ted"', 'Alex', 'abby road', 'andrea', 'tErRi', '#Zeus', '""Goldilocks and the Three Bears"', 'The Brave Little Tailor');
my %sorted_names;


foreach (@names) {
my $item = $_;
$item =~ s/^\W+//;
$item =~ s/^(the|a|an) //i;
$sorted_names{$item} = $_;
}


print "Content-type: text/html\n\n";
print "$sorted_names{$_}\n" foreach sort {lc $a cmp lc $b} keys %sorted_names;

output:

abby road
Alex
andrea
The Brave Little Tailor
""Goldilocks and the Three Bears"
Gordon
Mark Badolato
the sandman
Sandy
"Ted"
tErRi
#Zeus

--mark

[This message has been edited by Mark Badolato (edited April 21, 2000).]
Quote Reply
Re: sorting values In reply to
Ah. I see.

$item =~ s/^\W+//;

I forgot about that little + sign.

Thanks, Mark. Much tidier code. Smile


------------------
JPD






Quote Reply
Re: sorting values In reply to
Hi again,

Ok, I have a ID's which have alphabets in the beginning and in the end and numbers in between (with 0's). I want to chop of the alphabets from the beginning and the end and the zeros and return the numbers only. How to do?

ex: ak00071fg for which I want only 71 returned. I am trying to link two databses.

Thanks in advance.
Quote Reply
Re: sorting values In reply to
Assuming your string is in the variable $ID,

Code:
$ID =~ s/[a-zA-Z]|0//g;

If you might have numbers such as 20, 30, 40, etc., you'll need to use something else.

Code:
$ID =~ s/[a-zA-Z]//g;
$ID =int($ID);


------------------
JPD


[This message has been edited by JPDeni (edited April 21, 2000).]
Quote Reply
Re: sorting values In reply to
@list: Your original list. Assuming you already have it.
%stop_list: List of the words you want to exclude, such as "the".

%stoplist = ("the" => 1, "are" => 1 );
@sorted_filtered_list = sort grep{$_ !~ /[^a-zA-Z0-9]/ and !exists $stop_list{$_}}@list;

print "$_\n" for (@sorted_filtered_list);

------------------
Turk Scripts
turkiyem.com/turkscripts/
Quote Reply
Re: sorting values In reply to
Thank you very much, all. Actually, JPDeni's solution is suitbale, because it is not for a list but rather linking two Links2.0 databases. I just needed to return the LinkID to templates to link two database.

Goo Day!