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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Need a Custom or Prewritten Perl Program?: I need a program that...: Re: [rkk] Sorting an array: Edit Log


Jun 15, 2013, 7:18 AM

Views: 6658
Re: [rkk] Sorting an array

Perl would ignore the parenthesis in your second array. I assume that you intend an array of arrays. Your expected output is not consistent with my interpretation. Here is what I have.

use strict; 
use warnings;
use Readonly;
Readonly::Array my @arr2 =>
['ch1', 100],
['ch2', 20],
['ch1', 13],
['ch2', 45],
['ch1', 1]
my @index = sort { $arr2[$a][0] cmp $arr2[$b][0]
or $arr2[$a][1] <=> $arr2[$b][1]
} 0 .. $#arr2;
print "@index\n";

OUTPUT: 4 2 0 1 3

UPDATE: Please refer to cross posts. This question was already answered on perlmonks
Good Luck,

(This post was edited by BillKSmith on Jun 15, 2013, 7:37 AM)

Edit Log:
Post edited by BillKSmith (Veteran) on Jun 15, 2013, 7:37 AM

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

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