Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Perl: Can someone please explain this code "sort { $a <=> $b } @unsorted" Post 303032141 by jim mcnamara on Tuesday 12th of March 2019 07:00:10 PM
Old 03-12-2019
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:
Code:
@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:
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

2. Shell Programming and Scripting

Explain How ":=" works in shell scrpting

Hi , can any one explain how in the below code ":=" working in shell programming. L_DEBUG=${L_DBG:=N} if ; then set -x fi Thanks muddasas (2 Replies)
Discussion started by: muddasani
2 Replies

3. Shell Programming and Scripting

perl file, one line code include "length, rindex, substr", slow

Hi Everyone, # cat a.txt a;b;c;64O a;b;c;d;ee;f # cat a.pl #!/usr/bin/perl use strict; use warnings; my $tmp3 = ",,a,,b,,c,,d,,e,,f,,"; open(my $FA, "a.txt") or die "$!"; while(<$FA>) { chomp; my @tmp=split(/\;/, $_); if ( ($tmp =~ m/^(64O)/i) || ($tmp... (3 Replies)
Discussion started by: jimmy_y
3 Replies

4. Shell Programming and Scripting

Is this "Out of Memory!" error due to sort() of perl how to resolve it?

I am running a program written in perl script and it is stopped with "Out of memory!" error. This is very strange because at the time then the program is aborted, it only used 4GB RAM and there are still 30GB free physical memory left in the system. I check the perl script and found the program... (3 Replies)
Discussion started by: lilili07
3 Replies

5. Shell Programming and Scripting

Meaning of "b" modifier in "sort" command

I need to sort the following file by the rhdiskpower devices in the last column: Total_MB Free_MB OS_MB Name Failgroup Library Label UDID Product Redund Path 1024 851 1024 OCRVOT1_0000 OCRVOT1_0000 System UNKNOWN ... (3 Replies)
Discussion started by: wjssj
3 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. UNIX for Advanced & Expert Users

What does "__FD_PRN_" means in perl code ?

I have seen something like this in a perl code: $_ =~ s/__FD_PRN_/\\(/g What does this "__FD_PRN_" means. I have searched google but was not able to find any info regarding this. Appreciate if some one can refer to a link for these characters. From comments/code it used to substitue "(" with... (3 Replies)
Discussion started by: sarbjit
3 Replies

8. Shell Programming and Scripting

Is it Possible to sort a list of hexadecimal numbers using "sort" command?

Hello Everybody :) !!!. i have question in mind, is it possible to sort a list of hexadecimal numbers using "sort" command? (9 Replies)
Discussion started by: Kesavan
9 Replies

9. UNIX for Beginners Questions & Answers

Extract delta records using with "comm" and "sort" commands combination

Hi All, I have 2 pipe delimited files viz., file_old and file_new. I'm trying to compare these 2 files, and extract all the different rows between them into a new_file. comm -3 < sort file_old < sort file_new > new_file I am getting the below error: -ksh: sort: cannot open But if I do... (7 Replies)
Discussion started by: njny
7 Replies

10. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies
ModPerl::Code(3)					User Contributed Perl Documentation					  ModPerl::Code(3)

NAME
$class::Const - Perl Interface for $class Constants SYNOPSIS
CONSTANTS
EOF my $groups = $data{$class}; for my $group (sort keys %$groups) { print $fh <<"EOF"; ":$group" use $class::Const -compile qw(:$group); The ":$group" group is for XXX constants. EOF for my $const (sort @{ $groups->{$group} }) { print $fh "=head3 C<$class::$const> "; } } print $fh "=cut "; } } sub generate_constants_lookup_doc { my ($data) = @_; while (my ($class, $groups) = each %$Apache2::ConstantsTable) { my $constants = [map { @$_ } values %$groups]; constants_lookup_code_doc($constants, $class, $data); } } sub generate_constants_group_lookup_doc { my ($data) = @_; while (my ($class, $groups) = each %$Apache2::ConstantsTable) { constants_group_lookup_code_doc($class, $groups, $data); } } sub constants_group_lookup_code_doc { my ($class, $groups, $data) = @_; my @tags; my @code; while (my ($group, $constants) = each %$groups) { $data->{$class}{$group} = [ map { my @ifdef = constants_ifdef($_); s/^($constant_prefixes)_?//o; $seen_const{$class}{$_}++; $_; } @$constants ]; } } sub constants_lookup_code_doc { my ($constants, $class, $data) = @_; my (%switch, %alias); %alias = %shortcuts; my $postfix = lc $class; my $package = $class . '::'; my $package_len = length $package; my $func = canon_func(qw(constants lookup), $postfix); for (@$constants) { if (s/^($constant_prefixes)(_)?//o) { $alias{$_} = join $2 || "", $1, $_; } else { $alias{$_} ||= $_; } next unless /^([A-Z])/; push @{ $switch{$1} }, $_; } for my $key (sort keys %switch) { my $names = $switch{$key}; for my $name (@$names) { my @ifdef = constants_ifdef($alias{$name}); push @{ $data->{$class}{other} }, $name unless $seen_const{$class}{$name} } } } sub generate_exports { my ($self, $c_fh) = @_; require ModPerl::WrapXS; ModPerl::WrapXS->generate_exports($c_fh); } # src/modules/perl/*.c files needed to build APR/APR::* outside # of mod_perl.so sub src_apr_ext { return map { "modperl_$_" } (qw(error bucket), map { "common_$_" } qw(util log)); } 1; __END__ NAME
ModPerl::Code - Generate mod_perl glue code SYNOPSIS
use ModPerl::Code (); my $code = ModPerl::Code->new; $code->generate; DESCRIPTION
This module provides functionality for generating mod_perl glue code. Reason this code is generated rather than written by hand include: consistency thin and clean glue code enable/disable features (without #ifdefs) adapt to changes in Apache experiment with different approaches to gluing AUTHOR
Doug MacEachern perl v5.12.1 2007-12-31 ModPerl::Code(3)
All times are GMT -4. The time now is 08:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy