Gossamer Forum
Home : General : Perl Programming :

Display database field in alphabetical order

Quote Reply
Display database field in alphabetical order
I've managed to tweak a script to get it to parse a database to see if one field contains a given word (say, the country France) and, where there's a match, to print out a different field (say, the user's name). The code is below.

What I'd love to figure out is to then sort the list of users' names alphabetically? Does anyone have a piece of script I could borrow (and try to learn from).

A side thought: if one user has two records, both of which include the keyword of France, is there a way of displaying the user's name just the once?

TIA,

Andy

* * *

&open_file("FILE1","","$basedir$filename");

$line = &read_file("FILE1");
$counter = 0;

while (($line = &read_file("FILE1")) && ($counter < 120)) {
# split the fields at the | character
@tabledata = split(/\s*\|\s*/,$line,$fields);

#see if the category allocated field matches the one called for in the querystring
if ($tabledata[4] =~ /^$category/i) {

#see if the market field also includes the one called for in the querystring
@words = split(/\s*\,\s*/,$tabledata[14]);
foreach $aword (@words) {
if ($query =~ /\b$aword/i) {

# print list of directory entry titles from the records -- table definition not included in this posting to save space

print "<TR valign=top><TD class=bodyfull ALIGN=center>$tabledata[2]</TD></TR>\n";

$counter++;

}

}

}

}

[This message has been edited by Andy (edited November 07, 1999).]
Quote Reply
Re: Display database field in alphabetical order In reply to
Change:
foreach aword(@words) {

to:
foreach aword(sort(@words)) {

You could stack a list or delimitered line with each username found and skip any that are matched in the list already, then do your db lookup using that list of user names.
-- Gordon --


------------------
$blah='82:84:70:77';
print chr($_) foreach (split/:/,$blah);
Quote Reply
Re: Display database field in alphabetical order In reply to
Thanks, Gordon, the basic listing in alphabetical order is working fine.

I'm stumbling a bit on the next step of stacking the selected fields and listing these once only in alphabetical order.

I'd like to try to get to this, but am stuck even on an intermediate step of listing the search field contents in alphabetical order.

I'm using this on a test basis:

@words = split(/\s*\,\s*/,$tabledata[14]);
foreach $aword(sort(@words)) {
# print list of markets in alphabetical order
print "$aword(sort(@words))<BR>\n";
}

I see what's happening, but don't follow how to stack the $aword results and display them just once in alpha order.

Hoping that you might be able to help me on this internediate step and then that I can follow-through to my end goal.

TIA,

Andy

[This message has been edited by Andy (edited November 09, 1999).]
Quote Reply
Re: Display database field in alphabetical order In reply to
Maybe something like this:

Code:
foreach $aword (sort(@words)) {
push @filtered, $aword if ($dlist !~ /(^|\|)${aword}\|/);
$dlist .= $aword . "|";
}
print "$_\n" foreach (@filtered);
TMTOWTDI

(had to change the delimiter i was using so the regex wasn't smiling at you;p )
-- Gordon --


------------------
$blah='82:84:70:77';
print chr($_) foreach (split/:/,$blah);

[This message has been edited by GClemmons (edited November 09, 1999).]
Quote Reply
Re: Display database field in alphabetical order In reply to
Gordon, thanks again for your help and prompt reply.

I've tested my script with your suggested addition -- and reworked bits of my script which seemed to conflict. By adding various print lines to test what's happening, I've proved the script is running OK -- except I've run it without the first test of whether the category in the querystring matches the one in the database record (since this seemed to be problematical). Also, the final printing of the filtered list was causing a "no data" error, so I rearranged it and that stopped the error.

While there's no error now, all I get is printing of my test lines (omitted below to save space) and no list of words -- not even $dlist when I set that to be printed.

Have I still got a conflict? (I think) I follow the code, except I can't get my head around the "push @filtered" line. Is the problem to do with the delimiters? The database itself uses "|" to split the fields. Field 14 includes the "keywords" (a list of country names -- usually 1 or 2 words) with items in the list separated by commas.

I'd really appreciate you pointing out where the trouble is.

TIA,

Andy

====

code:

# split the database fields at the | character
@tabledata = split(/\s*\|\s*/,$line,$fields);

# see if the category in the database matches
# the one called for in the querystring
if ($tabledata[4] =~ /^$category/i) {
# identify the individual words or phrases
# which are split with a "," in field 14
@words = split(/\s*\,\s*/,$tabledata[14]);
}

# run through the list of words to filter out
# repeats and to sort in alphabetical order

foreach $aword (sort(@words)) {
push @filtered, $aword if ($dlist !~ /(^|\|)${aword}\|/);
$dlist .= $aword . "|";
}

# print the filtered list of words
foreach (@filtered) {
print "$_<BR>\n";
}
Quote Reply
Re: Display database field in alphabetical order In reply to
seems like your split might be returning nothing. have it print out the values of @tabledata and @words as it goes and see what you get.
also, what is an example of $line.

-- Gordon --


------------------
$blah='82:84:70:77';
print chr($_) foreach (split/:/,$blah);
Quote Reply
Re: Display database field in alphabetical order In reply to
Ah! Some progress.

I'd obviously printed out all variables except the critical ones you've picked out! Acting on your suggestions, it's clear that my file_read was failing to loop around and run thru all records -- it was only looking at the first record in the database!

$line is defined as &read_file("FILE1"); (a defined subroutine, basically no more than read, but allowing for an error message if the database can't be accessed). In the database, each record or line comprises UserID, Company name, Category, etc. -- and the fateful Geographic markets at field 14!

I've now sorted out the looping problem and -- surprise, surprise -- got some output in the form of a list of countries WITHOUT using the extra test print lines! No country name appears more than once in the results (although many occur several times across the database).

Alphabetical sorting works partially. The first 19 are not sorted, then 33 are sorted perfectly, then 17 are not. There seems to be no clear pattern as to why.

I've tried a few variants to try to sort @filtered just prior to printing, but my numerous guesses at the right coding haven't succeeded. Could you please walk me through this last step?

Thanks for your earlier suggestions. I'm pleased that I'm learning (slowly) and almost got there on this one!

Many thanks,

Andy

[This message has been edited by Andy (edited November 10, 1999).]
Quote Reply
Re: Display database field in alphabetical order In reply to
Check for capitalization. it sorts caps first and then lower-case. you might try switching:

push @filtered, $aword if ($dlist !~ /(^|\|)${aword}\|/);

to:
push @filtered, ucfirst($aword) if ($dlist !~ /(^|\|)${aword}\|/);

if you wanted to convert all of the countries to a universal format.
-- Gordon --



------------------
$blah='82:84:70:77';
print chr($_) foreach (split/:/,$blah);


[This message has been edited by GClemmons (edited November 10, 1999).]
Quote Reply
Re: Display database field in alphabetical order In reply to
Thanks for this -- it'll be very useful once users post their info and don't capitalize. But the database was put together by us and all country names are capitalized.

Which made me think again and take another look at the database. And now I realise that those listed alphabetically in the script results are ... listed alphabetically in the database. Is it that the script only sorts the multiple words appearing in one field, but doesn't sort the @filtered list? Is there a way to achieve this?

Thanks again,

Andy



[This message has been edited by Andy (edited November 10, 1999).]
Quote Reply
Re: Display database field in alphabetical order In reply to
Use sort on your final array of strings.
If you are simply wanting to print the string in order just use

print "$_\n" foreach (sort(@filtered));

if @filtered is the final group of data.
(Sort works tried and true if the data is consistant)

-- Gordon --

------------------
$blah='82:84:70:77';
print chr($_) foreach (split/:/,$blah);
Quote Reply
Re: Display database field in alphabetical order In reply to
Brilliant!

All is working just fine. Thanks, Gordon, you've been a tremendous help.

Andy