Tag Archives: Perl Scripts

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";
}

About the usage ‘my $q=new CGI();’

About the usage my $q=new CGI();

  • Why do you switch between OO and function-based use of CGI.pm? Why do you use the, potentially problematic, new CGI syntax instead of the recommended CGI->new syntax?
  • In other languages new is a keyword. In Perl it’s just a method name. Which is what makes new Class rather fragile and best avoided. It’s annoying that there is so much documentation out there that uses this syntax.