develooper Front page | perl.beginners | Postings from April 2002

RE: Quick question on Sorting

Thread Previous | Thread Next
From:
Wagner-David
Date:
April 29, 2002 10:11
Subject:
RE: Quick question on Sorting
Message ID:
113A7A0D1F47D511B92E00D0B7E03DAB0187BDBC@pmail02.vikingfreight.com
I modified your sort to be like:
@info = sort { $days{$a->[1]} <=> $days{$b->[1]} || $a->[2] =~ /^\d+/ <=>
$b->[2] =~ /^\d+/ || $a->[2] =~ /:\d+/ <=> $b->[2] =~ /:\d+/ } @info;


    I then reversed all your data within the simple script and this is the
output:

ccc  gamma     sun  3:00
aaa  alpha     mon  1:00
eee  epsilon   mon  3:00
zzz  zeta      mon  4:00
ddd  delta     tue  1:00
iiii ita       tue  1:30
bbb  beta      wed  2:00

    Depending on the data, you may want to verify the data as it comes
in(ie, make sure valid day and right case, numeric in the time, etc).

Wags ;)

-----Original Message-----
From: james poni [mailto:jamespoini@hotmail.com]
Sent: Monday, April 29, 2002 05:35
To: beginners@perl.org; krahnj@acm.org; beginners-get.123_145@perl.org
Subject: Quick question on Sorting




Hello again (final time)

I also want to sort the  (number time) field when we have the same day
eg:

below:
ccc gamma sun 3:00
aaa aplha mon 1:00   <<here the days are sorted as
eee epsilon mon 3:00  <<as well as the time
zzz zeta mon 4:00      << is sorted if the same day
ddd delta tue 1:00  <<
iii ita tue 1:30    << this is greater
bbb beta wed 2:00

How would i modify this program in order to do so ? Do i need a time hash
eg
time = (12:00 =>1,12:30=>2,13:00=>3,13:30=>4 ......) ?


THIS THE PROGRAM
$line1 = "aaa alpha\nbbb beta\nccc gamma\nddd  delta\neee epsilon\nzzz
zeta\niiii ita\n";
$line2 = "mon\nwed\nsun\ntue\nmon\nmon\ntue\n";
$line3 = "1:00\n2:00\n3:00\n1:00\n3:00\n4:00\n1:30\n";

open ( ALPH, ">pl.txt" ) || die "Cant open\n";

print ALPH $line1;

close ALPH;

open (AP, "<pl.txt" )  || die "Cant open\n";

$info1 = <AP>;

close AP;



open ( DAY, ">da.txt" ) || die "Cant open\n";

print DAY $line2;

close DAY;

open (DA, "<da.txt" )  || die "Cant open\n";

@info2 = <DA>;

close DA;


open ( NUM, ">numb.txt" ) || die "Cant open\n";

print NUM $line3;

close NUM;

open ( NU, "<numb.txt" ) || die "Cant open\n";

@info3 = <NU>;

close NU;

my @info;
my %days = (
   sun => 0,
   mon => 1,
   tue => 2,
   wed => 3,
   thu => 4,
   fri => 5,
   sat => 6,
   );



my $count;
chomp, push @{$info[$count++]}, $_ for @info1;
$count = 0;
chomp, push @{$info[$count++]}, $_ for @info2;
$count = 0;
chomp, push @{$info[$count++]}, $_ for @info3;

@info = sort { $days{$a->[1]} <=> $days{$b->[1]}  } @info;

I think i need another sort for time here but im not entirely sure how this
sort above sort works?

for my $row ( @info ) {
  print "@$row\n";
}


Thanks
James


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp.


-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About