Category Archives: Uncategorized

How can I check if a Perl array contains a particular value?

For searching the presence of a value in an array in Perl, we can use following code :

#!/usr/bin/perl -w
my @arry=('asd','hhf','qw33','dfd','ppq');
my $search_value='asd';
if($search_val ~~ @arry) {
    print "Value '$search_val' is present in the given array...";
}

The program will give an output as follows if the search value is present in the array.

Value 'asd' is present in the given array...

Perl Program to calculate age

To calculate the age, we can use following subroutine :

print &age($tmpdob[2],$tmpdob[1],$tmpdob[0]);
sub age {
           # Assuming $birth_month is 0..11
           my ($birth_day, $birth_month, $birth_year) = @_;
           my ($day, $month, $year) = (localtime)[3..5];
          $year += 1900;
          $month+=1;
          my $age = $year - $birth_year;
          $age-- unless sprintf("%02d%02d", $month, $day)
            >= sprintf("%02d%02d", $birth_month, $birth_day);
          my $mnth=($month>$birth_month)?$month-$birth_month:12+($month-$birth_month);
          return $age."Y/".$mnth."M ";

}

How to create a folder in PHP?

In PHP we can create a folder using mkdir() function.

eg:

>?php
       mkdir("testing");
?>

How to check availability of a folder in PHP?

We can check the availability of a folder using PHP as follows :

if(file_exists ( "folder_name" ))
{
     echo "Folder Exists";
}
else
{
     echo "Folder Not Exists";
}

How to check availability of a folder in perl?

We can check the availability of a folder using Perl as follows :

if(-d "cgi-bin")
{
          # directory called cgi-bin exists
}
elsif(-e "cgi-bin")
{
          # cgi-bin exists but is not a directory
}
else 
{
          # nothing called cgi-bin exists
}

As a note, -e doesn’t distinguish between files and directories. To check if something exists and is a plain file, use -f.