SlideShare uma empresa Scribd logo
1 de 17
DAY 4:
Control Structures
Vamshi Krishna .S
 A statement block is a sequence of statements, enclosed in
matching curly braces. It looks like this:
 {
 first_statement;
 second_statement;
 third_statement;
 ...
 last_statement; # last statement semicolon is optional.
 }
 print "how old are you? ";
 $a = <STDIN>;
 chomp($a);
 if ($a < 18) {
 print "So, you're not old enough to vote, eh?n";
 } else {
 print "Old enough! Cool! So go vote!n";
 $voter++; # count the voters for later
 }
 print "how old are you? ";
 $a = <STDIN>;
 chomp($a);
 unless ($a < 18) {
 print "Old enough! Cool! So go vote!n";
 $voter++;
 Arrays are a special type of variable that
store list style data types. Each object of the
list is termed an element and elements can
either be a string, a number, or any type of
scalar data including another variable.
 Array variables have the same format as
scalar variables except that they are
prefixed by an @ symbol.
 Arrays can also hold arrays.
  array variable is a variable
which is a list of scalars (ie
numbers and strings). Array
variables have the same
format as scalar variables
except that they are
prefixed by an @ symbol.
 @var =
(value1..valueN);
 @var = (“str1”,”str2”)
 ($a, $b) = @alphabets;
# $a and $b are the
first two elements of
@alphabets array
 #!/usr/bin/perl
 main(@ARGV);
 my $days =qw^Sun Mon
Tue Wed Thu Fri Sat^;
 Print @days;
 My $days=31;
 Print $days;
# It is valid to use the same
name for scalars and array
types.
 My @array1 =(1,2,3);
 My @array2 = (@array1 ,4,5,6);
 Print “@array2”; # prints 1 2 3 4 5 6
 Print $array2[2]; # prints 3
 Print scalar @array2
 # Here the scalar will contain the value 6 , as
the total no’of elements in the array2 is
equal to 6
#!/usr/bin/perl
use warnings;
use strict;
say “input a number”;# functionality same as
“print” , adds a new line character at the end
My chomp($choice =<STDIN>);
print qw(BMW Ferrari McLaren Jaguar )
[$choice]);
OUTPUT:- McLaren
 If you input a decimal value .. Choice = 1.2 then perl will
round if off and will output Ferrari.
 If you input a negative number.. Choice = -1 then perl
starts counting backwards from the end of the list.
LIST Slices
 instead of putting a scalar value [$choice].. We can also
print out multiple values in the list by putting a list of
index values
 (20,30,99,15,22,13,56,76,34)[2,5,7]
 This can also be used on strings.
 What happens when you assign a array to a scalar?
 @array1 = qw(a b c d);
 $scalar1 = @array1;
 Print “array cotents:” @array1 # prints with no spaces
 Print “array contents: @array1” # prints with spaces
 @array1 = (1,2,3,4,5)
 Print @array1 “n”;
 Print “@array1n”
 $scalar1 = “@array1n”; is Same as $scalar = “1 2 3 4 5n”;
 $scalar = @array1; is same as $scalar = 5 # as the total elements
in array is 5
 (1 .. 10)
 ( -9 .. 9)
 (a .. z)
 Cant mix up the list of elements list .. (a .. 5) or (-1 .. B) etc.,
 Always the right-hand element should be higher than the left-handed
element.
 We can mix up the Slices and ranges.. For effective programming
 @coins = qw(Quarter Dime Nickel Penny);
 @slicecoins = @coins[0,2];
 Say "@slicecoins"; # prints 0th
index value “Quarter” and 2nd
value “Nickel”
with Spaces b/w strings.
 $a = (@array)[3];
 My @array1=
 Print $array1[1]
 @lang = qw(java python perl c);
 My $element;# declating scalar var for iteration
 For $element (@lang)
 { print $element “n”;
 }
 For <iterator> (<list of array>) BLOCK
 If we don’t supply an iterator of our own.. Perl supplies a
special variable S_ which is often used in perl functins as
default value
 @array1 = (1,2,3,4);
 Print “before @array1 n”
 For (@array1) {$_ * = 10}
 Print “after @array1n”
 #!/usr/bin/perl
 my @coins =
("Quarter","Dime","Nickel");
 # ADD ELEMENTS
 push(@coins, "Penny");
 print "@coins";
 print "<br />";
 unshift(@coins, "Dollar");
 print "@coins";
 # REMOVE ELEMENTS
 pop(@coins);
 print "@coins";
 shift(@coins);
 Adding elements is a breeze, we
use the following functions to
add/remove and elements:
 push() - adds an element to the
end of an array.
 unshift() - adds an element to
the beginning of an array.
 pop() - removes the last element
of an array.
 shift() - removes the first
element of an array.

Mais conteúdo relacionado

Mais procurados

PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
Kumar
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 

Mais procurados (19)

List,tuple,dictionary
List,tuple,dictionaryList,tuple,dictionary
List,tuple,dictionary
 
PHP array 2
PHP array 2PHP array 2
PHP array 2
 
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
 
Php array
Php arrayPhp array
Php array
 
Php array
Php arrayPhp array
Php array
 
Chap1introppt2php(finally done)
Chap1introppt2php(finally done)Chap1introppt2php(finally done)
Chap1introppt2php(finally done)
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
 
How to Create an Array & types in PHP
How to Create an Array & types in PHP How to Create an Array & types in PHP
How to Create an Array & types in PHP
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
7. tuples, set &amp; dictionary
7. tuples, set &amp; dictionary7. tuples, set &amp; dictionary
7. tuples, set &amp; dictionary
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
6. list
6. list6. list
6. list
 
学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト学生向けScalaハンズオンテキスト
学生向けScalaハンズオンテキスト
 
An Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in PythonAn Introduction to Tuple List Dictionary in Python
An Introduction to Tuple List Dictionary in Python
 
学生向けScalaハンズオンテキスト part2
学生向けScalaハンズオンテキスト part2学生向けScalaハンズオンテキスト part2
学生向けScalaハンズオンテキスト part2
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 

Destaque

Destaque (11)

Data structure in perl
Data structure in perlData structure in perl
Data structure in perl
 
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTHWEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
 
Perl Memory Use 201207 (OUTDATED, see 201209 )
Perl Memory Use 201207 (OUTDATED, see 201209 )Perl Memory Use 201207 (OUTDATED, see 201209 )
Perl Memory Use 201207 (OUTDATED, see 201209 )
 
YAPC::Europe 2008 - Mike Astle - Profiling
YAPC::Europe 2008 - Mike Astle - ProfilingYAPC::Europe 2008 - Mike Astle - Profiling
YAPC::Europe 2008 - Mike Astle - Profiling
 
Practical SystemTAP basics: Perl memory profiling
Practical SystemTAP basics: Perl memory profilingPractical SystemTAP basics: Perl memory profiling
Practical SystemTAP basics: Perl memory profiling
 
Advanced Perl Techniques
Advanced Perl TechniquesAdvanced Perl Techniques
Advanced Perl Techniques
 
Perl tutorial
Perl tutorialPerl tutorial
Perl tutorial
 
Profiling with Devel::NYTProf
Profiling with Devel::NYTProfProfiling with Devel::NYTProf
Profiling with Devel::NYTProf
 
Perl Memory Use 201209
Perl Memory Use 201209Perl Memory Use 201209
Perl Memory Use 201209
 
Perl Memory Use - LPW2013
Perl Memory Use - LPW2013Perl Memory Use - LPW2013
Perl Memory Use - LPW2013
 
Optička rešetka 17
Optička rešetka 17Optička rešetka 17
Optička rešetka 17
 

Semelhante a Introduction to perl_control structures

PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
rhshriva
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
rhshriva
 

Semelhante a Introduction to perl_control structures (20)

Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
 
Scalar data types
Scalar data typesScalar data types
Scalar data types
 
Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01
 
perl_lessons
perl_lessonsperl_lessons
perl_lessons
 
perl_lessons
perl_lessonsperl_lessons
perl_lessons
 
PERL for QA - Important Commands and applications
PERL for QA - Important Commands and applicationsPERL for QA - Important Commands and applications
PERL for QA - Important Commands and applications
 
Arrays
ArraysArrays
Arrays
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
 
Unit 1-perl names values and variables
Unit 1-perl names values and variablesUnit 1-perl names values and variables
Unit 1-perl names values and variables
 
Complete Overview about PERL
Complete Overview about PERLComplete Overview about PERL
Complete Overview about PERL
 
Php Chapter 2 3 Training
Php Chapter 2 3 TrainingPhp Chapter 2 3 Training
Php Chapter 2 3 Training
 
Bioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introductionBioinformatica 06-10-2011-p2 introduction
Bioinformatica 06-10-2011-p2 introduction
 
Dades i operadors
Dades i operadorsDades i operadors
Dades i operadors
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
 
Perl intro
Perl introPerl intro
Perl intro
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Perl slid
Perl slidPerl slid
Perl slid
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 

Introduction to perl_control structures

  • 2.  A statement block is a sequence of statements, enclosed in matching curly braces. It looks like this:  {  first_statement;  second_statement;  third_statement;  ...  last_statement; # last statement semicolon is optional.  }
  • 3.  print "how old are you? ";  $a = <STDIN>;  chomp($a);  if ($a < 18) {  print "So, you're not old enough to vote, eh?n";  } else {  print "Old enough! Cool! So go vote!n";  $voter++; # count the voters for later  }
  • 4.  print "how old are you? ";  $a = <STDIN>;  chomp($a);  unless ($a < 18) {  print "Old enough! Cool! So go vote!n";  $voter++;
  • 5.
  • 6.
  • 7.
  • 8.  Arrays are a special type of variable that store list style data types. Each object of the list is termed an element and elements can either be a string, a number, or any type of scalar data including another variable.  Array variables have the same format as scalar variables except that they are prefixed by an @ symbol.  Arrays can also hold arrays.
  • 9.   array variable is a variable which is a list of scalars (ie numbers and strings). Array variables have the same format as scalar variables except that they are prefixed by an @ symbol.  @var = (value1..valueN);  @var = (“str1”,”str2”)  ($a, $b) = @alphabets; # $a and $b are the first two elements of @alphabets array  #!/usr/bin/perl  main(@ARGV);  my $days =qw^Sun Mon Tue Wed Thu Fri Sat^;  Print @days;  My $days=31;  Print $days; # It is valid to use the same name for scalars and array types.
  • 10.  My @array1 =(1,2,3);  My @array2 = (@array1 ,4,5,6);  Print “@array2”; # prints 1 2 3 4 5 6  Print $array2[2]; # prints 3  Print scalar @array2  # Here the scalar will contain the value 6 , as the total no’of elements in the array2 is equal to 6
  • 11. #!/usr/bin/perl use warnings; use strict; say “input a number”;# functionality same as “print” , adds a new line character at the end My chomp($choice =<STDIN>); print qw(BMW Ferrari McLaren Jaguar ) [$choice]); OUTPUT:- McLaren
  • 12.  If you input a decimal value .. Choice = 1.2 then perl will round if off and will output Ferrari.  If you input a negative number.. Choice = -1 then perl starts counting backwards from the end of the list. LIST Slices  instead of putting a scalar value [$choice].. We can also print out multiple values in the list by putting a list of index values  (20,30,99,15,22,13,56,76,34)[2,5,7]  This can also be used on strings.
  • 13.  What happens when you assign a array to a scalar?  @array1 = qw(a b c d);  $scalar1 = @array1;  Print “array cotents:” @array1 # prints with no spaces  Print “array contents: @array1” # prints with spaces  @array1 = (1,2,3,4,5)  Print @array1 “n”;  Print “@array1n”  $scalar1 = “@array1n”; is Same as $scalar = “1 2 3 4 5n”;  $scalar = @array1; is same as $scalar = 5 # as the total elements in array is 5
  • 14.  (1 .. 10)  ( -9 .. 9)  (a .. z)  Cant mix up the list of elements list .. (a .. 5) or (-1 .. B) etc.,  Always the right-hand element should be higher than the left-handed element.  We can mix up the Slices and ranges.. For effective programming  @coins = qw(Quarter Dime Nickel Penny);  @slicecoins = @coins[0,2];  Say "@slicecoins"; # prints 0th index value “Quarter” and 2nd value “Nickel” with Spaces b/w strings.
  • 15.  $a = (@array)[3];  My @array1=  Print $array1[1]
  • 16.  @lang = qw(java python perl c);  My $element;# declating scalar var for iteration  For $element (@lang)  { print $element “n”;  }  For <iterator> (<list of array>) BLOCK  If we don’t supply an iterator of our own.. Perl supplies a special variable S_ which is often used in perl functins as default value  @array1 = (1,2,3,4);  Print “before @array1 n”  For (@array1) {$_ * = 10}  Print “after @array1n”
  • 17.  #!/usr/bin/perl  my @coins = ("Quarter","Dime","Nickel");  # ADD ELEMENTS  push(@coins, "Penny");  print "@coins";  print "<br />";  unshift(@coins, "Dollar");  print "@coins";  # REMOVE ELEMENTS  pop(@coins);  print "@coins";  shift(@coins);  Adding elements is a breeze, we use the following functions to add/remove and elements:  push() - adds an element to the end of an array.  unshift() - adds an element to the beginning of an array.  pop() - removes the last element of an array.  shift() - removes the first element of an array.