Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ASAP - Sort a delimeted file string by date

Posted on 2003-11-05
5
Medium Priority
?
429 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 1500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

772 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