in reply to Sorting rows in a text file
This code is by no means robust, but it should work if the data files are in _exactly_ the form you state.
Also, I am not sure whether 15/19/2001 means the 15th day of the 19th month, or vice versa. Whatever, that month is out of scale in my planet :-). So I assume dd/mm/yy
Oh, and it doesn't check the integrity of dates :-). Did I mention that.
Still, I hope the sort code can help you become familiar with how things work.
cheers#!/usr/bin/perl -w use strict; open DATA, "./data.txt"; # or wherever the file is my @data=<DATA>; my @sorted = sort sort_func @data ; for(@sorted){print $_}; sub sort_func{ my ($a_dd,$a_mm,$a_yy)=$a =~ m|(\d*)/(\d*)/(\d*)|; my ($b_dd,$b_mm,$b_yy)=$b =~ m|(\d*)/(\d*)/(\d*)|; return ( $a_yy<=>$b_yy || $a_mm<=>$b_mm || $a_dd<=>$b_dd); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Sorting rows in a text file
by maverick (Curate) on Nov 09, 2001 at 20:18 UTC |