SlideShare a Scribd company logo
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++;
Introduction to perl_control structures
Introduction to perl_control structures
Introduction to perl_control structures
 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.

More Related Content

PPT
Introduction to perl_lists
PPTX
Array,lists and hashes in perl
PPTX
Perl names values and variables
PPTX
PHP array 1
PDF
Scripting3
PPTX
Array in php
PDF
Cs3430 lecture 17
PDF
Arrays in PHP
Introduction to perl_lists
Array,lists and hashes in perl
Perl names values and variables
PHP array 1
Scripting3
Array in php
Cs3430 lecture 17
Arrays in PHP

What's hot (19)

PDF
List,tuple,dictionary
PPT
PHP array 2
PPTX
Chap 3php array part1
PDF
Php array
PPT
Php array
PPTX
Chap1introppt2php(finally done)
PPTX
PHP Functions & Arrays
PDF
PHP Unit 4 arrays
PPTX
How to Create an Array & types in PHP
PDF
Sorting arrays in PHP
PPT
Php Using Arrays
PPT
Class 4 - PHP Arrays
PPTX
7. tuples, set &amp; dictionary
PPT
PDF
学生向けScalaハンズオンテキスト
PPTX
An Introduction to Tuple List Dictionary in Python
PDF
学生向けScalaハンズオンテキスト part2
PDF
Python Variable Types, List, Tuple, Dictionary
List,tuple,dictionary
PHP array 2
Chap 3php array part1
Php array
Php array
Chap1introppt2php(finally done)
PHP Functions & Arrays
PHP Unit 4 arrays
How to Create an Array & types in PHP
Sorting arrays in PHP
Php Using Arrays
Class 4 - PHP Arrays
7. tuples, set &amp; dictionary
学生向けScalaハンズオンテキスト
An Introduction to Tuple List Dictionary in Python
学生向けScalaハンズオンテキスト part2
Python Variable Types, List, Tuple, Dictionary
Ad

Viewers also liked (11)

PPTX
Data structure in perl
PDF
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
PDF
Perl Memory Use 201207 (OUTDATED, see 201209 )
PDF
YAPC::Europe 2008 - Mike Astle - Profiling
PDF
Practical SystemTAP basics: Perl memory profiling
ODP
Advanced Perl Techniques
PPT
Perl tutorial
ODP
Profiling with Devel::NYTProf
PDF
Perl Memory Use 201209
PDF
Perl Memory Use - LPW2013
Data structure in perl
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
Perl Memory Use 201207 (OUTDATED, see 201209 )
YAPC::Europe 2008 - Mike Astle - Profiling
Practical SystemTAP basics: Perl memory profiling
Advanced Perl Techniques
Perl tutorial
Profiling with Devel::NYTProf
Perl Memory Use 201209
Perl Memory Use - LPW2013
Ad

Similar to Introduction to perl_control structures (20)

PPTX
Unit 1-array,lists and hashes
PDF
Barcelona.pm Curs1211 sess01
PDF
perl_lessons
PDF
perl_lessons
DOCX
PERL for QA - Important Commands and applications
PDF
Learning Perl 6
PPTX
Unit 1-perl names values and variables
PPTX
Complete Overview about PERL
PPT
Php Chapter 2 3 Training
PPT
Bioinformatica 06-10-2011-p2 introduction
PDF
Dades i operadors
PDF
Lecture 23
PDF
Perl intro
PPTX
Unit 2-Arrays.pptx
PDF
Lecture 22
PPTX
Perl slid
PDF
perl-pocket
Unit 1-array,lists and hashes
Barcelona.pm Curs1211 sess01
perl_lessons
perl_lessons
PERL for QA - Important Commands and applications
Learning Perl 6
Unit 1-perl names values and variables
Complete Overview about PERL
Php Chapter 2 3 Training
Bioinformatica 06-10-2011-p2 introduction
Dades i operadors
Lecture 23
Perl intro
Unit 2-Arrays.pptx
Lecture 22
Perl slid
perl-pocket

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Approach and Philosophy of On baking technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
A Presentation on Artificial Intelligence
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Assigned Numbers - 2025 - Bluetooth® Document
Building Integrated photovoltaic BIPV_UPV.pdf
Encapsulation theory and applications.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Approach and Philosophy of On baking technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
A comparative analysis of optical character recognition models for extracting...
Encapsulation_ Review paper, used for researhc scholars
A Presentation on Artificial Intelligence
sap open course for s4hana steps from ECC to s4
Review of recent advances in non-invasive hemoglobin estimation
NewMind AI Weekly Chronicles - August'25-Week II
Unlocking AI with Model Context Protocol (MCP)
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
The Rise and Fall of 3GPP – Time for a Sabbatical?
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Assigned Numbers - 2025 - Bluetooth® Document

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++;
  • 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.