1⟩ How to sort dates in Perl?
Example
-------
use Data::Dumper;
my @dates = ( "02/11/2009" , "20/12/2001" , "21/11/2010" ) ;
@dates = sort { join( '', (split '/', $a)[2,1,0] ) cmp
join( '', (split '/', $b)[2,1,0]) } @dates;
print Dumper @dates;
“CGI Perl frequently Asked Questions by expert members with experience in CGI Perl. So get preparation for the CGI Perl job interview”
Example
-------
use Data::Dumper;
my @dates = ( "02/11/2009" , "20/12/2001" , "21/11/2010" ) ;
@dates = sort { join( '', (split '/', $a)[2,1,0] ) cmp
join( '', (split '/', $b)[2,1,0]) } @dates;
print Dumper @dates;
use the following code.
<code>
use bigint;
my $r = 0x234;
my $t = 0x345;
my $x1 = $r << 32 | $t;
print $x1->as_hex;
</code>
#! /usr/bin/per
use strict;
#- Use Input
#---------------
my $ip_add = "";
#- Script start
#---------------
print"Enter the IP addressn";
$ip_add = <STDIN>;
chomp($ip_add); # Remove the last "n" character
$ip_add=$ip_add."."; # Append . at the end of IP address
#-------Expression to check IP address is valid or not-----#
if($ip_add =~ m/^[0-2]?[0-5]?[0-5]?.){4}$){
print" Valid IP Addressn";
}else{
print" Invalid IP Addressn"
}
#!/usr/bin/perl
my $word = $ARGV[0];
$sortword = "";
$lastchar = "";
while($word =~ /(.)/g)
{
$lastchar = $1;
if( $sortword ) {
$flag = "";
$newsortword = "";
while($sortword =~ /(.)/g) {
if( $lastchar gt $1 || $flag
eq "charcovered") {
$newsortword =
$newsortword.$1;
$flag = "greater" if($flag
ne "charcovered")
}
else {
$newsortword =
$newsortword.$lastchar.$1;
$flag = "charcovered";
}
}
if( $flag ne "charcovered" ) {
$newsortword =
$newsortword.$lastchar;
}
$sortword = $newsortword;
}
else {
$sortword = $lastchar;
}
}
print $sortword."n";
my $i;
my $str="hello";
my @str=split('',$str);
for($i=$#str;$i>=0;$i--)
{
print $str[$i];
}
$r="YASHWANTH";
@n=split (//,$r);
print "@nn";
@t=splice(@n,0,3);
print "@tn";
$r=join("",@t);
print "$rn";
~
~
my $str="This is testing of string reverse";
print scalar reverse $s
chomp is used to remove the trailing new line.
If we give chomp list then it will remove the trailing new
line in all the elements of the list.
die function is used for handling the errors in Perl.
It terminates the program immediately after printing the
message passed to the die function.
die("testing of die");
It will print the message "testing of die" on screen and
terminates the program.
Pattern Matching
----------------
[code]
my $str="10.111.23.11";
my $file="/root/abc/cde/fgg/ac.xml";
my $file1="/root/abc/ac.xml/fgg/ac.xml";
if($str=~/[0-9]{2}.[0-9]{3}.[0-9]{2}.[0-9]{2}/)
{
print "Pattern $& get matchedn";
}
if($file1=~s//(.*)/(.*).(.*)/$2/)
{
print "File Name is $file1n";
}
print strftime("%B",0,0,0,11,2-1,2009);
[/code]
compare to other c and java perl is having strong regular
expression concept so data extraction will be easier and
complition will be faster too
Rigging is use for if we want to give animation for any
object or character then we apply to character or object
internel bone setting(like our bones).that is called
rigging. when apply rigging, then we can give proper
animation.
Package::Module(); This will throw as error,
I think,the question should be as: What is the difference
between,
Package::MyModule qw(); # FIRST
and
Package::MyModule; # SECOND
# FIRST :- This will not import any subroutine from MyModule.
# SECOND :- This will import all the subroutine from the
MyModule.
[code]
my $pat;
$pat='Using push we can add multiple items into an array in
a single instance.
If we are trying to add a module or library files in
our program using require or use statement then it will
search that module or library files in the Perl's default
search path.
The statement use lib is used to add the directories
to default search path.
So if the module or library file is not located in
the Perl's default search path then it will find the
library files in the path we have given with the use lib
$path.';
if($pat=~/push/)
{
print "Pattern push get matchedn";
}
[/code]
Lets assume that the text to match is present in a file
say "data.txt".
Following program will print the line containing same
starting and ending word.
open(FILE,"data.txt") or die "cannot open file : $!";
while(<FILE>) {
if($_ =~ /^(w+)s+.*?1$/) {
print "the line is $_ n";
}
}
print caller;
my $ref=[1,2,3,4];
print ref $ref;
ref will return the type of reference.
In this case ref will return as 'ARRAY'.
Perl is a language and MOD_PERL is a module of Apache used
to enhance the performance of the application.
If we are trying to add a module or library files in our
program using require or use statement then it will search
that module or library files in the Perl's default search path.
The statement use lib is used to add the directories to
default search path.
So if the module or library file is not located in the
Perl's default search path then it will find the library
files in the path we have given with the use lib $path.
#!c:perlbinperl
$Count = 0;
$pt = 2;
while ( $Count < @ARGV[0] )
{
if (isPrimary($pt))
{
print "$ptn";
$Count++;
}
$pt++;
}
sub isPrimary
{
$flag = 1;
for ($i=2; $i<=$_[0]/2; $i++)
{
if ($_[0] % $i == 0)
{
$flag = 0;
}
}
return $flag;
}
print("Enter the no. to check for Palindrome : ");
$no = <STDIN>;
chop($no);
$i = 0;
# Store into a array
while($no != 0)
{
@array[$i] = $no % 10;
$no = int($no / 10);
$i++;
}
$i--;
$j=0;
$flag = "true";
# Check for Palindrome
while( ($flag eq "true" )&& ( $j < @array/2) ){
if (@array[$j] != @array[$i])
{
$flag = "false"
}
$i--;
$j++;
}
# Print the result
if( $flag eq "true")
{
print("It is a Palindromen");
}
else
{
print("It is NOT a Palindromen");
}