CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Sort Array

 



Wiliam
Novice

Aug 16, 2000, 2:43 AM

Post #1 of 6 (747 views)
Sort Array Can't Post

I've now got the following code:

@keys = sort { $tobesorted_nextsub{$a} cmp $tobesorted_nextsub{$b} } keys %tobesorted_nextsub;

But my results still do not appear in alphabetical order. They still appear in random fashion. What am I doing wrong?

I believe the above code should sort out the keys in the alphabetical order of the values. But it doesn't Argh!

Help please? :-)

Wil.


Kanji
User

Aug 16, 2000, 4:33 AM

Post #2 of 6 (747 views)
Re: Sort Array [In reply to] Can't Post

They are in the ASCIIbetical sort order of your values.

If you want just the keys to be sorted use sort keys %tobesorted_next; instead.


Wiliam
Novice

Aug 16, 2000, 7:41 AM

Post #3 of 6 (747 views)
Re: Sort Array [In reply to] Can't Post

I know they're in ASCIIbetical but how do I get them into ALPHAbetical order?

Thanks!

Wil.


Cameron
Deleted

Aug 16, 2000, 8:08 AM

Post #4 of 6 (747 views)
Re: Sort Array [In reply to] Can't Post

You sorted the values, not the keys.
so if your print @sorted_keys, they will
still be in an apparently random order.

to sort the keys
@sorted_keys=sort keys %hash;
to sort the values
@sorted_values=sort values %hash;

Cameron


Kanji
User

Aug 16, 2000, 8:56 AM

Post #5 of 6 (747 views)
Re: Sort Array [In reply to] Can't Post

You need to be a little more consistent: you say the results are in apparent random order, but then turn around and say they're ASCIIbetically, which isn't anywhere near random in nature.

To get alphabetically order, make the sort case insensitive. You prolly also want to sort numbers appropriately while you're at it ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

@sorted_values =
sort {
$hash{$a} <=> $hash{$b} ||
lc( $hash{$a} cmp lc( $hash{$b} )
} keys %hash;</pre><HR></BLOCKQUOTE>

As Cameron pointed out, you could also use values %hash instead ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

@sorted_values =
sort { $a <=> $b || lc( $a ) cmp lc( $b ) }
values %hash;</pre><HR></BLOCKQUOTE>


Wiliam
Novice

Aug 18, 2000, 6:57 AM

Post #6 of 6 (747 views)
Re: Sort Array [In reply to] Can't Post

Kanji,

Thankyou very very very much for your reply. It finaly worked! You did one typo in your piece of code:

@sorted_values = sort { $hash{$a} <=> $hash{$b} &#0124; &#0124; lc( $hash{$a} cmp lc( $hash{$b} ) } keys %hash;

You forgot a closing bracket after lc( $hash{$a} so I added that in and it works a treat.

Thankyou very much for your help. I do appreciate it!

Rgds,

Wiliam.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives