Perl array sorting of numbers

Perl has in-built function called sort that can sort contents in array . By default sort  function consider the content as string. That means comparing the first character in both strings. “1” to “3”. “1” is ahead of “3” in the ASCII table and thus the string “12” will come before the string “3”.

my @numbers = (14, 3, 12, 2, 23);
my @sorted_numbers = sort @numbers;
say Dumper \@sorted_numbers;

Output :-

$VAR1 = [
    12,
    14,
    2,
    23,
    3
];

Perl don’t understand automatically that you need a sorting based on numerical methods.

No problem though as we can write a comparison function that will compare the two values as numbers. For that we use the <=> (also called spaceship operator) that will compare its two parameters as numbers and return 1, -1 or 0.

my @sorted_numbers = sort { $a <=> $b } @numbers;

Output:-

$VAR1 = [
    2,
    3,
    12,
    14,
    23
];

Perl array sorting in alphabetical order

Perl has in-built function called sort that can sort contents in array . By default sort function works based on ASCII value. To do sorting based on alphabetical order, we have to use function cmp which is used for comparison and function lc which is used for converting text to lower case.

my @words = qw(foo bar zorg moo);
my @sorted_words = sort { lc($a) cmp lc($b) } @words;
say Dumper \@sorted_words;

Output :-

$VAR1 = [
    'bar',
    'foo',
    'moo',
    'Zorg'
];

Perl array sort based on ASCII order

Perl has in-built function called sort that can sort contents in array . Please go through the below example :

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use Data::Dumper qw(Dumper);
my @words = qw(foo bar zorg moo);
say Dumper \@words;
my @sorted_words = sort @words;
say Dumper \@sorted_words;

Output :-

$VAR1 = [
    'foo',
    'bar',
    'zorg',
    'moo'
];
 
$VAR1 = [
    'bar',
    'foo',
    'moo',
    'zorg'
];

Perl Script to calculate difference between 2 dates.

Following script can be used to calculate difference between 2 dates.

sub date_diff {
    my %hash = @_;
    if($hash{start_day} && $hash{start_day}=~/^\d{1,2}$/ && $hash{start_month} && $hash{start_month}=~/^\d{1,2}$/ && $hash{start_year} && $hash{start_year}=~/^\d{4}$/) {
        if(!$hash{end_day} && !$hash{end_month} && !$hash{end_year}) {
            $hash{end_day}=strftime "%d", localtime;
            $hash{end_month}=strftime "%m", localtime;
            $hash{end_year}=strftime "%Y", localtime;
        } elsif($hash{end_day} && $hash{end_month} && $hash{end_year} && $hash{end_day}=~/^\d{1,2}$/ && $hash{end_month}=~/^\d{1,2}$/ && $hash{end_year}=~/^\d{4}$/) {
            
        } else {
            return 0;
        }
        use Time::Local;
        my @start_sec = (0, 0, 0, $hash{start_day}, int($hash{start_month})-1, $hash{start_year});
        my @end_day_sec = (0, 0, 0, $hash{end_day}, int($hash{end_month})-1, $hash{end_year});
        my $starttime = timelocal(@start_sec);
        my $endtime = timelocal(@end_day_sec);
        my %result;
        $result{s}=$endtime-$starttime;
        $result{i}=ceil($result{s}/60);
        $result{h}=ceil($result{s}/60/60);
        $result{d}=ceil($result{s}/60/60/24);
        $result{m}=ceil($result{s}/2629743);
        $result{y}=ceil($result{s}/31556926);
        return \%result;
    } else {
        return 0;
    }
}

my $result=&date_diff(start_day=>12, start_month=>6, start_year=>2015,end_day=>13, end_month=>6, end_year=>2015);
print "In seconds :- ".$result->{s};
print "In minutes :- ".$result->{i};
print "In Hours :- ".$result->{h};
print "In days :- ".$result->{d};
print "In months :- ".$result->{m};
print "In years :- ".$result->{y};

Output :-

In seconds :- 86404
In minutes :- 1440
In Hours :- 24
In days :- 1
In months :- 0
In years :- 0

Check for data match in an array.

Please be careful when you check for match in array:

#!/usr/bin/perl -w
use strict;
use warnings;
my @x=('manu','vinu','rinshad');
if("manu"~~@x) {
       print "Success";
} else {
       print "fail";
}

The above program will work fine in Perl V5.16. But there will be a warning message in Perl V5.18 and above. To overcome this issue, use following code :

#!/usr/bin/perl -w
use strict;
use warnings;
use experimental 'smartmatch';
my @x=('manu','vinu','rinshad');
if("manu"~~@x) {
    print "Success";
} else {
    print "fail";
}