On Sunday, September 28, 2003, at 11:47 AM, [EMAIL PROTECTED] wrote:
Hi all,
Is there an easy way to sort 2 arrays and keep the relating indices together.
for instance I have an array of times, and an array of emails.
I would like to give the user a choice of sorting by time(numeral) or
email(alpabetical);
What about something like this (untested code):
# first, we merge the data into one array my @merge; push @merge, [$times[$_], $emails[$_]] foreach (0..$#times);
# some sort code here sort { $$a[1] cmp $$b[1] } @merge; # alphabetical by @emails # or sort { $$a[0] <=> $$b[0] } @merge; for numerical by @times
# finally, we restore the original arrays @times = map { $$_[0] } @merge; @emails = map { $$_[1] } @merge;
Hope that helps.
James
[EMAIL PROTECTED] = (); for(my$i=0;$i<@open_pages;$i++){ my$time = -M "../open_pages/$_"; push(@time, $time);
#this code is just for a readable time description,
my$lastchange ="";
if($time>1){s/\.\d*//;if($time>1){$lastchange = "$time
days";}else{$lastchange = "$time day";}}
if($time<1){$time = $time*24;if($time>
=2){if(/\.\d*/){s/\.\d*//;}$lastchange = "$time hours";}else{$time = $time*60;if(/\.\d/){s/\.\d*//}$lastchange =
"$time minutes";}}
$lastchange =~ s/\..*\d(.*)/$1/;
push(@Cemails, $Cemail);
my$row = qq{<TR><TD align="center">$Cemail</TD><TD align="center">
$lastchange</TD><TD align="center"><A class="main_LNK" href="./view_page.pl?fl=$Cemail&
st=oo">view</A></TD></TR>};
# @rows is the array I want to rearrange push(@rows,$rows}); }
# here I would like to sort @rows according to the method they chose
# but I'm not sure how to sort 2 arrays at the same time without using some
sort of grep for the indices.
[EMAIL PROTECTED] = (); if($q->param('oldest') || $q->param('newest')){ @sorted = sort($a<=>$b) @times; # change @rows here somehow with @times if($q->param('oldest))[EMAIL PROTECTED] = reverse @sorted;} }
elsif($q->param('AtoZ') || $q->param('ZtoA')){ @sorted = sort($a cmp $b) @Cemails; # change @rows here somehow with @Cemails if($q->param('ZtoA'))[EMAIL PROTECTED] reverse @rows} }
Hope this is understandable to everyone, Thanks
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]