ASAP - Sort a delimeted file string by date

I have a file that contains url hit information.  the stucture is as follows:

Url  |  Date  |  Hits Today  |  Total Hits

www.berard.cc/work|11/04/2003|3|1000
www.berard.cc|11/06/2003|3|1000
www.teamlhhc.com |11/05/2003|3|2345

I need to sort these lines by date.  Seperatly i also need to sort them by total hits.

After sorting theses i will need to loop through them and split them to display the output of the line to the browser.

here's what im doing... http://www.berard.cc/cgi-bin/hit_counter.cgi?VIEW

Thanks for any help!
JRBDEVAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jmcgOwnerCommented:
Your current output looks pretty good. What problem are you having?

Just to get the discussion started, here's a date sort that might work for your data. I'll assume your data starts out as an array @unsorted of the lines in your data file, trimmed of headers.

my @sorted = map {$_->[1]}
     sort { $b->[0] <=> $a->[0] }
          map {  m{(\d{1,2})/(\d{1,2})/(\d{4})};
                [sprintf( "%04d%02d%02d", $3, $1, $2), $_ ]
              } @unsorted;

print @sorted;

This looks complicated, but it is following a standard Perl idiom for efficient sorts called a "Schwartzian transform".

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JRBDEVAuthor Commented:
OK.  I have changed the format of the date field to include the time.

my file now looks like this.  I have no header in my file.


www.berard.cc/work|20031104231500|3|1000
www.berard.cc|20031104231505|3|1000
www.teamlhhc.com |20031104231510|3|2345

i want to sort the input array @lines by the date field then display the results.

then i want to sort the input array @lines by the LAST field then display the results.

Im supr close to being done with this project and this is the last thing.  Thank you for your help so far!  It is greatly apprecieated.
0
JRBDEVAuthor Commented:
oh yeah... i would like them sorted in descending order.
0
jmcgOwnerCommented:
Yes, I had gathered from your example that you wanted descending order. That's why the $b term is on the left side of the comparison expression and the $a term is on the right.

==========

Let's prepare the @lines array just once for the two sorts:

my $counter = 0;
my @unsorted = map { [ (split /\|/)[1,3], $counter++] } @lines;
my @dateorder = map { $_->[2]} sort { $b->[0] cmp $a->[0] } @unsorted;
my @hitsorder = map { $_->[2]} sort {$b->[1] <=> $a->[1] } @unsorted;

The @dateorder and @hitsorder arrays now contain a list of the index values of @lines in the respective order in which they should appear.

To print out the table rows in date order, you could do something like (raw HTML):

my $counter = 0;
for (@dateorder) {
      printf "<tr><td>%s</td><td>%s</td><td>%d</td><td>%d</td></tr>\n",
                split (/\|/, $lines[$_]);
      last if ++$counter >= 50;
   }
0
jmcgOwnerCommented:
(A tough grader and impatient. I'd much prefer that you leave the question open until you have an answer you're completely satisfied with.)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.