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)

DOCX
PERL for QA - Important Commands and applications
PPT
Perl training-in-navi mumbai
PPTX
Perl slid
PPT
Introduction to Perl
PPT
Crash Course in Perl – Perl tutorial for C programmers
PPT
PERL.ppt
PDF
Marc’s (bio)perl course
PPT
Perl Basics with Examples
ODP
Perl Introduction
PPT
Introduction to perl scripting______.ppt
PPT
Introduction to perl_ a scripting language
PDF
Perl programming language
PDF
Perl intro
PDF
Learning Perl 6
ODP
Introduction to Perl - Day 2
PDF
perl_lessons
PDF
perl_lessons
PPTX
PERL PROGRAMMING LANGUAGE Basic Introduction
PERL for QA - Important Commands and applications
Perl training-in-navi mumbai
Perl slid
Introduction to Perl
Crash Course in Perl – Perl tutorial for C programmers
PERL.ppt
Marc’s (bio)perl course
Perl Basics with Examples
Perl Introduction
Introduction to perl scripting______.ppt
Introduction to perl_ a scripting language
Perl programming language
Perl intro
Learning Perl 6
Introduction to Perl - Day 2
perl_lessons
perl_lessons
PERL PROGRAMMING LANGUAGE Basic Introduction

Recently uploaded (20)

PDF
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
PDF
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
PDF
LMS bot: enhanced learning management systems for improved student learning e...
PPTX
Module 1 Introduction to Web Programming .pptx
PDF
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
PDF
SaaS reusability assessment using machine learning techniques
PDF
Planning-an-Audit-A-How-To-Guide-Checklist-WP.pdf
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
PDF
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
DOCX
Basics of Cloud Computing - Cloud Ecosystem
PDF
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
PDF
Electrocardiogram sequences data analytics and classification using unsupervi...
PDF
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
PDF
Connector Corner: Transform Unstructured Documents with Agentic Automation
PDF
A symptom-driven medical diagnosis support model based on machine learning te...
PDF
Early detection and classification of bone marrow changes in lumbar vertebrae...
PDF
Auditboard EB SOX Playbook 2023 edition.
PDF
Human Computer Interaction Miterm Lesson
PDF
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
PDF
Build Real-Time ML Apps with Python, Feast & NoSQL
A hybrid framework for wild animal classification using fine-tuned DenseNet12...
IT-ITes Industry bjjbnkmkhkhknbmhkhmjhjkhj
LMS bot: enhanced learning management systems for improved student learning e...
Module 1 Introduction to Web Programming .pptx
CXOs-Are-you-still-doing-manual-DevOps-in-the-age-of-AI.pdf
SaaS reusability assessment using machine learning techniques
Planning-an-Audit-A-How-To-Guide-Checklist-WP.pdf
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
Transform-Quality-Engineering-with-AI-A-60-Day-Blueprint-for-Digital-Success.pdf
Basics of Cloud Computing - Cloud Ecosystem
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
Electrocardiogram sequences data analytics and classification using unsupervi...
The-Future-of-Automotive-Quality-is-Here-AI-Driven-Engineering.pdf
Connector Corner: Transform Unstructured Documents with Agentic Automation
A symptom-driven medical diagnosis support model based on machine learning te...
Early detection and classification of bone marrow changes in lumbar vertebrae...
Auditboard EB SOX Playbook 2023 edition.
Human Computer Interaction Miterm Lesson
Transform-Your-Supply-Chain-with-AI-Driven-Quality-Engineering.pdf
Build Real-Time ML Apps with Python, Feast & NoSQL

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.