cgmd has asked for the wisdom of the Perl Monks concerning the following question:
The part I can't seem to understand is:#!/usr/bin/perl @table = qw{ 4:a:400 1:b:300 3:c:200 2:d:100 }; print "Original table...\n\n"; foreach (@table) { print "$_\n"; } print "\n"; $, = "\n"; print "Sorted table...\n\n"; print sort { (split ":", $a)[0] <=> (split ":", $b)[0] } @table; print "\n";
I was unable to conceive of a successful line of code when I tried to sort the array myself.Would someone please help me with the interpretation of how this line successfully sorts the array by the leading number of each element?print { (split ":", $a)[0] <=> (split ":", $b)[0] }
Thanks!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sorting an array with mixed elements...
by FunkyMonk (Bishop) on Jun 09, 2007 at 22:03 UTC | |
by cgmd (Beadle) on Jun 09, 2007 at 23:08 UTC | |
by lidden (Curate) on Jun 09, 2007 at 23:51 UTC | |
by FunkyMonk (Bishop) on Jun 10, 2007 at 08:18 UTC | |
by blazar (Canon) on Jun 10, 2007 at 21:38 UTC | |
|
Re: sorting an array with mixed elements...
by friedo (Prior) on Jun 09, 2007 at 21:57 UTC | |
|
Re: sorting an array with mixed elements...
by Cristoforo (Curate) on Jun 10, 2007 at 03:16 UTC | |
by cgmd (Beadle) on Jun 10, 2007 at 12:31 UTC | |
by blazar (Canon) on Jun 10, 2007 at 21:27 UTC | |
by cgmd (Beadle) on Jun 10, 2007 at 22:22 UTC |