Solved

ASAP - Sort a delimeted file string by date

Posted on 2003-11-05
5
394 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now