Perl Archive

Perl Scripting for Beginners

While learning Perl these are the few scripts I developed.

I am not an expert in perl but I know enough to accomplish my tasks

Execute the code and understand it. You might find a better way to accomplish a certain task, do
share it with us.
……………………….

#! /usr/bin/perl

#Adding two numbers.

$a=5;
$b=7;
$c = $a + $b;
print “The sum is $c \n”;

……………………………

#! /usr/bin/perl

#Adding two numbers.

($a, $b)= (5,7);
$c = $a + $b;
print “The sum is $c \n”;

…………………………..

#! /usr/bin/perl

#Adding two numbers.

print “Enter First Value \n”;
$a=;
print “Enter Second value \n”;
$b=;
$c=$a+$b;

print “The sum is $c \n”;

—————————–

#! /usr/bin/perl

#Adding two numbers.

print “Enter two values \n”;
chomp($a=<>,$b=<>);
print “The sum is”,$a+$b, “\n”;
print “The sum is $a+$b \n”;

——————————–
#! /usr/bin/perl

$x=’abc xyz’;
print “$x \n”;

—————————-

#! /usr/bin/perl

$x=q/abc/;
$y = qq//xyz//;

print “$x $y”;

————————
#! /usr/bin/perl

$x=5;
print “The value is $x \n”;
undef $x;
printf “The value afer undef is $x \n”;

———————
#! /usr/bin/perl

printf “Enter Name : “;
chomp($a=<>);

if ( -f $a){
printf “File \n”; }

elsif (-d $a){
printf “Directory \n”; }

else {
printf “Something else \n”; }

————————-

#! /usr/bin/perl

$file=0;
$dir=0;

foreach $x ( `ls` )

{
chomp($x);
if ( -f $x){
$file++; }
if ( -d $x){
$dir++; } }

printf “Count of Files $file \n”;
printf “Count of Directories $dir \n”;

——————————————–

#! /usr/bin/perl

$file=0;
$dir=0;

foreach ( `ls` )
{
chomp($_);
if ( -f $_){
$file++; }
if ( -d $_){
$dir++; } }

printf “Count of Files $file \n”;
printf “Count of Directories $dir \n”;

—————————————————
Find the file names which has a in them
#! /usr/bin/perl
foreach $x ( `ls` )
{
chomp($x);
$indexVal=index($x,’a’);
if ($indexVal >= ‘0′)
{
printf “file name with a is :$x \n”; } }

—————————————-

Find all file names wit dot in it

————————————-

Using string functions
#! /usr/bin/perl

$str=”My name is khan”;

$indexInt=index($str,’i’);
printf “index of i is : $indexInt \n”;

$lengthStr=length($str);
printf “Length of string is : $lengthStr\n”;

$reverseStr=reverse($str);
printf “Reverse of strng is : $reverseStr\n”;

$rindexInt=index($str,’i’);
printf “reverse index of i is : $rindexInt \n”;
$subStr=substr($str,’5′);
printf “substring is : $subStr \n”;

$lcStr=lc($str);
printf “lowercase string is : $lcStr \n”;

————————–

Using Number Functions

#! /usr/bin/perl

print abs(-1);
print “\n”;
print int(“45?);
print “\n”;
print sin(90);
print “\n”;
print cos(90);
print “\n”;
print exp(2);
print “\n”;
print sqrt(4);
print “\n”;
print rand();
print “\n”

—————————–

Conditions and loops

Take a filename, in that filename find out that there is no repitition of the same character.

(Using While loop )

#! /usr/bin/perl

foreach $x ( `ls` )
{
chomp($x);
$pos=0;

$lengthStr=length($x);

while ($lengthStr !=0 ){

$subStr=substr($x,$pos);
$index=$lengthStr;
$lengthStr–;
$char = substr($x,$pos,$index-$lengthStr);

$pos++;

# printf “char $char + lengthStr $lengthStr + index $index pos $pos \n”;

$tempStr = substr($x,$pos,$index);
# print “tempStr $tempStr”;

$indexInt=index($tempStr,$char);

if ($indexInt >= 0)
{
printf “repetition of character $char in file $x \n”; }

} }

——————————–

Arrays

#! /usr/bin/perl

@x=(1,2,3,4,5);
@y=(1..4);
@z=();
@p=(‘a’..’z’);
@q=(@p,@x);

print ” x : @x \n”;
print ” y : @y \n”;
print ” z : @z \n”;
print ” p : @p \n”;
print ” q : @q \n”;

$length = @q;

print ” length of q : $length \n”;

——————————–

Deleting elements of array and increasing size

#! /usr/bin/perl

@x=(1,2,3,4,5);
print ” original array @x \n”;
splice(@x,0,1);
print ” spliced array @x \n”;

# Increasing array limit

$x[7]=8;

print ” increased array @x \n”;

———————————
Selective copying of array

#! /usr/bin/perl

@x=(1,2,3,4,5);

@y=@x[0,2,4];
print ” array x @x \n”;
print ” array y @y \n”;
——————

Splitting array
#! /usr/bin/perl

$x=”COMPUTER”;
@y=split(”,$x);
print “value of y @y \n “;

—————-
Joining aray

#! /usr/bin/perl

@x=(‘a’..’z’);
$y= join(”,@x);
print “value of y $y \n “;
——————–
Sorting array…

#! /usr/bin/perl

@x=(1,3,4,5,6);
@y= sort { $a <=> $b} @x;
print “value of y @y \n “;

—————–
popping, shifting and unshifting…
#! /usr/bin/perl
@x=(1..10);
pop(@x);
print “after poppin @x \n “;

shift(@x);
print “after shifting @x \n “;

unshift(@x,1);
print “after un shifting @x \n “;

—————–
#! /usr/bin/perl
@y=(66,53,74,75,76,75,76,32,99,45,66);
@x= sort {$b<=>$a} @y;

$length = @x;

$pos=0;

for $a (@x)
{
# printf ” a = $a \n”;
$curr = @x[$pos];

$temp = $pos+1;
$next = @x[$temp];

# printf ” pos $pos curr $curr next $next \n”;
if ($curr == $next )
{
# printf ” curr == next! \n”;
printf “$temp:@x[$pos]\n “;
printf “$temp:@x[$temp]\n”;
$pos++; }
else {
printf “$temp:@x[$pos]\n”; }
$pos++;

if($pos >= $length)
{
exit } }

————————————-

Working with associative array

#! /usr/bin/perl

%emp=(101=>abc,102=>xyz,103=>rst,104=>pqr,105=>uvw);

print %emp;
$x = keys %emp;
$y = values %emp;

print “keys $x \n”;
print “values $y \n”;

print “\n $emp{102} \n”;

foreach $k (sort{$a cmp $b}keys %emp)
{

print “$k => $emp{$k} \n”; }

————————————–
Creating hash with key and value taken from the keyboard.

#! /usr/bin/perl
foreach $k (1..4)
{
print “Enter key and pair values \n”;
chomp($key=<>,$value=<>);
$emp{$key}=$value;

}

print %emp;

——————

Reversing hash -> key becomes value and value becomes hash

#! /usr/bin/perl

%emp=(101=>abc,102=>xyz,103=>rst,104=>pqr,105=>uvw);

print %emp;
print “\n”;
print “After reversing\n”; %emp = reverse %emp;
print %emp;
print “\n”;

—————————-

Addressing

# Creating reference for a scalar in perl
#! /usr/bin/perl
print “Referencing scalar \n”;
$x=”COMPUTER”;
$y=\$x;

print “$x\n”;
print “$y\n”;
print “Dereferencing y $$y\n”;

#Creating reference for an array in perl

@x=(1,2,3,4,5);
$y=\@x;

print “Referencing array \n”;
print “@x\n”;
print “$y\n”;
print “Dereferencing y @$y\n”;

# to dereference individual element

print “Dereferencing fist element $$y[0]\n”;

#Creating reference for hash in perl

%x=(101=>abc,102=>xyz,103=>rst,104=>pqr,105=>uvw);
$y=\%x;

print “Referencing hash \n”;
print %x;
print “\n”;
print “$y\n”;
print “Dereferencing y”,%$y,”\n”;

————————————————-
Printing content of an array in perl

#! /usr/bin/perl

@x=([1,2,3],[4,5,6],[7,8,9]);
$y=\@x;

foreach $i (0,1,2)

{
foreach $j (0,1,2)
{
print “x[$i][$j]= $x[$i][$j] \t”; }
print “\n”; }

————————-

Adding two array in perl

#! /usr/bin/perl

@x=([1,2],[4,5]);
$y=([1,2],[4,5]);

foreach $i (0,1)
{
foreach $j (0,1)
{
$a[$i][$j] = $x[$i][$j] + $y[$i][$j];
print “a[$i][$j]= $a[$i][$j] \t”; }
print “\n”; }

————————–

Array of hashes in perl

#! /usr/bin/perl

#Array oh hashes

@x=({1,a,2,b},{3,c,4,d},{5,e,6,f},{7,g,8,h});

$hashref0=@x[0];
$hashref1=@x[1];
$hashref2=@x[2];
$hashref3=@x[3];

print “Dereferencing hashref0 “,%$hashref0,”\n”;
print “Dereferencing hashref1 “,%$hashref1,”\n”;
print “Dereferencing hashref2 “,%$hashref2,”\n”;
print “Dereferencing hashref3 “,%$hashref3,”\n”;

—————————————

#! /usr/bin/perl

%h=(a=>[1,2,3],b=>[4,5,6],c=>[7,8,9]);

foreach $k (values %h)
{
@a[$k]=$h{$k}; }
$arrRef0=@a[0];
$arrRef1=@a[1];
$arrRef2=@a[2];

print “$arrRef0 $arrRef1 $arrRef2?;

————————
Print the hash values in perl

%x={2=>{‘Faisal’,’TM’},1=>{‘Ritu’,’IT’},3=>{‘Dhruv’,’CT’}};

?
—————————

Defining Sub routines in perl

#! /usr/bin/perl

print ” Enter temperature in Fahrenheit \n”;
chomp($f=);

&cal();

print “The result is $cel\n”;

sub cal()
{
$cel= ($f-32)*5/9; }
————————–

Returning values from sub routine in perl

#! /usr/bin/perl

$sum=&cal();
print “The sum is $sum \n”;
sub cal()
{
print “Enter two numbers. \n”;
chomp($n1=);
chomp($n2=);
return ($n1+$n2); }

———————-
Using Local variables in subroutines using my in perl

#! /usr/bin/perl

$sum=&cal();
print “The sum is $sum \n”;
sub cal()
{
my ($n1,$n2);
print “Enter two numbers. \n”;
chomp($n1=);
chomp($n2=);
return ($n1+$n2); }

————————–
Passing values to the function using @_

#! /usr/bin/perl

$sum=&cal(3,5);
print “The sum is $sum \n”;
sub cal()
{
my ($n1,$n2)=@_;
print “Enter two numbers. \n”;
chomp($n1=);
chomp($n2=);
return ($n1+$n2); }

——————————

Anonymous subroutine in perl

#! /usr/bin/perl

$subref =sub{
my ($a,$b)=@_;
print $a+$b;
print “\n”; };

&$subref(2,3);

—————————-
Finding maximum value in an array by passing array

#! /usr/bin/perl

@x=(10,20,30,33,21,34,56,6,44);

$max=&getmaxarray(@x);

print ” maximum value $max \n”;

sub getmaxarray()
{
my ($x)=@_;
$temp=0;

for $a (@x)
{

if($temp < $a)
{
$temp=$a; } }
return $temp; }

————————————-

Finding maximum value in an array by passing array reference

@x=(10,20,30,33,21,34,56,6,44);
$y=\@x;

$max=&getmaxarray($y);

print ” maximum value $max \n”;

sub getmaxarray()
{
my ($y)=@_;

for $a (@$y)
{

if($temp < $a)
{
$temp=$a; } }
return $temp;

}

————————-

Regular Expressions in perl

Print list of all files in the current directory which starts with a

#! /usr/bin/perl

foreach $x (`ls`)
{
chomp($x);

if($x=~m/^a/)
{
print “$x\n”; } }

————————–
Change filename which filename more that 10 characters to capital letter.

#! /usr/bin/perl

foreach $x (`ls`)
{
chomp($x);

if(length($x)>10)
{
$x =~ tr/a-z/A-Z/;
print “Length greater than 10 $x \n”; }
}