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

What's hot (19)

PDF
List,tuple,dictionary
nitamhaske
 
PPT
PHP array 2
Mudasir Syed
 
PPTX
Chap 3php array part1
monikadeshmane
 
PDF
Php array
Nikul Shah
 
PPT
Php array
Core Lee
 
PPTX
Chap1introppt2php(finally done)
monikadeshmane
 
PPTX
PHP Functions & Arrays
Henry Osborne
 
PDF
PHP Unit 4 arrays
Kumar
 
PPTX
How to Create an Array & types in PHP
Ajit Sinha
 
PDF
Sorting arrays in PHP
Vineet Kumar Saini
 
PPT
Php Using Arrays
mussawir20
 
PPT
Class 4 - PHP Arrays
Ahmed Swilam
 
PPTX
7. tuples, set &amp; dictionary
PhD Research Scholar
 
PPT
Arrays in PHP
Compare Infobase Limited
 
PDF
学生向けScalaハンズオンテキスト
Opt Technologies
 
PPTX
An Introduction to Tuple List Dictionary in Python
yashar Aliabasi
 
PDF
学生向けScalaハンズオンテキスト part2
Opt Technologies
 
PDF
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 
List,tuple,dictionary
nitamhaske
 
PHP array 2
Mudasir Syed
 
Chap 3php array part1
monikadeshmane
 
Php array
Nikul Shah
 
Php array
Core Lee
 
Chap1introppt2php(finally done)
monikadeshmane
 
PHP Functions & Arrays
Henry Osborne
 
PHP Unit 4 arrays
Kumar
 
How to Create an Array & types in PHP
Ajit Sinha
 
Sorting arrays in PHP
Vineet Kumar Saini
 
Php Using Arrays
mussawir20
 
Class 4 - PHP Arrays
Ahmed Swilam
 
7. tuples, set &amp; dictionary
PhD Research Scholar
 
学生向けScalaハンズオンテキスト
Opt Technologies
 
An Introduction to Tuple List Dictionary in Python
yashar Aliabasi
 
学生向けScalaハンズオンテキスト part2
Opt Technologies
 
Python Variable Types, List, Tuple, Dictionary
Soba Arjun
 

Viewers also liked (11)

PPTX
Data structure in perl
sana mateen
 
PDF
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
Bhavsingh Maloth
 
PDF
Perl Memory Use 201207 (OUTDATED, see 201209 )
Tim Bunce
 
PDF
YAPC::Europe 2008 - Mike Astle - Profiling
lokku
 
PDF
Practical SystemTAP basics: Perl memory profiling
Lubomir Rintel
 
ODP
Advanced Perl Techniques
Dave Cross
 
PPT
Perl tutorial
Manav Prasad
 
ODP
Profiling with Devel::NYTProf
bobcatfish
 
PDF
Perl Memory Use 201209
Tim Bunce
 
PDF
Perl Memory Use - LPW2013
Tim Bunce
 
Data structure in perl
sana mateen
 
WEB PROGRAMMING UNIT V BY BHAVSINGH MALOTH
Bhavsingh Maloth
 
Perl Memory Use 201207 (OUTDATED, see 201209 )
Tim Bunce
 
YAPC::Europe 2008 - Mike Astle - Profiling
lokku
 
Practical SystemTAP basics: Perl memory profiling
Lubomir Rintel
 
Advanced Perl Techniques
Dave Cross
 
Perl tutorial
Manav Prasad
 
Profiling with Devel::NYTProf
bobcatfish
 
Perl Memory Use 201209
Tim Bunce
 
Perl Memory Use - LPW2013
Tim Bunce
 
Ad

Similar to Introduction to perl_control structures (20)

DOCX
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
PPT
Perl training-in-navi mumbai
vibrantuser
 
PPTX
Perl slid
pacatarpit
 
PPT
Introduction to Perl
NBACriteria2SICET
 
PPT
Crash Course in Perl – Perl tutorial for C programmers
Gil Megidish
 
PPT
PERL.ppt
Farmood Alam
 
PDF
Marc’s (bio)perl course
Marc Logghe
 
PPT
Perl Basics with Examples
Nithin Kumar Singani
 
ODP
Perl Introduction
Marcos Rebelo
 
PPT
Introduction to perl scripting______.ppt
nalinisamineni
 
PPT
Introduction to perl_ a scripting language
Vamshi Santhapuri
 
PDF
Perl programming language
Elie Obeid
 
PDF
Perl intro
Swapnesh Singh
 
PDF
Learning Perl 6
brian d foy
 
ODP
Introduction to Perl - Day 2
Dave Cross
 
PDF
perl_lessons
tutorialsruby
 
PDF
perl_lessons
tutorialsruby
 
PPTX
PERL PROGRAMMING LANGUAGE Basic Introduction
johnboladevice
 
PERL for QA - Important Commands and applications
Sunil Kumar Gunasekaran
 
Perl training-in-navi mumbai
vibrantuser
 
Perl slid
pacatarpit
 
Introduction to Perl
NBACriteria2SICET
 
Crash Course in Perl – Perl tutorial for C programmers
Gil Megidish
 
PERL.ppt
Farmood Alam
 
Marc’s (bio)perl course
Marc Logghe
 
Perl Basics with Examples
Nithin Kumar Singani
 
Perl Introduction
Marcos Rebelo
 
Introduction to perl scripting______.ppt
nalinisamineni
 
Introduction to perl_ a scripting language
Vamshi Santhapuri
 
Perl programming language
Elie Obeid
 
Perl intro
Swapnesh Singh
 
Learning Perl 6
brian d foy
 
Introduction to Perl - Day 2
Dave Cross
 
perl_lessons
tutorialsruby
 
perl_lessons
tutorialsruby
 
PERL PROGRAMMING LANGUAGE Basic Introduction
johnboladevice
 
Ad

Recently uploaded (20)

PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Biography of Daniel Podor.pdf
Daniel Podor
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 

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.