Home > Archive > PERL Beginners > June 2007 > perl sort query
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| I want to sort a hash of hash by date&time and then extract some of
the data.
>From the data ive got i can contruct a key that is yyyymmddhhmm
and i do this
##error trap absent entries
if ($endan=~m/\d{2}:\d{2}\s+\d{2}/ && $stan=~m/\d{2}:\d{2}\s+\d{2}/ )
{my %daylist;
##split start and end date and time
my ($endhour,$endmin,$endday,$endmonth,$end
year)=split(/[\ |:|\/]/,
$endan);
my($sthour,$stmin,$stday,$stmonth,$styea
r)=split(/[\ |:|\/]/,$stan);
##if its at w ends ( for instance )
my $day = (Date_DayOfW ($stmonth,$stday,$styear)
*1);
if ($day>5 )
{
my $fd="$styear$stmonth$stday$sthour$stmin";
$daylist{$fd}=\%$br;
}
foreach $key (sort (keys(%daylist)))
{
print "$key ";print $daylist{$key};print "\n";
}
print "---\n";
}
gives me output like this
for 1 Oct
---
200610011733 HASH(0x8510560)
---
200610012057 HASH(0x85b3434)
---
200610011029 HASH(0x85732a0)
---
200610010928 HASH(0x86268a4)
---
200610011220 HASH(0x86270fc)
so th print out is in the order
17:33
20:57
10:29
09:28
12:20
and my data for the 7th oct 2006
---
200610071321 HASH(0x859cfdc)
---
200610071156 HASH(0x85a17e8)
---
200610071514 HASH(0x85a2034)
---
which is
13:21 HASH(0x859cfdc)
11:56 HASH(0x85a17e8)
15:14 HASH(0x85a2034)
so its not sorted as I want
| |
| John W. Krahn 2007-06-19, 3:59 am |
| pauld wrote:
> I want to sort a hash of hash by date&time and then extract some of
> the data.
>
>
> and i do this
> ##error trap absent entries
> if ($endan=~m/\d{2}:\d{2}\s+\d{2}/ && $stan=~m/\d{2}:\d{2}\s+\d{2}/ )
> {my %daylist;
> ##split start and end date and time
> my ($endhour,$endmin,$endday,$endmonth,$end
year)=split(/[\ |:|\/]/,
> $endan);
> my($sthour,$stmin,$stday,$stmonth,$styea
r)=split(/[\ |:|\/]/,$stan);
>
> ##if its at w ends ( for instance )
> my $day = (Date_DayOfW ($stmonth,$stday,$styear)
*1);
> if ($day>5 )
> {
> my $fd="$styear$stmonth$stday$sthour$stmin";
> $daylist{$fd}=\%$br;
> }
> foreach $key (sort (keys(%daylist)))
> {
> print "$key ";print $daylist{$key};print "\n";
> }
> print "---\n";
> }
>
> gives me output like this
> for 1 Oct
> ---
> 200610011733 HASH(0x8510560)
> ---
> 200610012057 HASH(0x85b3434)
> ---
> 200610011029 HASH(0x85732a0)
> ---
> 200610010928 HASH(0x86268a4)
> ---
> 200610011220 HASH(0x86270fc)
>
> so th print out is in the order
> 17:33
> 20:57
> 10:29
> 09:28
> 12:20
>
> and my data for the 7th oct 2006
> ---
> 200610071321 HASH(0x859cfdc)
> ---
> 200610071156 HASH(0x85a17e8)
> ---
> 200610071514 HASH(0x85a2034)
> ---
> which is
>
> 13:21 HASH(0x859cfdc)
> 11:56 HASH(0x85a17e8)
> 15:14 HASH(0x85a2034)
>
> so its not sorted as I want
It sorts fine here:
$ perl -le'
print for @x = qw/200610011733 200610012057 200610011029 200610010928
200610011220/, "";
print for sort @x;
'
200610011733
200610012057
200610011029
200610010928
200610011220
200610010928
200610011029
200610011220
200610011733
200610012057
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
| |
|
|
> $ perl -le'
> print for @x = qw/200610011733 200610012057 200610011029 200610010928
> 200610011220/, "";
>
> print for sort @x;
> '
> 200610011733
> 200610012057
> 200610011029
> 200610010928
> 200610011220
>
the time is the last 4 digits (hhmm)
so the first is 17:33
then 20:57
then 10:29
then 09:28
i want them chronologically
ie
09:28
10:29
17:33
20:57
| |
| Rob Dixon 2007-06-19, 6:59 pm |
| pauld wrote:
>
> John W. Krahn wrote:
>
> the time is the last 4 digits (hhmm)
> so the first is 17:33
> then 20:57
> then 10:29
> then 09:28
>
> i want them chronologically
> ie
> 09:28
> 10:29
> 17:33
> 20:57
Your post misquoted John. This is his full response.
His program shows first the unsorted data and then the same information after
sorting. As he says, it seems to work fine.
Rob
| |
|
|
|
|
|