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.
Subject Author Views Date
Thread Sort by the number Pasha1 6248 Aug 21, 1999, 12:43 PM
Post Re: Sort by the number
Pasha1 6108 Aug 21, 1999, 7:45 PM
Post Re: Sort by the number
Bobsie 6108 Aug 22, 1999, 2:35 PM
Post Re: Sort by the number
Pasha1 6112 Aug 22, 1999, 7:39 PM
Post Re: Sort by the number
Bobsie 6104 Aug 23, 1999, 3:51 AM
Post Re: Sort by the number
Pasha1 6096 Aug 23, 1999, 8:56 PM
Post Re: Sort by the number
Bobsie 6093 Aug 24, 1999, 10:22 AM
Post Re: Sort by the number
Pasha1 6100 Aug 24, 1999, 12:56 PM
Post Re: Sort by the number
polpus 6149 Aug 24, 1999, 7:17 PM
Post Re: Sort by the number
Pasha1 6108 Aug 25, 1999, 4:17 AM