Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort array by date

Status
Not open for further replies.

anyavaiableidsleft

Technical User
May 7, 2011
5
US
Have HTML files that I want to (check for dupes) and put in date order. Each record begins with <H1> and ends with <HR>, so I would open the file and split @ <HR>. There's a date section in these records - mm-dd-yy - that would determine how the records are ordered.

$a =~ /(\d{2})\-(\d{2})\-(\d{2})/;
$c = $3 . $1 . $2;

would pull out the date and make it yymmdd, which would allow numeric comparison with other dates.

What I need, if possible, is how to load the file into an array, splitting at the <HR>, then pull out each record's date, compare it to the other records in the array, delete dupes, and then reorder (entire record) by most recent on top, and print back to the original file. I'd then
exec($file) to view it with my browser.

I'm not clear as to how to load the file and then go through the array with the sort function. I'm not a programmer and last used Perl a few years ago.

Thanks in advance


 
mmm.. can you put a few example records on here?
But I'd just load it into a hash and sort when you print it. That gets rid of dupes and puts them in order.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
They're just simple html records.
Here's a sample:
"
<H1>
<CENTER>CA-01-8-11-R-1</CENTER></H1>some text here
<TABLE border=1>
<TBODY>
<TR>
<TH>some text here</TH>
<TH>p#</TH>
<TH>some text here</TH>
<TH>some text here</TH>
<TH>some text here</TH>
<TH>some text here</TH>
<TH>some text here</TH>
<TH>some text here</TH>
<TH>some text here</TH>
<TH>some text here</TH>
<TR>
<TD>some text here</TD>
etc.
</TR></TBODY></TABLE>
<P>
<CENTER><IMG src="ca-01-8-11.gif">
<P><BR>
<HR> "

and the next record follows.

The key is that I want to split these at the <HR> and then sort them as complete records, not just sort the dates.

Thanks


 
is the 01-8-11 the date?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Try this:

Code:
[COLOR=#006600]#!/usr/bin/perl -w[/color]

[COLOR=#0000FF]use[/color] strict;

[COLOR=#0000FF]local[/color] $/; [COLOR=#006600]# slurp input[/color]

[COLOR=#0000FF]my[/color] $data = <>;
[COLOR=#0000FF]my[/color] %records;
[COLOR=#0000FF]my[/color] $date;

[COLOR=#0000FF]while[/color] ($data =~ /<H1>.*?<HR>/gms) {
        [COLOR=#0000FF]my[/color] $record = $&;
        [COLOR=#0000FF]if[/color] ($record =~ /<CENTER>..-(\d+)-(\d+)-(\d+)-/) {
                $date = [COLOR=#FF0000]sprintf[/color]([COLOR=#808080]"%02d%02d%02d"[/color],$[COLOR=#FF0000]3[/color],$[COLOR=#FF0000]1[/color],$[COLOR=#FF0000]2[/color]);
        }
        $records{$date}=$record;
}

[COLOR=#0000FF]foreach[/color] $date ([COLOR=#FF0000]sort[/color] { $a <=> $b } [COLOR=#FF0000]keys[/color] %records) { [COLOR=#FF0000]print[/color] $records{$date} };

The while loop does a global match to pull out each individual record and adds it to a hash indexed by the pseudo-numeric date. The sprintf is to make sure they contain enough digits (since I notice the days of the month may be missing a digit according to your sample record).

The last line sorts it by hash indices and prints out the records.

Annihilannic.
 
Thanks. This is almost there.

It prints ONLY the earliest record (by date). (Which, in this case was the last record in the file.) None of the other records are printed. It also returns the following error:
"Use of uninitialized value in hash element at orderfh.pl line 42, <FILE> chunk 1."

line 42 is " $records{$date}=$record;"


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top