.:: Welcome To My Personal Blog ::.

Sunday, April 24, 2011

Beginner's Guide To Travel With Perl - Part 9

Sorting:

There is a built-in sort function/subroutine in Perl for sorting array elements. This is done by just writing "sort @name_of_the_array;". But it sorts the elements alphabetically according to their ASCII value. This sort function sorts lexically
Example:
use strict;
main(@ARGV);
sub main
{
    my @arr = ("abc", "abcd", "defg", "def", "ijklm", "ijkl", "nop", "nop", "aaa", "AAA");
    print "before sort : @arr\n";        #before sort : abc abcd defg def ijklm ijkl nop nop aaa AAA
    @arr = sort @arr;
    print "after sort : @arr\n";        #after sort : AAA aaa abc abcd def defg ijkl ijklm nop nop
}


This code also does the same work:
use strict;
main(@ARGV);
sub main
{
    my @arr = ("abc", "abcd", "defg", "def", "ijklm", "ijkl", "nop", "nop", "aaa", "AAA");
    print "before sort : @arr\n";
    @arr = sort {$a cmp $b} @arr;        #Here's the difference
    print "after sort : @arr\n";
}
To sort in reversed order you have to change the condition "$a cmp $b" into "$b cmp $a".
Example:
use strict;
main(@ARGV);
sub main
{
    my @arr = ("abc", "abcd", "defg", "def", "ijklm", "ijkl", "nop", "nop", "aaa", "AAA");
    print "before sort : @arr\n";        #before sort : abc abcd defg def ijklm ijkl nop nop aaa AAA
    @arr = sort {$b cmp $a} @arr;
    print "after sort : @arr\n";        #after sort : nop nop ijklm ijkl defg def abcd abc aaa AAA
}
To sort case-insensitively just write "uc($b) cmp uc($a)" or "lc($b) cmp lc($a)" in order to "$b cmp $a".

Now let's try to sort numerical array elements. We can do it by writing "sort {$a <=> $b} @array_name". It sorts the numbers in increasing order.
Example:
use strict;
main(@ARGV);
sub main
{
    my @arr = (4, 1, 6, 20, 8, 200, 9, 4, 5, 5);
    print "before sort : @arr\n";         #before sort : 4 1 6 20 8 200 9 4 5 5
    @arr = sort {$a <=> $b} @arr;
    print "after sort : @arr\n";            #after sort : 1 4 4 5 5 6 8 9 20 200
}
To sort in decreasing order, just change "$a <=> $b" into "$b <=> $a".
Example:
use strict;
main(@ARGV);
sub main
{
    my @arr = (4, 1, 6, 20, 8, 200, 9, 4, 5, 5);
    print "before sort : @arr\n";         #before sort : 4 1 6 20 8 200 9 4 5 5
    @arr = sort {$b <=> $a} @arr;
    print "after sort : @arr\n";            #after sort : 200 20 9 8 6 5 5 4 4 1
}
Something Interesting:
use strict;
main(@ARGV);
sub main
{
    my @arr1 = ("abcd", "efg", "hiJ", "KLM", "NOPE");
    my @arr2 = ("abdc", "Efg", "hIJ", "KLM", "nope");
    my @arr = sort @arr1, @arr2;
    print "after sort : @arr\n";      #after sort : Efg KLM KLM NOPE abcd abdc efg hIJ hiJ nope
}
Another One:
use strict;
main(@ARGV);
sub main
{
    my @arr1 = (23, 42, 12, 4, 56, 12, 45);
    my @arr2 = (56, 42, 12, 78, 54, 80, 20);
    my @arr = sort {$a <=> $b} @arr1, @arr2;
    print "after sort : @arr\n";               #after sort : 4 12 12 12 20 23 42 42 45 54 56 56 78 80
}
Have you realized, what's going on? It merges multiple arrays and sorts all the elements according to the sort criteria.

Discussion:

To sort elements we are using "$a <=> $b" or "$a cmp $b" symbols. Why we are using "<=>" or "cmp" ? Basically, the expression "$a <=> $b" or "$a cmp $b" for strings returns one of the values 1, 0, -1 if $a is, respectively, larger, equal or lower than $b. According the returned value, the swapping of the elements occur. If we use any other variables rather than $a and $b, it will not work. We must use $a and $b.



Help Links:

No comments:

Post a Comment

Popular Posts