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?
Apr 19, 2000, 10:55 PM
Veteran (1509 posts)
Apr 19, 2000, 10:55 PM
Post #2 of 13
Views: 5898
There's a bit of redundancy in that solution that doesn't need to be there.
Just needs a little optimizing. for example:
$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.
--mark
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.
--mark
Apr 19, 2000, 11:29 PM
Veteran (8669 posts)
Apr 19, 2000, 11:29 PM
Post #3 of 13
Views: 5893
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).]
""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).]
Apr 20, 2000, 9:11 AM
Veteran (8669 posts)
Apr 20, 2000, 9:11 AM
Post #4 of 13
Views: 5898
This is the sort of puzzle I enjoy. (You don't need to tell me I'm a strange person. )
Assuming that all of your names are in an array called @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
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
Apr 20, 2000, 2:08 PM
Enthusiast (832 posts)
Apr 20, 2000, 2:08 PM
Post #5 of 13
Views: 5880
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).]
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).]
Apr 21, 2000, 10:01 AM
Veteran (1509 posts)
Apr 21, 2000, 10:01 AM
Post #8 of 13
Views: 5909
Back to the original post. My solution:
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).]
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).]
Apr 21, 2000, 7:36 PM
Enthusiast (832 posts)
Apr 21, 2000, 7:36 PM
Post #10 of 13
Views: 5890
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.
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.
Apr 21, 2000, 8:33 PM
Veteran (8669 posts)
Apr 21, 2000, 8:33 PM
Post #11 of 13
Views: 6026
Assuming your string is in the variable $ID,
If you might have numbers such as 20, 30, 40, etc., you'll need to use something else.
$ID =int($ID);
------------------
JPD
[This message has been edited by JPDeni (edited April 21, 2000).]
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).]
Apr 22, 2000, 12:37 PM
Novice (18 posts)
Apr 22, 2000, 12:37 PM
Post #12 of 13
Views: 5902
@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/
%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/