Gossamer Forum
Home : General : Perl Programming :

Sort by the number

Quote Reply
Sort by the number
I have a little problem here. Well, not very little Smile

I have a directory of files with the data about people. The structure of each file is:
Code:
Name|Social Security Number|Other Data|Other Data|Other Data

Here're the samples of five files:
Code:
John|384750295|Other Data|Other Data|Other Data

Anna|456328172|Other Data|Other Data|Other Data

Peter|754839273|Other Data|Other Data|Other Data

Alex|643728923|Other Data|Other Data|Other Data

Ted|367898766|Other Data|Other Data|Other Data

What I need is to read the directory for all text files with these data. Sort all data in the files starting with the highest SS Number, and print all sorted stuff to the screen. Like if with sample data files above the output should look like this:

Code:
Peter - 754839273 - Other Data - Other Data - Other Data
Alex - 643728923 - Other Data - Other Data - Other Data
Anna - 456328172 - Other Data - Other Data - Other Data
John - 384750295 - Other Data - Other Data - Other Data
Ted - 367898766 - Other Data - Other Data - Other Data

Here's what I came up with so far:
Code:
$ss_dir = "/home/private/ss/"; # Social Security directory.
$number_to_show = "20"; # The default number of SS to show.
$use_flock = "1"; # File locking? (0=No, 1=Yes)

opendir(SSDIR, "$ss_dir");
@all_ss = grep(/\.txt$/,readdir(SSDIR));
closedir(SSDIR);
foreach $one_ss (@all_ss)
{
open (SS,"$ss_dir$one_ss");
if ($use_flock == "1")
{
flock SS, 2;
}
@entries = <SS>;
foreach $line (@entries)
{
@fields = split(/\|/,$line);
if ($z<$number_to_show)
{
$z++;
# Do the sorting from highest SS number to lowest.
# Print the sorted stuff to the screen.
}
}
close (SS);
}

The only thing I can't figure out is how to sort the data.

Please, help me out here.

Thank you.
Quote Reply
Re: Sort by the number In reply to

Anybody?
Please help!
Quote Reply
Re: Sort by the number In reply to
See if my reply at http://www.gossamer-threads.com/...um8/HTML/000526.html helps any.
Quote Reply
Re: Sort by the number In reply to
Okay Bobsie,
But I still didn't get how to sort my database. I would really appreciate it, if you could give me some help here.

Thank you in advance.
Quote Reply
Re: Sort by the number In reply to
I'll have to take another look at your problem when I am fully awake. I've been going strong, catching up on all the posts, since 1 p.m. yesterday and it is now 6:55 a.m. today (but not on Alex's server, apparently Smile)

[This message has been edited by Bobsie (edited August 23, 1999).]
Quote Reply
Re: Sort by the number In reply to
Bobsie?

Any one?
Quote Reply
Re: Sort by the number In reply to
Like you, sorting is not my strong point. I am studying the code and do not see how to sort the data the way the script is written. This part of the code is what is has me stumped:

Code:
foreach $line (@entries) {
@fields = split(/\|/,$line);
if ($z<$number_to_show) {
$z++;
# Do the sorting from highest SS number to lowest.
# Print the sorted stuff to the screen.
}
}

Since you are working only with one line at a time, I do not see how you can sort it for printing since you do not know what the other $lines are going to contain until you have them split.

If the files being read in from the directory were named based on the field you want to sort on, then you could sort them right up front. For example, 754839273.txt. Then, your line:

Code:
foreach $one_ss (@all_ss) {

could read:

Code:
foreach $one_ss ( sort {$b cmp $a} @all_ss) {

Of course, this might also work:

Code:
foreach $line (sort {$b cmp $a} @entries) {

The problem with the last one is, the entire @entries is sorted, not just the SS number.

I hope this helps.

[This message has been edited by Bobsie (edited August 24, 1999).]
Quote Reply
Re: Sort by the number In reply to
Well, I wrote this script this way, because I got a directory of ASCII files with people's information. Over 100 files, and growing.

The files are numbered like:
1.txt
2.txt
3.txt
4.txt
5.txt
etc...
50.txt
etc...
114.txt
etc...
etc...

The structure of each file is just one line:
Name|Social Security Number|Other Data|Other Data|Other Data

My target output is to print on the screen the table of 20 people starting from the highest Social Security number.

What I need is to read this directory, then read each file, then sort, and then print sorted Social Security numbers.

I got everything work (I think) except for this sorting thingy, which I just can't figure out.

I hope you understand it Smile
Quote Reply
Re: Sort by the number In reply to
Ok, I'm not from the U.S, so I'll assume that all security numbers are 9 digits long.
Here's the code:

Code:
$ss_dir = "/home/private/ss/";
$number_to_show = "20";
$use_flock = "1";
opendir(SSDIR, "$ss_dir");
@all_ss = grep(/\.txt$/,readdir(SSDIR));
closedir(SSDIR);

for (@all_ss) {
open (SS,"$ss_dir$_");
if ($use_flock == 1) {
flock(SS, 2);
}
while (<SS> ) {
@fields = split(/\|/);
push @rearranged, "$fields[1]|$fields[0]|$fields[2]|$fields[3]|$fields[4]";
}
close SS;
}
@sorted = sort {$b cmp $a} @rearranged;
$y = 0;
for (@sorted) {
last if $y == $number_to_show;
(@nowfields) = split(/\|/);
print "$nowfields[1] - $nowfields[0] - $nowfields[2] - $nowfields[3] - $nowfields[4]<br>\n";
$y++;
}

------------------
www.UBBdir.com
Have you seen Links 2.0 looking just like UBB? UBBdir.com does!
Quote Reply
Re: Sort by the number In reply to
It works great.
http://www.cellwarp.com/ss/test.cgi

Thank you guys!