Professional Documents
Culture Documents
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
Day 1 :
For details :
“Cpan.org” – Inbuilt modules are present , you can use from here.
Scripting :
Types of data :
$ scalar
@ list
% associate array
#!C:/PERL/bin/PERL51~1.EXE –w
$name=<STDIN>
CHOP:
Arithmetic operation :
So when you give a string say “abc” , its value converts to ZERO ,
As ‘+’ operator converts to numerical.
DOCUMENTTYPE 2 (22)
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
Concatenating :
$concatenate="firstword"."secondword";
print "$concatenate\n";
‘.’ DOT operator is concatenation , and has HIGHER PRIORITY for DOT than ADDITION (+)
#!C:/Perl/bin/PERL51~1.EXE –w
print "my first element is : $myArray[0]\n"; # $ is used here , as one at a time is taken , hence scalar
$#myArray --- HASH itself is an operator that gives the LAST INDEX value of
an array.
SLICING :
DOCUMENTTYPE 3 (22)
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
@array1=@myArray[0,2,4];
That is , it will take elements pointing by listed indexs , so its an array.
@array1=@myArray[0,2,3];
# creating a array from an array , when LHS is $ , it can store only ONE element
What if LHS is scalar ie. $ then ?? but RHS is an array, what will happen .
If @ is used in LHS , even for SINGLE element , you can in future add more elements
$Scalar=@myArray[0,1,3];
print "whats this : $Scalar\n"; # takes only LAST index value element.
$length=@myArray;
or :
LOOPS :
#!C:/Perl/bin/PERL51~1.EXE -w
#Loops
# WHILE loop
$i=1;
while($i<=5)
{
print "i is $i\n";
++$i;
}
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
@myArray=(10,20,30,40,50);
#$i=0;
while ($i<=$#myArray)
{
print "index is $i and value is $myArray[$i]\n";
++$i;
}
@arraycity=(Delhi,Bombay,Bangalore);
foreach $c (@arraycity)
{
print "List of cities after impeding is $c"."_KN"."\n";
}
#!C:/Perl/bin/PERL51~1.EXE -w
#String functions
$str1="bangalore";
print "my string is $str1\n"; #to show current string
$x=uc($str1);
print "x is $x\n"; # this does not update exsisting string , only give new value
@empName=(Sush,Saha,RAM,DEEpu);
print "Names of employess at present @empName\n";
{
$e=uc($e).":".length($e);
DOCUMENTTYPE 5 (22)
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
print join("\n",@empName);
JOIN :
Join function creates a single scalar with array elements delimiting with specified chatercter .
SPLIT :
Create
Access
Dlete
Add
Replace
PUSH POP
SHIFT or UNSHIFT
SPLICE :
Insert
Delete
Replace
splice (@numlist,$i,1);
splice (@numlist,$i,1,"replaced"); # if NO value is specified , then it DELETES the value
COMPARISIONS:
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
@x=reverse(@num1);
DAY 2 :
@a1=(1,2);
print "array is @a1 \n";
@b1=push (@a1,3);
print "now array is @b1 \n"; #PUSH fuction RETURNS the length of the updated array.
print "now array is @b1 \n"; #PUSH fuction RETURNS the length of the updated array.
($x,@a2)=@a1; # stores x= first element and @a2 = all other elements in RHS array
($x,$y)=@a1 ; # stores x= first element , y = second element
(@a3,$z)=@a1; # stores a3 has all array , and z = null
while (<STDIN>)
{
chop($_);
#If you use CHOMP , error , as it chomps off even ENTER key , not realising its END of array
push(@num,$_);
}
DOCUMENTTYPE 7 (22)
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
HASH :
SYNTAX 1
: always use FLOWER BRACKET when defining individually .
$empID{100}="044-8467";
$empID{101}="022-841367";
$empID{102}="033-238467";
$empID{103}="012-843367";
Syntax 2 :
%myHash=(“A”=>65,”B”=>34,”C”=>22,”D”=>87);
Print “A asciss code is : $myHash{A} \n”;
Key functionality :
@keylist= keys %empID; # use KEY functionality to get all keys , and push to an array
Values functionalty :
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
SUBROUTINES :
DISPLAY :
May pass the values , or may not pass the values or arguments.
sub display {
print "control is in display function \n";
print "display :@_ \n"; # by default , any arguments passed into a function , is stored is an array
}
display(10);
display("hello","how");
print "List with array return is @result \n" ; #this takes full array as one big SCALAR , and prints , that
why printing HASH is circus with many brackets.
print "Odd number list using PRINT ".AddOdd(@myArray)."\n"; #this concatenation can only PRINT
ONE SCALAR value , so LENGTH of Odd list is shown
END # Need not CALL the function END , it WILL execute anyways if its defined under END
{
print "End of script\n";
}
AUTOLOAD :
DOCUMENTTYPE 9 (22)
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
AUTOLOAD { # AUTOLOAD will have an inbuilt scalar variable ,when something is not defined ,
Autoload executes
print " this is Autoload $AUTOLOAD \n";
}
Program :
AUTOLOAD { # AUTOLOAD will have an inbuilt scalar variable ,when something is not defined ,
Autoload executes
print " this is Autoload $AUTOLOAD \n";
}
sub display {
show(); # since SHOW is not defined , or commented , it will show AUTOLOAD value
Regular Expressions :
Applys on strings
FORMAT : $myString=~m/pattern/
Above code return 1 if $myString consists of pattern you are looking for
Otherwise it will return null or zero.
$result=$myString=~m/pattern/
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
if($myString2=~m/^S/)
$myString2=~m/^[aieou]/i/)
[] a charater class
+previous character, check wether previous occurs miimun once , max ay time ( mandatory )
*Applicable previous, check wheather , check weather previous occurs , min ZERO , max One time
( optional)
/[0-9][0-9][0-9]/
xxx-xxxxxxx
DOCUMENTTYPE 11 (22)
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
x indicates numbers
\^[0-9]{3}-[0-9]{10}$/
Or
\^\d{3}-\d{10}$/
/^.[aeiou]/ this means , first character is DOT ( can be anything, number or alphabet )
From second chart should be vowel
/^…[aeiou]/ this means , first 3 characters is 3 DOT’s ( can be anything, number or alphabet
)
From second chart should be vowel
.*[0-9]$/
/^[a-z]
Here “DOT and STAR “ means DOT can have any character , then STAR can be any number of
charecters.
/a.*b.*c/
Escape characters :
\bwork\b/i
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
DAY 3 :
Global Pattern
@a=$mystr=~m/[0-9][0-9]/g;
^ - start with
$ - end with
+ - min once , max anything
“*” – zero or max once ( optional ) , it might or might not be there .
? – min zero or max once
“. “- one character
\b – word boundry
\s – space
() – bound ry
\d can be used instead of [0-9]
\w can be used instead of valid alphabet , which matches all these : [0-9][a-z][A-Z] and “_”
underscore.
() boundry – starts and ends with same , used for BACk reference
Older explanation :
“+previous character, check wether previous occurs miimun once , max ay time ( mandatory )
*Applicable previous, check wheather , check weather previous occurs , min ZERO , max One time
( optional)
() boundry :
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
Like :
$mystr=~/^{[a-z]).*\1/ means
Like :
So now check if string STARTS with “S” and ends with “S”,
As “S” is the first alphabet in you string .
So :
$x=$str=~/^([a-z]).*\1$/;
$mystr= "sush made shaus"; # try matching s in first & last , and U matches second and
second last charcter
$x=$mystr=~/^([a-z])([a-z]).*\2\1$/g; # so here , first bracket stores "s" next bracket stores "u" ,
so \1 stores "s" , \2 stores "u" ,
So you have interchanged the order of alphabets while matching the ENDing of string.
Negation :
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
print "@a"; # this takes all charcters that is NOT a NUMBER , and "ig" together makes global
and case sensitive
SUBSTITUTION :
$str=~s/my/BANG/; # this replaces "my" with "BANG" and keeps all extra charaters as it is
# check if multiple spaces are seen , remove and keep only ONE space
$str=~s/\s+/ /g; # if you give"\s" it types it , so hust give a SPACE to replace by a space
print " now string is:$str\n";
~s/\b(\w+)(e)\b/\2\1/g;
Use \1 or $1 , \2 or $2
$1 and $2 stores only the PATTERN and not the actual data .
So Output will be :
REFERENCES :
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
REF function :
to find WHAT is format of the data in that address, so that you can perform functions based on
the FORMAT
if($x eq "HASH")
#ref only understands SCALAR,ARRAY,HASH words to find WHAT is format of the data in
that address
{
print "href is reference HASH\n";
Equal to :
less than lt
greater gt
greter than and equal to : ge
less than or equal : le
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
@array1=(1,2,3,4);
$aref=\@array1;
Use {} flower bracket to make the code more visible and neat
To find LENGTH :
$len1=@array1;
print "length is $len1 \n";
give an ARRAY into a SCALAR , it always takes the LENGTH , ( not the first element of the
array into scalar !!!!)
QUOTES :
#%myHash=("apple","red","grapes","green");
%myHash=qw(apple red grapes green); # use "qw" if all are STRINGS , else have to QUOTE
each element manually
#if you need not perform any arithmetic operations then you can QUOTE
that too, such as emp ID
Usually in big data size, you will any way work only on the data ,
So why wate a variable by naming an Array or Hash ,
So simply create data directly by using a Reference .
#@a1=(1,3,4); # so you are SAVING a variable a1 , as you are anyways refrening only to that
array block , and not the NAME!
#$ref1=\@a1;
DOCUMENTTYPE 17 (22)
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
$ref2=[3,4,2];
#[] square brackets creates an anonymous ARRAY /LIST
block
print "ref2 is $ref2";
#Anonymous HASH
$ref={"A",65,"B",66};
#{} flower brackets creates an anonymous HASH block
print "hash refrence is $ref\n";
#complex arrays
my @myarray=([2,3,4],"java",[3,7,9],"excel");
one array which has refrence array or strings inside one array.
--++++++++++++++++++++++++++++
#complex arrays
my @myarray=([2,3,4],"java",[3,7,9],"excel");
$x=ref($myarray[0]);
print "x is $x \n";
#get output as :
#index 0 : elements 2,3,4
#index 1 : elements java
#index 2 : elements 3,7,9
#index 3 : elements excel
$ref1=\@myarray;
#foreach (@{$ref1})
DOCUMENTTYPE 18 (22)
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
#{
#print "Index is $_ and element is @{$ref1}[$_]";
#}
$x=0;
#foreach (@myarray)
if (ref($_) eq "ARRAY") # here is data type is ARRAY , then $_ will hold the word ARRAY
{
print "$x is : @{$_} \n";
}
else
{
print "$x is : $_ \n";
}
++$x;
++++++++++++++++++++++++++++++++++
Complex HASH :
print "@{$myHash{$_}}"; # this prints the ARRAY that is pointed by the reference key
@ {$myHash{$_}} @ the rate lists out all the strings in that above pointed array .
NOTE :
REALLY SCARY – Want to try “Use Anonymous HASH on LSH , and COMPLEX HASH , with
anonymous HASH & Array’s inside on RHS . ( You ‘ll go MAD I m sure !!! )
FILE Handling :
By default its READ mode, if no symbol is preceeded before the file name.
Regular Expressions are extensively used in File Handling , as you usually use to find a
pattern and retrieve or write accordingly .
DOCUMENTTYPE 19 (22)
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
“<” READ
“>”WRITE, erase all exsitng data
“>>” append , add to existing data
“+<” read and write mode , if file exits,then write to it .(cant CREATE a file)
“>+” write plus read mode,if file does not exits,it CREATES file,but OVERWRITTES
“>>+” append and read mode, if file does not exits,it CREATES file,but adds at the LAST.
++++++++++++++++++++++++++++++++++++++++++++++
Open(FH,”D:\\flower.txt”) || die “file cannot open \n”;
While (<FH>)
{
print $_;
}
Close(FH);
++++++++++++++++++++++++++
Print “Hello”;
Actually means
Print STDOUT “Hello”;
STDOUT throws display to screen – it’s a Handler
You can define any File Handler in any name you want .
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
sub uppercase {
my $r=uc($_[0]);
print "result is $r \n";
sub lowercase {
my $r=lc($_[0]);
print "result is $r \n";
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
+++++++++++++++++++++++++++++
If the Pacxkage file is not in the SAME FOLDER AS YOUR “.PL” FILE,then there
is a inbuilt function “@INC”
Gets pathsof where all PERL is present ,
So when you say :
USE Converter ,
This statement actually invokes @INC ,
Which inturns looks for the “ConveterPackage.pm” file in all the PATHS defined
inside @INC,
If It dint find it under any of those , then throws error ,
Telling it dint find the file !
Goto “cpan.org”
Goto PERL Module ->
CPAN search -> look for IxHash (This is used to SORT an array)
C:\>build test this is to simply check if BUILD has been complied or not !
C:\>build install this INSTALLS the build contents
TypeUnitOrDepartmentHere
TypeYourNameHere TypeDateHere
Building Tie-IxHash
Installing C:\Perl\site\lib\Tie\IxHash.pm
Installing C:\Perl\man\man3\Tie.IxHash.3
Installing C:\Perl\html\site\lib\Tie\IxHash.html
$foo{c} = 3;
$foo{b} = 2;
$foo{a} = 1;
print keys(%foo);