Solved

ASAP - Sort a delimeted file string by date

Posted on 2003-11-05
5
410 Views
Last Modified: 2010-03-04
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!
0
Comment
Question by:JRBDEV
  • 3
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
jmcg earned 500 total points
ID: 9692422
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
 

Author Comment

by:JRBDEV
ID: 9692688
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
 

Author Comment

by:JRBDEV
ID: 9692704
oh yeah... i would like them sorted in descending order.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 9692970
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
 
LVL 20

Expert Comment

by:jmcg
ID: 9692985
(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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Syslog text file into database or into .CSV 19 137
Uploading Binary Data using Perl 5 89
perl rename 2 143
Control Number of Log Files -Perl 7 71
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question