Saturday, October 18, 2008

Arrays

Arrays in the Perl starts with '@' symbol and it is not limited to only one data type.

#!/usr/bin/perl -w

@myArray=("Zero","one","two","three","four","five","six","seven","eight","nine");

foreach $val(@myArray)
{
print($val."\n");

}

Change the case to upper and lower

#!/usr/bin/perl -w

$str="Dasuntha";

$sub=substr $str, 2, 4;

print($sub."\n");

#convert the Uppercase

$Uppercase=uc $str;

print($Uppercase."\n");

#convert case of the first letter
$fcase=ucfirst $str;

print($fcase."\n");

$lcase=lcfirst $str;

print($lcase."\n");


#convert the Lowercase

$Lowercase=lc $str;

print($Lowercase."\n");

#chop -get the last letter
$ch=chop $str;

print($ch."\n");

Arguments are passed by reference

#!/usr/bin/perl -w
$v = 32;
print ("Value before calling the function: " . $v . "\n");
fun1($v);
print ("Value after calling the function: " . $v . "\n");
# end of program
sub fun1
{
print ("Value within the function: " . $_[0] . "\n");
$_[0] <<= 1; # shifts all bits left by 1 (like * 2)
print ("Value within the function, again: " . $_[0] . "\n");
}

Write methods in perl

Find the maximum value of the array

#!/usr/bin/perl -w

# find the maximum value of the array

$maxVal=maximum(2,10,1,9,3,12);
print("\n\nThe Maximum is : $maxVal \n");

sub maximum
{

$val=$_[0];

foreach $my(@_)
{
if($my > $val)
{
$val=$my;
print("\n Max value change to $val");
}
}
return $val;
}

last, next, redo and labels in control structures

#!/usr/bin/perl -w

for($i=0; $i<10; $i++)
{
print($i."\n");

#last will end up the loop
if($i == 7)
{
last;
}

#next
if($i == 5)
{
next;
}

}

print("\nSecond loop ...\n");

for($i=0; $i<10; $i++)
{
print($i."\n");

#next
if($i == 5)
{
next;
}

}
print("\nThird loop ...\n");

# put the label out
out:for($i=0; $i<3; $i++)
{
print($i."\n");

In:for($j=0; $j<3; $j++)
{
print($j."\n");

if($j==1)
goto out;
}
}

Friday, October 17, 2008

Find and Replace

Replace the word new with the word old. /g is used to search globally. /i is used to make case insensitive search.

#!/usr/bin/perl -w

while($_=STDIN)
{
chomp($_);

if (s/new/old/gi)
{
print($_ ."\n");
}
}


If we want to replace only the word new we can use following code.

#!/usr/bin/perl -w

while($_=STDIN)
{
chomp($_);

if (s/\bnew\b/old/gi)
{
print($_ ."\n");
}

}

Anchors

Allow to fix the pattern into the specific part.

Example: \b is limiting the pattern into the word boundary.

#!/usr/bin/perl -w

print("Please Enter a sentence: \n");

while($_=)
{
if(/sky\b/)
{
print("Sky is at the end");
}

if(/\bsky/)
{
print("sky must at the beginning\n");
}

if(/sky\B/)
{
print("sky is not in boundries \n");
}

}



/B can use to find the sentences with that word is not at the end

Pattern Matching

There are some special read only variables
$&: matched part of the string
$`: preceding part
$’: following part

Example:
#!/usr/bin/perl -w

print("please Enter word to find out\n");
$a=;

chomp($a);

print("Please ENter the sentence \n");

while ($_=)
{
chomp($_);


if(/$a/)
{
print("\n The word is find in the sentence $_ \n");

print("The matching part is ".$&. "\n");

print("Before the matching : $`\n");

print("After the matching : $'");

}
}

Thursday, October 16, 2008

Metacharacters

^ matches at start of line
$ matches at end of line
. matches any single character except newline (\n)
* matches zero or more of previous character or group
? matches zero or more of previous character or group
+ matches one or more of previous character or group
.* as many characters as you like


Program to illustrate above regular expressions

#!/usr/bin/perl

#should be
while(STDIN)
{

if(/^hello/g)
{
print("hello is at the beginning \n")
}

if(/Bye$/g)
{
print("Bye is at the end \n")
}


if(/A?/g)
{
print("This has zero and one A with regular Expression'?'\n")
}

if(/A*/g)
{
print("This has zero and one A with regular Expression '*' \n")
}

if(/A+/g)
{
print("This has one or more A \n")
}

}

Tuesday, October 14, 2008

More about Arrays

Get Array length
#!/usr/bin/perl -w

@myarray=(1,2,3,4,5,6,7,8,9);

$len=@myarray;

print("length of the Array : $len");


@ARGV is the array that contains the command line arguments.
example:
#!/usr/bin/perl -w

print("First Argument is : $ARGV[0]\n");

print("First Argument is : $ARGV[1]\n");

print("First Argument is : $ARGV[2]\n");

print("First Argument is : $ARGV[3]\n");

File Handling

#!/usr/bin/perl -w

open (INFILE, "first.txt") or die "Input file: $!";
open (OUTF, ">second.txt") || die "Output file: $!";

while ( INFILE ) {
#Replace the "hi" with "Hello"
s/hi/Hello/g;
#print the input stream into the file "second.txt"
print OUTF $_;
}

Arrays and foreach loop

#!/usr/bin/perl -w

#Define array
@myArray=(1,2,3,4,5,6,7);

#foreach loop
foreach $myVal(@myArray)
{
print($myVal);
print("\n");
}

#To reverse the array
print("Reverse Array \n");
foreach $revVal(reverse @myArray)
{
print($revVal);
print("\n");
}

#Directly print Array

print("@myArray \n");

Welcome to Perl!!

Write Hello World program using perl.

#!/usr/bin/perl -w

print("Hello World \n");


#! -> Gives specifies which perl to use
# -> Comments
-w -> This flag gives more warnings about possible errors.

Next Program


#!/usr/bin/perl -w

$ln=0;
# Should be while()
while(STDIN)
{

$ln++;

$len=length($_);

if($len > 15)
{
print("Line $ln has $len Characters\n");

}
}


Above program will print the lines which have more the 15 characters. Variables in perl start with $, @ or % sign. $ is for that normal variables (scalar), @ is for the arrays and % is for the associative arrays.

$_ is defaultly allocated for the input stream.

Now we extend above program little bit.

#!/usr/bin/perl -w

$ln=0;

#should be while()
while(STDIN)
{

$ln++;

$len=length($_);

if($len > 15)
{
print("Line $ln has $len Characters\n");

}

if(/hello/)
{
print("Line $ln has the word hello");

}
}


Put if statement to identify the sentences with the word "hello"