Solved

ASAP - Sort a delimeted file string by date

Posted on 2003-11-05
5
423 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to extract data from a table into a fixed length file 10 64
Rename file based on contents in the file? 3 107
rename outfile before writing 2 77
File Find regex problem 4 70
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

751 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