Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Newbie question sorting (http://www.velocityreviews.com/forums/t890928-newbie-question-sorting.html)

mdfoster44@netscape.net 02-18-2005 06:47 PM

Newbie question sorting
 
Hi

Could someone please point me to some documentation on how to sort
arrays such as these?

a[0][0]= ANA a[0][1]= 1
a[1][0]= MFI a[1][1]= 12
a[2][0]= ABW a[2][1]= 1
a[3][0]= LTL a[3][1]= 2
a[4][0]= APD a[4][1]= 2

I would like to sort the first column after sorting the second column
numerically.
So the sort of the above data should give back.

ABW 1
ANA 1
APD 2
LTL 2
MFI 12

Thanks


phaylon 02-18-2005 07:18 PM

Re: Newbie question sorting
 
mdfoster44 wrote:

> Could someone please point me to some documentation on how to sort arrays
> such as these?


Sure: 'perldoc -f sort'.

hth,phay

--
http://www.dunkelheit.at/
thou shallst fear...


Gunnar Hjalmarsson 02-18-2005 07:37 PM

Re: Newbie question sorting
 
mdfoster44@netscape.net wrote:
> Could someone please point me to some documentation on how to sort
> arrays such as these?
>
> a[0][0]= ANA a[0][1]= 1
> a[1][0]= MFI a[1][1]= 12
> a[2][0]= ABW a[2][1]= 1
> a[3][0]= LTL a[3][1]= 2
> a[4][0]= APD a[4][1]= 2


Which programming language are you dealing with? That does not look like
Perl code to me.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Paul Lalli 02-18-2005 07:54 PM

Re: Newbie question sorting
 
<mdfoster44@netscape.net> wrote in message
news:1108752461.443860.42760@o13g2000cwo.googlegro ups.com...

> Could someone please point me to some documentation on how to sort


perldoc -f sort

> arrays such as these?
>
> a[0][0]= ANA a[0][1]= 1
> a[1][0]= MFI a[1][1]= 12
> a[2][0]= ABW a[2][1]= 1
> a[3][0]= LTL a[3][1]= 2
> a[4][0]= APD a[4][1]= 2


What language is this?

> I would like to sort the first column


How are you defining 'column'?

> after sorting the second column numerically.
> So the sort of the above data should give back.
>
> ABW 1
> ANA 1
> APD 2
> LTL 2
> MFI 12
>


#!/usr/bin/perl
use strict;
use warnings;
my @data = (
['ANA', 1],
['MFI', 12],
['ABW', 1],
['LTL', 2],
['APD', 2],);

print "$_->[0] $_->[1]\n" for sort {$a->[1] <=> $b->[1] or $a->[0] cmp
$b->[0]} @data;

__END__
ABW 1
ANA 1
APD 2
LTL 2
MFI 12



Paul Lalli


mdfoster44@netscape.net 02-18-2005 10:02 PM

Re: Newbie question sorting
 
Paul Lalli wrote:
> <mdfoster44@netscape.net> wrote in message
> news:1108752461.443860.42760@o13g2000cwo.googlegro ups.com...
>
> > Could someone please point me to some documentation on how to sort

>
> perldoc -f sort
>
> > arrays such as these?
> >
> > a[0][0]= ANA a[0][1]= 1
> > a[1][0]= MFI a[1][1]= 12
> > a[2][0]= ABW a[2][1]= 1
> > a[3][0]= LTL a[3][1]= 2
> > a[4][0]= APD a[4][1]= 2

>
> What language is this?
>
> > I would like to sort the first column

>
> How are you defining 'column'?
>
> > after sorting the second column numerically.
> > So the sort of the above data should give back.
> >
> > ABW 1
> > ANA 1
> > APD 2
> > LTL 2
> > MFI 12
> >

>
> #!/usr/bin/perl
> use strict;
> use warnings;
> my @data = (
> ['ANA', 1],
> ['MFI', 12],
> ['ABW', 1],
> ['LTL', 2],
> ['APD', 2],);
>
> print "$_->[0] $_->[1]\n" for sort {$a->[1] <=> $b->[1] or $a->[0]

cmp
> $b->[0]} @data;


Thank you for your reply.

Can something similar work for a reference to an array of references to
arrays?
I using a "$data" from a mysql query.

my $data = $sth->fetchall_arrayref;

so then the data is actually
$data->[0][0] = ANA
$data->[0][1] = 1
$data->[1][0] = MFI and so forth.

Thanks again for taking the time to help.

Martin Foster.

>
> __END__
> ABW 1
> ANA 1
> APD 2
> LTL 2
> MFI 12
>
>
>
> Paul Lalli



A. Sinan Unur 02-18-2005 10:48 PM

Re: Newbie question sorting
 
mdfoster44@netscape.net wrote in news:1108764160.887577.83230
@g14g2000cwa.googlegroups.com:

> Can something similar work for a reference to an array of references
> to arrays?
> I using a "$data" from a mysql query.
>
> my $data = $sth->fetchall_arrayref;


Why don't you write the query to return the results in the order you want?

Sinan.


All times are GMT. The time now is 01:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.