Perl: Can someone please explain this code "sort { $a <=> $b } @unsorted"

Login or Register to Reply

Thread Tools Search this Thread
# 1  
Old 1 Week Ago
Perl: Can someone please explain this code "sort { $a <=> $b } @unsorted"

@sorted = sort { $a <=> $b } @unsorted;

I am having hard time understanding how this works? I know the output but interested to know the working.

Thanks in advance.
# 2  
Old 1 Week Ago
the spaceship operator, <=>, is a comparison function, for numeric data only. It compares elements in the input array, @unsorted. The output of the sort call is written to the array @sorted. If no comparison function statement was entered (a blank), then sort works on the @unsoted array as letters of the alphabet, not numbers.

$a and $b are individual elements in the unosrted array - in this case numbers

If element $a is equal to element $b, then it returns 0; if $a is less than $b it returns -1; if $a is greater it returns +1 .

So the general statement is:
@output_array = sort [optional comparison function]  @input_array

Internally perl sort calls a standard C library function, qsort for doing the comparisons and reordering over and entire array -or at least it did years ago.
qsort(3): sort array - Linux man page
These 3 Users Gave Thanks to jim mcnamara For This Post:
apmcd47 (1 Week Ago) rbatte1 (6 Days Ago) Tanu (1 Week Ago)
# 3  
Old 6 Days Ago
Hi, Tanu.

If you were to execute perldoc -f sort, you would find explanation:
    sort BLOCK LIST
    sort LIST
            In list context, this sorts the LIST and returns the sorted list
            value. In scalar context, the behaviour of "sort()" is undefined.

            If SUBNAME or BLOCK is omitted, "sort"s in standard string
            comparison order. If SUBNAME is specified, it gives the name of a
            subroutine that returns an integer less than, equal to, or greater
            than 0, depending on how the elements of the list are to be
            ordered. (The "<=>" and "cmp" operators are extremely useful in
            such routines.)

and if executed perldoc perldoc, one sees:
    perldoc - Look up Perl documentation in Pod format.

        perldoc [-h] [-D] [-t] [-u] [-m] [-l] [-F]

so you can find answers to questions like this by yourself. Of course, not all documentation is easily understandable, so we can help in situations like that.

These commands were run on a system like:
OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64
Distribution        : Debian 8.11 (jessie) 
perl 5.20.2
Perldoc v3.23, under perl v5.020002 for linux

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
Tanu (6 Days Ago)
Login or Register to Reply

Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9" jcdole Shell Programming and Scripting 1 01-19-2019 05:37 AM
Extract delta records using with "comm" and "sort" commands combination njny UNIX for Beginners Questions & Answers 7 08-16-2017 11:56 PM
Using "mailx" command to read "to" and "cc" email addreses from input file asjaiswal UNIX for Dummies Questions & Answers 2 10-11-2014 09:32 PM
Perl failure with "main::$fn" used only once:" in error logs bashily Shell Programming and Scripting 3 03-07-2013 11:52 AM
Is it Possible to sort a list of hexadecimal numbers using "sort" command? Kesavan Shell Programming and Scripting 9 08-21-2011 08:33 AM
What does "__FD_PRN_" means in perl code ? sarbjit UNIX for Advanced & Expert Users 3 07-19-2011 07:16 AM
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 08:59 AM
Meaning of "b" modifier in "sort" command wjssj Shell Programming and Scripting 3 02-04-2011 10:56 AM
Is this "Out of Memory!" error due to sort() of perl how to resolve it? lilili07 Shell Programming and Scripting 3 07-07-2010 06:00 PM
perl file, one line code include "length, rindex, substr", slow jimmy_y Shell Programming and Scripting 3 06-09-2010 09:44 AM
Explain How ":=" works in shell scrpting muddasani Shell Programming and Scripting 2 02-16-2010 06:57 AM
perl folder list with "..", without ".". jimmy_y Shell Programming and Scripting 6 07-07-2009 09:25 PM
Explain the line "mn_code=`env|grep ""|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 12:52 AM
can you explain this AWK "c+" statement ? Nicol UNIX for Advanced & Expert Users 8 10-06-2004 05:31 AM