37
Practical Approach to PERL Rakesh Mukundan

Practical approach to perl day1

Embed Size (px)

DESCRIPTION

Learn perl with exampleswww.rakeshmukundan.in

Citation preview

Page 1: Practical approach to perl day1

Practical Approach to PERL

Rakesh Mukundan

Scripting Language Uses an interpreter to run the code No compilation needed just run it Interpreted line by line Fast to learn and program Easy debugging Every user is a developer )

PERLbull Practical Extraction and Report Languagebull Also known as Practically Everything Really Likeable

bull Began as the result of one mans frustration and by his own account inordinate laziness

bull Perl is free The full source code and documentation are free to copy compile print and give away

Why Learn Perl

bull Perl is easy and it makes life easybull Its open sourcebull Lots of tested modules available for re-usebull You are too lazy to do mechanical repetitive

work

Do You Have Perl Installed

bull Execute the following command in a shellndash perl -v

Installationbull UNIX and Linux Available with installation CD or

standard repositories if not installed by default

bull Windows Many flavors are available with and without IDEs free and proprietary etcndash Active State Perlndash Strawberry Perl

Your First Perl Programbull Open MyFirstProgrampl from the examples directory

with your favorite text editor ( geditvimnotepad++ etc )

bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl

bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl

Under the hood

A perl statement terminated with a semi-colon

Comment

Location of interpreter

Variables

bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar

sign ($)ndash Ex $name$password$ip_address

bull No need to define a variable explicitly use directly

bull A scalar can hold data of any type be it a string a number or whatnot

Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val

Numerical Operations

Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2

Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 2: Practical approach to perl day1

Scripting Language Uses an interpreter to run the code No compilation needed just run it Interpreted line by line Fast to learn and program Easy debugging Every user is a developer )

PERLbull Practical Extraction and Report Languagebull Also known as Practically Everything Really Likeable

bull Began as the result of one mans frustration and by his own account inordinate laziness

bull Perl is free The full source code and documentation are free to copy compile print and give away

Why Learn Perl

bull Perl is easy and it makes life easybull Its open sourcebull Lots of tested modules available for re-usebull You are too lazy to do mechanical repetitive

work

Do You Have Perl Installed

bull Execute the following command in a shellndash perl -v

Installationbull UNIX and Linux Available with installation CD or

standard repositories if not installed by default

bull Windows Many flavors are available with and without IDEs free and proprietary etcndash Active State Perlndash Strawberry Perl

Your First Perl Programbull Open MyFirstProgrampl from the examples directory

with your favorite text editor ( geditvimnotepad++ etc )

bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl

bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl

Under the hood

A perl statement terminated with a semi-colon

Comment

Location of interpreter

Variables

bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar

sign ($)ndash Ex $name$password$ip_address

bull No need to define a variable explicitly use directly

bull A scalar can hold data of any type be it a string a number or whatnot

Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val

Numerical Operations

Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2

Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 3: Practical approach to perl day1

PERLbull Practical Extraction and Report Languagebull Also known as Practically Everything Really Likeable

bull Began as the result of one mans frustration and by his own account inordinate laziness

bull Perl is free The full source code and documentation are free to copy compile print and give away

Why Learn Perl

bull Perl is easy and it makes life easybull Its open sourcebull Lots of tested modules available for re-usebull You are too lazy to do mechanical repetitive

work

Do You Have Perl Installed

bull Execute the following command in a shellndash perl -v

Installationbull UNIX and Linux Available with installation CD or

standard repositories if not installed by default

bull Windows Many flavors are available with and without IDEs free and proprietary etcndash Active State Perlndash Strawberry Perl

Your First Perl Programbull Open MyFirstProgrampl from the examples directory

with your favorite text editor ( geditvimnotepad++ etc )

bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl

bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl

Under the hood

A perl statement terminated with a semi-colon

Comment

Location of interpreter

Variables

bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar

sign ($)ndash Ex $name$password$ip_address

bull No need to define a variable explicitly use directly

bull A scalar can hold data of any type be it a string a number or whatnot

Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val

Numerical Operations

Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2

Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 4: Practical approach to perl day1

Why Learn Perl

bull Perl is easy and it makes life easybull Its open sourcebull Lots of tested modules available for re-usebull You are too lazy to do mechanical repetitive

work

Do You Have Perl Installed

bull Execute the following command in a shellndash perl -v

Installationbull UNIX and Linux Available with installation CD or

standard repositories if not installed by default

bull Windows Many flavors are available with and without IDEs free and proprietary etcndash Active State Perlndash Strawberry Perl

Your First Perl Programbull Open MyFirstProgrampl from the examples directory

with your favorite text editor ( geditvimnotepad++ etc )

bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl

bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl

Under the hood

A perl statement terminated with a semi-colon

Comment

Location of interpreter

Variables

bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar

sign ($)ndash Ex $name$password$ip_address

bull No need to define a variable explicitly use directly

bull A scalar can hold data of any type be it a string a number or whatnot

Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val

Numerical Operations

Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2

Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 5: Practical approach to perl day1

Do You Have Perl Installed

bull Execute the following command in a shellndash perl -v

Installationbull UNIX and Linux Available with installation CD or

standard repositories if not installed by default

bull Windows Many flavors are available with and without IDEs free and proprietary etcndash Active State Perlndash Strawberry Perl

Your First Perl Programbull Open MyFirstProgrampl from the examples directory

with your favorite text editor ( geditvimnotepad++ etc )

bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl

bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl

Under the hood

A perl statement terminated with a semi-colon

Comment

Location of interpreter

Variables

bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar

sign ($)ndash Ex $name$password$ip_address

bull No need to define a variable explicitly use directly

bull A scalar can hold data of any type be it a string a number or whatnot

Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val

Numerical Operations

Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2

Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 6: Practical approach to perl day1

Installationbull UNIX and Linux Available with installation CD or

standard repositories if not installed by default

bull Windows Many flavors are available with and without IDEs free and proprietary etcndash Active State Perlndash Strawberry Perl

Your First Perl Programbull Open MyFirstProgrampl from the examples directory

with your favorite text editor ( geditvimnotepad++ etc )

bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl

bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl

Under the hood

A perl statement terminated with a semi-colon

Comment

Location of interpreter

Variables

bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar

sign ($)ndash Ex $name$password$ip_address

bull No need to define a variable explicitly use directly

bull A scalar can hold data of any type be it a string a number or whatnot

Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val

Numerical Operations

Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2

Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 7: Practical approach to perl day1

Your First Perl Programbull Open MyFirstProgrampl from the examples directory

with your favorite text editor ( geditvimnotepad++ etc )

bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl

bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl

Under the hood

A perl statement terminated with a semi-colon

Comment

Location of interpreter

Variables

bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar

sign ($)ndash Ex $name$password$ip_address

bull No need to define a variable explicitly use directly

bull A scalar can hold data of any type be it a string a number or whatnot

Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val

Numerical Operations

Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2

Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 8: Practical approach to perl day1

Under the hood

A perl statement terminated with a semi-colon

Comment

Location of interpreter

Variables

bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar

sign ($)ndash Ex $name$password$ip_address

bull No need to define a variable explicitly use directly

bull A scalar can hold data of any type be it a string a number or whatnot

Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val

Numerical Operations

Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2

Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 9: Practical approach to perl day1

Variables

bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar

sign ($)ndash Ex $name$password$ip_address

bull No need to define a variable explicitly use directly

bull A scalar can hold data of any type be it a string a number or whatnot

Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val

Numerical Operations

Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2

Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 10: Practical approach to perl day1

Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val

Numerical Operations

Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2

Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 11: Practical approach to perl day1

Numerical Operations

Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2

Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 12: Practical approach to perl day1

Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 13: Practical approach to perl day1

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 14: Practical approach to perl day1

Strings

An string of characters no size limit ldquoHello Worldrdquo

Can be specified using single quotes() or double quotes(ldquo)

Strings can be concatenated using dot () operator

No operations are possible inside a single quoted string

ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 15: Practical approach to perl day1

Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 16: Practical approach to perl day1

Print Function

Most commonly used perl functionCan print a variable or string to

consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT

Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 17: Practical approach to perl day1

User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 18: Practical approach to perl day1

Calculator Program

Ask user to enter two numbers Do all the numerical operations mentioned in

previous slide and print the output

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 19: Practical approach to perl day1

String Operations

index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR

length(EXPR) Returns the length in characters of the value of EXPR

rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR

substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 20: Practical approach to perl day1

Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)

bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 21: Practical approach to perl day1

Arrays

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 22: Practical approach to perl day1

Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 23: Practical approach to perl day1

Working With Arrays

bull An array element can be indexed as $MyFirstArray[1]

bull push() - adds an element to the end of an array

bull unshift() - adds an element to the beginning of an array

bull pop() - removes the last element of an arraybull shift() - removes the first element of an

array

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 24: Practical approach to perl day1

A phonebook

bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004

bull How do you represent such a listndash Lookup by names

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 25: Practical approach to perl day1

Hashesbull Hash is a like a phone book which has names

and corresponding phone numbersbull Each element in a hash will have a key and

valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 26: Practical approach to perl day1

Hashes

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 27: Practical approach to perl day1

Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo

bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo

bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)

bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 28: Practical approach to perl day1

Contd

bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 29: Practical approach to perl day1

Program Control

bull Ifbull Whilebull Forbull Foreach

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 30: Practical approach to perl day1

ifbull Conditional statement to check if a criteria is

met or notbull The syntax is

bull if(condtion) code to executebull if($var1 ==5)

bull print ldquovariable is 5nrdquobull

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 31: Practical approach to perl day1

ifelse Else is the compliment of if Execute code if the condition is not met Syntax

if(condition) code for condition met else code for condition false

if($var1==5) print ldquovariable value is 5nrdquo

else

Print ldquovariable value is not 5nrdquo

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 32: Practical approach to perl day1

elsif

if(condition1) Code to execute

elsif(condition2) Code to executeElse

Code to execute

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 33: Practical approach to perl day1

whilebull Loop while the condition is truebull while($countlt5)

bull Print ldquoCont$countnrdquobull $count++

bull bull While(1) makes an infinite loopbull Flow Control

bull next go to the next iterationbull lastend while loop

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 34: Practical approach to perl day1

forbull A for loop counts through a range of

numbers running a block of code each time it iterates through the loop

bull for(initial condition $increment) code to execute

bull for($count=0$countlt11$count++)

bull Print ldquoCount $count nrdquobull

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 35: Practical approach to perl day1

foreachbull Used for iterating over an array or hashbull foreach(MyArray)

bull print ldquoElement $_nrdquobull

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 36: Practical approach to perl day1

Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37
Page 37: Practical approach to perl day1

Strict UsageBy default perl doesnt need any variable to be

declared before useSimple spelling mistakes in variable names can

lead to hours of code debuggingBy using the strict methodperl will strictly ask

you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash

  • Slide 1
  • Slide 2
  • Slide 3
  • Slide 4
  • Slide 5
  • Slide 6
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • Slide 16
  • Slide 17
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Slide 23
  • Slide 24
  • Slide 25
  • Slide 26
  • Slide 27
  • Slide 28
  • Slide 29
  • Slide 30
  • Slide 31
  • Slide 32
  • Slide 33
  • Slide 34
  • Slide 35
  • Slide 36
  • Slide 37