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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with sorting recoerds by date

Status
Not open for further replies.

calabama

Programmer
Feb 19, 2001
180
0
0
US
Hello, I am trying to get this save() sub to use sortRecordByDate() so when the records are written to the database file thery are already in correct order by date.

Thanks

Tim
################### save ##########################
Code:
sub save{
    my ($self)=@_;

    my $fileName = $self->filename();
    my $delimiter= $self->delimiter();

    open ( FH, ">$fileName") || die "Unable to open $fileName. [$!] \n";
    flock FH, $LOCK_EX;
 
    print "[FileDB] Writing to File..\n" if ($self->debug()); 

    foreach my $key (sort keys%{$self->{data}}){ 

       print FH join($delimiter, @{$self->{data}{$key}}) ."\n";
    }

    flock FH, $LOCK_UN;
    close FH;
    return 1;
}
#################### sortRecordByDate ####################
Code:
sub sortRecordByDate{

    my ($hr1,$min1) = split(":",$a->[4]);
    my ($hr2,$min2) = split(":",$b->[4]);

    my $tstamp1 = timelocal(0,$min1,$hr1,$a->[2],$month2Num{$a->[1]},$a->[3] );
    my $tstamp2 = timelocal(0,$min2,$hr2,$b->[2],$month2Num{$b->[1]},$b->[3] );

    return $tstamp1 <=> $tstamp2;

}

In the begining
Let us first assume that there was nothing to begin with.
 
Tim,

What was the question again?

Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Assuming I'm following what you are trying to do, you need to add a code block for sortRecordByDate to your sort:
Code:
...
foreach my $key (sort sortRecordByDate keys %{$self->{data}} ){
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top