PERL

 These scripts will have an extension of .pl (perl file ) or .pm (perl packaged module)

perl -v

perl -e <perl code>  ## interpreted program execution

perl  scripr_file.pl ## executes the file with scripts

perl- e "HelloWorld\n"'

#!/usr/bin/perl   #shebang will be at the beginning of script

./file_script.pl  # can be execute  with 755 file permissions

each line must end with ;

ways of commenting 

Use # to comment a line

=head and  =cut ;  first head can be some name and =cut is ending, no spacing before =

<<SOMENAME   SOMENAME




my $doc = <<"DOC";

This program illustrates commenting

DOC

print $doc,"\n";


White space Quotes and Escape sequences

White spaces are ignored but inside quotes will be treated as it is.

Double quotes have special meaning but single quotes are not  ; in single quotes if we write any special character it looses its meaning. 

Use back slash to escape any character in double quotes  "\$\@"


Datatype 

There are 3 data type  1)Scalars ->$

                                    2)Arrays ->@

                                    3)Hashes ->%

#create a scalar

$string="Perl";

#create a array

@SkillName=("Perl","Python",5,3);

#create a ashes

%skillsExp =( "Perl"=>5,

                        "Python"=>2);

print "$string \n@SkillName\n",%skillsExp,"\"


Scalars :

Scalar is a variable which can hold an integer, a string, a float, etc

No specific type mentioned like integer, string (or) float 

Numeric Scalar, String Scalar, floating scalar, etc

Declaring a scalar ,symbol $, $scalarNum = "Tutorialspoint"

Scalars operation:

Arthematic operations (+,-)

Concatenation operation    dot(or)comma i used for concatenation strings

Repetition operation  x is used for repeating the scalar ex: $scalarNum x 5

vString -Scalar declaration using ASCII numbers

#Numeric scalar

$numScalar =100

#String scalar

$strScalar = "Perl"

#vString

$vString = v85.78.73.88

print $numScalar."-".$strScalar."-".$vString."\n";

print "=" x 10,"/n"


Arrays

An array is a list of having scalars of anydatatype , Array is created with the symbol  @(or)qw

Elements can be accessed by index starting  from 0 (or) by range operator (..),   Array size can be known by 2 ways ,  1)scalar @<Array Name>  2)$#Array+1

Functions to modify array  

push()    appends an element at the end of array.     syntax :push(@<ArrayName>,<element>)

 unshift ()   appends an element at the starting of array.     syntax: unshift(@<ArrayName>,<element>)

pop()    Removes element at the end of array    syntax: pop(@<ArrayName>)

shift()     Removes element at the starting of array.    syntax: shift(@ArrayName)

 


my @skills = ("Perl",5,"Python",2,"Java",4);

print "@skills\n";


####accesing the elements of Array

print "First elements: $skills[0]\n";

print "Last element:$skills[-1]\n";

print "Last but one element: $skills[-2]\n";

print "Elements from 2 to 4 index:@skills[2..4]\n";

print "Element from 2 to last index:@skills[2..$#skills]\n";

print "Last 3 elements of an array:@skills[$#skills-2..$#skills]\n";


#Declaring a numbered array with range operator

my @experiences = (1..10);

print "@experiences\n";

##array size

print "Array Size firt way:",scalar @experiences,"\n";

print "Array size second way:", $#experiences +1,"\n";


#function to add or delete element in an array

#inserting element at the end

push(@experiences, "ADD");

print "@experiences\n";


##inserting element at the beginning 

unshift(@experiences, "Begin");

print "@experiences\n";


##removing element at the end 

my $popped = pop(@experiences) ;

print  "@experiences\n" ;

print  "$popped\n";


##removing elements from the beginning 

my $popped = shift(@experiences);

print "@experiences";

print "Popped string: $popped\n";


###Silicing & Splice function of arrays

Slicing : Extracting elements with indexes from an array Syntax: @<ArrayName>[..]

Splicing: Replace elements, Removeelements from last.  Syntax: splice(@ArrayName>,OFFSET,Length.List) 

###Silicing & Splice function of arraays


my @marks = (65,76,89,90,55,44);

print "@marks[2..5]\n";

splice(@marks,2,3,98..100);

print "@marks\n";

my @lastThree = splice(@marks, -3);

print "@lastThree\n";

###split and join 

Split: convert  string to an array depending on a delimiter. Syntax: split("delimiter".<String>)

Join: Convert array to string . Syntax: convert array to string. Syntax: join("delimiter",@<ArrayName>)

my $experiences = "Perl=Python=Java=Unix=Jython=DNS";

my @experiences = split("=", $experiences);

print "@experiences\n";

my $experiences = join("|",@experiences);

print "$experiences\n";


Sort Array ,$[ and Merging

Sort : sort() function sorts an array based on ASCII     Syntax: sort(@<ArrayName>)

$[    : Modify the first index number of an array

Merging : Two or more arrays can be merged  

###Sort Array ,$ and Merging

my @experiences = ("Perl","Python","Java","c","c++","jython");

my @experiences = sort(@experiences);

print "@experiences\n";


$[ = 2;

print "$experiences[1]\n";

my @arr1 = (1..5);

my @arr2 = (6..10);

my @arr = (@arr1,@arr2);

print "@arr\n";

####Hashes#########

Hash is a set having key value pairs , % symbol used to create a hash. syntax   %<HahName>=(key1 =>value1,key2=> value2);

=> symbol used for relative value to a key .Accessing  value from a key syntax:$<Hash Name>{<key>}

Exits() function: Syntax  exists $<HashName>{<key>}

Hash Size:  Extract keys or values into an array and get the size of array

Adding Key value pair: Syntax  : $<HashName>{<key>}=<value>

Deleting a pair:  Syntax: delete $<HashName>{<key>}


#########

my %skillsExperiences = (Unix => 5,Perl => 5, Python =>2, Java=>1);

print $skillsExperiences{"Unix"},"\n";

my @skillsExperiencesKeys = keys %skillsExperiences;

print "Keys: @skillsExperiencesKeys\n";

my @skillsExperiencesValues = values %skillsExperiences;

print "Values: @skillsExperiencesValues\n";

if (exists($skillsExperiences{"LDAP"})) {

print "LDAP Exists!!\n";

}

else {

print "LDAP Not Exist!!\n";

}


if (!exists($skillsExperiences{"Java"})) {

print "Java Not Exists!!\n";

}

else {

print "Java Exist!!\n";

}


print scalar @skillsExperiencesKeys,"\n";

print scalar @skillsExperiencesValues,"\n";


$skillsExperiences{"Oracle"} = 2;

print "thi is last line    ".$skillsExperiences{"Oracle"},"\n";


delete $skillsExperiences{"Java"};

if (!exists($skillsExperiences{"Java"})) {

print "Java Not Exists!!\n";

}

else {

print "Java Exist!!\n";

}

#####Chomp and Chop#####

chomp() : Removes newline at the end of a scalar.

chop(): Removes last character from the end 


print "Enter a kill :";

$input = <STDIN>;

print "$input";

chmop($input)

print "$input"

################################################

@skills=("Perl\n","Python\n","Java\n");

print "@skills\n";

chop(@skills);

print "@skills\n";


@skillsNames=("Perl-","Python-","Java-");


print "@skillNames\n";

chop(@skillNames);

print "@skillNames\n";

####################

%skillsExperiences = (Perl=>"5\n",Python=>"2\n",Unix=>"6\n");

print %skillsExperiences,"\n";

chop(%skillsExperiences);

print %skillsExperiences, "\n";


%skillsExperiences =(Perl=>"5-", Python=> "2-", Unix =>"6-");


print %skillsExperiences, "\n";

chop(%skillsExperiences);

print %skillsExperiences, "\n";

##############  Map and GREP########################################

 map()    Evaluates a block or expression on each element of array and returns an array or hash.

syntax:  map{Expreion}@<Array Name>

Ex:{1,2,3,4,5} ->get square of numbers

Result:(1,4,9,16,25)

grep()     Evaluates a block or expression and return an array of  having  elements which are evaluated to true.

Syntax:  grep{Expression} @<Array Name>

Ex:(1,2,3,4,5) -> get the numbers >3

Result: (4,5)

 

 

 

#################################################

 Grep command examples
#!/usr/bin/perl  
 @myNames = ('jacob','Radhashyam','vivek','vikram','Alexander','Jyothi',9873,4532);
 #@grepNames = grep(/[a-z]$/, @myNames);
 #@grepNames = grep(/m$/, @myNames);
# @grepNames = grep(/^v/, @myNames); #return which has v
 #@grepNames = grep(!/^v/, @myNames);  #return which has no v
 @grepNames = grep(!/\d/, @myNames); #return decimal value
#@grepNames = grep(/[a-z]$/, @myNames); rint("@myNames\n"); #return which has a-z characters
print("@grepNames\n");


>>>>>>>>>Regular Expresions<<<<<<<<<<<<<<<<#
#>>>>>pattern matching
#--Split function is used to cut a string into smaller sections or pieces, There are different criteria to split a string  (space) (slash"/") (comma",");
$line = "this is a sentance" ;
$line1= "i,will,kill,you";
@array = split(/ /, $line);
@array1 = split(/,/, $line1);
print("$line\n");
print("$line1\n");
print("@array[0]\n");
print("@array1[2]\n");

###>>>Match Operator:-(=~):Test whether a perticular pattern appears in a character string   $syntax:m/string/ various operators to print text based on matched pattern:- 

>> $& - contains the entire matched string
#>>  $` - contains everything before the matched string
#>>  $' - contains everything after the matched string
$string = "perl tutorial by VLSI engineer";
$string =~ m/by/;
print "Before: $`\n";
print "After: $'\n";
####>subtitution operator:-
#extension of the match operator that allows you to replace the text matched with some newtext, syntax:- s/pattern/replacement/;
$string = "Vlsi engineers are rare to find";
print "initial string: $string\n";
$string =~ s/rare/easy/;
print "final string: $string\n";
###>transliterate operation :substution operator will match only firt occurrence and replace it.Transliterate operator replaces all occurances. syntaxtr/pattern/replacement/;
$string ="100100" ;
$string =~ s/0/9/;
 print "$string\n";
$string =~ tr/0/9/;
 print "$string\n";  

########>>Data extraction using regular expression #####
$text = "Benefits of eating healthy food intake nourishes both our phyical and mental health and helps us stay active for many years. Oddly enough , 19.2.1894 ,weekly and avoid making the process dull repetitive. 64.582295 ,look your food minimize eating from outside.";

($date) = ($text =~ /(\d+\.\d+\.\d+)/);
print"date:$date\n";

($phone) = ($text =~ /(\d{2}\.\d{6})/);
print "phone: $phone\n";












 



 


Comments