Ask a Question related to PERL Beginners, Design and Development.
-
Motherofperls@aol.com #1
How to sort 2 arrays and keep indices ordered?
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);
my@rows = ();
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.
my@sorted = ();
if($q->param('oldest') || $q->param('newest')){
@sorted = sort($a<=>$b) @times;
# change @rows here somehow with @times
if($q->param('oldest)){@sorted = 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')){@rows= reverse @rows}
}
Hope this is understandable to everyone,
Thanks
Motherofperls@aol.com Guest
-
#40224 [NEW]: Could PHP leave superglobal array indices alone?
From: joem at tempomg dot com Operating system: redhat linux PHP version: 5.2.0 PHP Bug Type: Feature/Change Request Bug... -
Sort a hash based on values in the hash stored as arrays of hashes
Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {... -
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB.
Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. hi, guys i have asp... -
Sort Multiple Arrays from MySQL DB
Hi, I have the following code: <PRE> <?php $username = "####t"; $password = "####"; $hostname = "####"; mysql_connect($hostname,... -
#23604 [Ver->Bgs]: Problem with negative indices
ID: 23604 Updated by: iliaa@php.net Reported By: nicolas at van-lancker dot be -Status: Verified +Status: ... -
James Edward Gray II #2
Re: How to sort 2 arrays and keep indices ordered?
On Sunday, September 28, 2003, at 11:47 AM, [email]Motherofperls@aol.com[/email]
wrote:
What about something like this (untested code):> 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);
# 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
> my@rows = ();
> 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.
>
> my@sorted = ();
> if($q->param('oldest') || $q->param('newest')){
> @sorted = sort($a<=>$b) @times;
> # change @rows here somehow with @times
> if($q->param('oldest)){@sorted = 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')){@rows= reverse @rows}
> }
>
>
>
> Hope this is understandable to everyone,
> ThanksJames Edward Gray II Guest
-
John W. Krahn #3
Re: How to sort 2 arrays and keep indices ordered?
[email]Motherofperls@aol.com[/email] wrote:
Hello,>
> Hi all,
I would use an array of arrays to keep the two fields synced.> 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);
# Array of Arrays
@rows = ( [ email1 => time1 ],
[ email2 => time2 ],
...
[ emailn => timen ],
);
# Sorting an Array of Arrays
my @sorted;
if ( $q->param( 'oldest' ) or $q->param( 'newest' ) ) {
@sorted = sort { $a->[ 1 ] <=> $b->[ 1 ] } @rows;
if ( $q->param( 'oldest' ) ) {
@sorted = reverse @sorted;
}
}
elsif ( $q->param( 'AtoZ' ) or $q->param( 'ZtoA' ) ) {
@sorted = sort { $a->[ 0 ] cmp $b->[ 0 ] } @rows;
if ( $q->param( 'ZtoA' ) ) {
@sorted = reverse @sorted;
}
}
John
--
use Perl;
program
fulfillment
John W. Krahn Guest



Reply With Quote

