SlideShare a Scribd company logo
ARRAYS, LISTS AND HASHES
By
SANA MATEEN
ARRAYS
 It is collections of scalar data items which have an assigned storage space in memory,
and can therefore be accessed using a variable name.
 The difference between arrays and hashes is that the constituent elements of an array are
identified by a numerical index, which starts at zero for the first element.
 array always starts with @, eg: @days_of_week.
 An array stores a collection, and list is a collection, so it is natural to assign a list to an
array. eg.
@rainfall=(1.2, 0.4, 0.3, 0.1, 0, 0 , 0);
This creates an array of seven elements. These can be accessed like
$rainfall[0], $rainfall[1], .... $rainfall[6].
A list can also occur as elements of other list.
@foo=(1,2,3, “string”);
@foobar= (4, 5, @foo, 6);
This gives foobar the value (4,5,1,2,3, “string”,6).
MANIPULATING ARRAYS
 Elements of an array are selected using C like square bracket syntax, eg: $bar=$foo[2].
 The $ and [ ] make it clear that this instance foo is an element of the array foo, not the
scalar variable foo.
 A group of contiguous elements is called a slice, and is accessed using simple syntax.
 @foo[1..3]
 Is the same as the list
($foo[1],$foo[2],$foo[3])
 The slice can be used as the destination of the assignment eg:@foo[1..3]= (“hop”, “skip”,
“jump”);
 Array variables and lists can be used interchangeably in almost any sensible situation:
$front=(“bob”, “carol”, “ted”, “alice”)[0];
@rest=(“bob”, “carol”, “ted”, “alice”) [1..3];
or even
@rest=qw/bob carol ted alice/[1..3];
Elements of an array can be selected by using another array selector.
 @foo =(7, “fred”, 9);
 @bar=(2,1,0);
then
@foo=@foo[@bar];
LISTS
 List is a collection of variables , constants or expressions which is to be
treated as a whole . It is written as comma separated sequence of values.
Eg: “red”, “green”, “blue”
 A list often appears in a script enclosed in round brackets.
(“red”, “green”, “blue”)
 Short hand used in lists: (1..8) and (“A”.. “H”, “O”.. “Z”).
 To save the tedious typing, qw(the quick brown fox)
 Is short hand for : (“the”, “quick”, “brown”, “fox”).
 qw- quote words operator, is an obvious extension of the q and qq operator.
 qw/the quick brown fox/ (or) qw|the quick brown fox|
 The list containing variables can appear as the target of an assignment and/or
as the value to be assigned.
($a , $b , $c)= (1,2,3);
MANIPULATING LISTS
 Perl provides several built-in functions for list manipulation. Three useful ones are a)shift
LIST b)unshift LIST c)push LIST
 a) returns the first item of the list and moves remaining items down reducing the size of the
list by 1.
 b) the opposite of shift: puts the items in LIST at the beginning of ARRAY, moving the
original contents up by the required amount.
 c) push LIST: It is similar to unshift but adds the values in LIST to the end of ARRAY
 ITERATING OVER LISTS
Perl provides a number of mechanisms to achieve this.
I. foreach
II. map
III. grep
 foreach loop: It performs a simple iteration over all the elements of a list.
foreach $item (list){
}
This blocks takes each value from the list and repeats execution.
foreach (@array){
.... #process $_
 map: perl provides an inbuilt function map to create plural forms of words.
 @p1=map $_. ‘s’ , @s;
 general form of map is: map expression, list;
 and map BLOCK list;
 we can also use foreach loop to achieve the same.
@s=qw/cat, dog, rabbit, hamster, rat/;
@p1=();
foreach (@s){
push @p1, $_. ‘s’
}
 grep : In unix grep is used to print all lines of the file which contains an instance of
pattern.
grep pattern file
The perl grep function takes a pattern and a list and returns new list containing all the
elements of the original list that match the pattern.
Eg: @things = (car, bus, cardigan, jumper, carrot);
grep /car/ @things
returns the list
(car,cardigan,carrot)
HASHES
 A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To
refer to a single element of a hash, you will use the hash variable name preceded by a
"$" sign and followed by the "key" associated with the value in curly brackets.
 Here is a simple example of using the hash variables −
 #!/usr/bin/perl
 %data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);
 print "$data{'John Paul'} = $data{'John Paul'}n";
 print "$data{'Lisa'} = $data{'Lisa'}n";
 print "$data{'Kumar'} = $data{'Kumar'}n";
 This will produce the following result −
 $data{'John Paul'} = 45
 $data{'Lisa'} = 30
 $data{'Kumar'} = 40
 CREATING HASHES
we can assign a list to an array, so it is not surprising that we can assign a list of key-value
pairs to a hash.
for example:
%foo= (key1, value1, key2, value2,.....);
alternative syntax is provided using the => operator to associate key-value pairs
%foo =(banana => ‘yellow’ , apple=>’green’ , ...)
Key Value(age)
John Paul 45
Lisa 30
Kumar 40
MANIPULATING HASHES
 Perl provides a number of built-in functions to facilitate manipulation of hashes. If we
have a hash called magic.
keys %magic
Returns a list of the keys of the elements in the hash.
values %magic
These functions provide way to iterate over the elements of hash using foreach:
foreach $key(keys %magic) {
do something with $magic($key)
}
Explicit loop variable is omitted, in which case the anonymous variable $_ will be assumed.
foreach(keys %magic)
{
process $magic($_);
}
An alternative is to use “each” operator which delivers successive key-value pairs from a hash.
while(($key,$value)=each %magic){
...
}
 Other useful operators for manipulating hashes are delete and exists.
 delete $magic($key)
 Removes the elements whose key matches $key from the hash %magic, and
 exists $magic($key)
 Returns true if the hash %magic contains an element whose key matches
$key.common idiomis
exists($h{‘key’})&&do(statements)
To avoid using an if statement.

More Related Content

What's hot

JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
WebStackAcademy
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
malathip12
 
Javascript validating form
Javascript validating formJavascript validating form
Javascript validating form
Jesus Obenita Jr.
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
TheCreativedev Blog
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
Neeru Mittal
 
Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
Web technology lab manual
Web technology lab manualWeb technology lab manual
Web technology lab manual
neela madheswari
 
Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
Marcos Rebelo
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
sana mateen
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
Priya Goyal
 
Php Basico
Php BasicoPhp Basico
Php Basico
Eliecer Cedano
 
Php array
Php arrayPhp array
Php array
Nikul Shah
 
Perl Programming - 01 Basic Perl
Perl Programming - 01 Basic PerlPerl Programming - 01 Basic Perl
Perl Programming - 01 Basic Perl
Danairat Thanabodithammachari
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
shanmukhareddy dasi
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
Nithin Kumar Singani
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
Nisa Soomro
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
Sangharsh agarwal
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Andres Baravalle
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
Arulmurugan Rajaraman
 

What's hot (20)

JavaScript - Chapter 6 - Basic Functions
 JavaScript - Chapter 6 - Basic Functions JavaScript - Chapter 6 - Basic Functions
JavaScript - Chapter 6 - Basic Functions
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
 
Javascript validating form
Javascript validating formJavascript validating form
Javascript validating form
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Web technology lab manual
Web technology lab manualWeb technology lab manual
Web technology lab manual
 
Perl Introduction
Perl IntroductionPerl Introduction
Perl Introduction
 
Unit 1-array,lists and hashes
Unit 1-array,lists and hashesUnit 1-array,lists and hashes
Unit 1-array,lists and hashes
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Php Basico
Php BasicoPhp Basico
Php Basico
 
Php array
Php arrayPhp array
Php array
 
Perl Programming - 01 Basic Perl
Perl Programming - 01 Basic PerlPerl Programming - 01 Basic Perl
Perl Programming - 01 Basic Perl
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
 
Advance oops concepts
Advance oops conceptsAdvance oops concepts
Advance oops concepts
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 

Viewers also liked

Lists, queues and stacks
Lists, queues and stacksLists, queues and stacks
Lists, queues and stacks
andreeamolnar
 
OOP
OOPOOP
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. PatilDiscrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
widespreadpromotion
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
15. STL - Data Structures using C++ by Varsha Patil
15. STL - Data Structures using C++ by Varsha Patil15. STL - Data Structures using C++ by Varsha Patil
15. STL - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
14. Files - Data Structures using C++ by Varsha Patil
14. Files - Data Structures using C++ by Varsha Patil14. Files - Data Structures using C++ by Varsha Patil
14. Files - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patil12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
Intro C# Book
 

Viewers also liked (13)

Lists, queues and stacks
Lists, queues and stacksLists, queues and stacks
Lists, queues and stacks
 
OOP
OOPOOP
OOP
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
 
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. PatilDiscrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil
 
15. STL - Data Structures using C++ by Varsha Patil
15. STL - Data Structures using C++ by Varsha Patil15. STL - Data Structures using C++ by Varsha Patil
15. STL - Data Structures using C++ by Varsha Patil
 
4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
 
14. Files - Data Structures using C++ by Varsha Patil
14. Files - Data Structures using C++ by Varsha Patil14. Files - Data Structures using C++ by Varsha Patil
14. Files - Data Structures using C++ by Varsha Patil
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patil12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patil
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 

Similar to Array,lists and hashes in perl

Lists and arrays
Lists and arraysLists and arrays
Introduction to perl_control structures
Introduction to perl_control structuresIntroduction to perl_control structures
Introduction to perl_control structures
Vamshi Santhapuri
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
Elie Obeid
 
Introduction to perl_lists
Introduction to perl_listsIntroduction to perl_lists
Introduction to perl_lists
Vamshi Santhapuri
 
Scripting3
Scripting3Scripting3
Scripting3
Nao Dara
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
40NehaPagariya
 
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
 
Perl intro
Perl introPerl intro
Perl intro
Kanchilug
 
Intermediate Perl
Intermediate PerlIntermediate Perl
Intermediate Perl
Dave Cross
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
mythili213835
 
Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01
Javier Arturo Rodríguez
 
Marcs (bio)perl course
Marcs (bio)perl courseMarcs (bio)perl course
Marcs (bio)perl course
BITS
 
6. list
6. list6. list
Perl
PerlPerl
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
Ahmed Swilam
 
ruby3_6up
ruby3_6upruby3_6up
ruby3_6up
tutorialsruby
 
ruby3_6up
ruby3_6upruby3_6up
ruby3_6up
tutorialsruby
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
tutorialsruby
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
tutorialsruby
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
tutorialsruby
 

Similar to Array,lists and hashes in perl (20)

Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
 
Introduction to perl_control structures
Introduction to perl_control structuresIntroduction to perl_control structures
Introduction to perl_control structures
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
 
Introduction to perl_lists
Introduction to perl_listsIntroduction to perl_lists
Introduction to perl_lists
 
Scripting3
Scripting3Scripting3
Scripting3
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
 
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
 
Perl intro
Perl introPerl intro
Perl intro
 
Intermediate Perl
Intermediate PerlIntermediate Perl
Intermediate Perl
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
 
Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01
 
Marcs (bio)perl course
Marcs (bio)perl courseMarcs (bio)perl course
Marcs (bio)perl course
 
6. list
6. list6. list
6. list
 
Perl
PerlPerl
Perl
 
Class 5 - PHP Strings
Class 5 - PHP StringsClass 5 - PHP Strings
Class 5 - PHP Strings
 
ruby3_6up
ruby3_6upruby3_6up
ruby3_6up
 
ruby3_6up
ruby3_6upruby3_6up
ruby3_6up
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 

More from sana mateen

Files
FilesFiles
PHP Variables and scopes
PHP Variables and scopesPHP Variables and scopes
PHP Variables and scopes
sana mateen
 
Php intro
Php introPhp intro
Php intro
sana mateen
 
Php and web forms
Php and web formsPhp and web forms
Php and web forms
sana mateen
 
Mail
MailMail
Files in php
Files in phpFiles in php
Files in php
sana mateen
 
File upload php
File upload phpFile upload php
File upload php
sana mateen
 
Regex posix
Regex posixRegex posix
Regex posix
sana mateen
 
Encryption in php
Encryption in phpEncryption in php
Encryption in php
sana mateen
 
Authentication methods
Authentication methodsAuthentication methods
Authentication methods
sana mateen
 
Xml schema
Xml schemaXml schema
Xml schema
sana mateen
 
Xml dtd
Xml dtdXml dtd
Xml dtd
sana mateen
 
Xml dom
Xml domXml dom
Xml dom
sana mateen
 
Xhtml
XhtmlXhtml
Intro xml
Intro xmlIntro xml
Intro xml
sana mateen
 
Dom parser
Dom parserDom parser
Dom parser
sana mateen
 
Unit 1-subroutines in perl
Unit 1-subroutines in perlUnit 1-subroutines in perl
Unit 1-subroutines in perl
sana mateen
 
Unit 1-uses for scripting languages,web scripting
Unit 1-uses for scripting languages,web scriptingUnit 1-uses for scripting languages,web scripting
Unit 1-uses for scripting languages,web scripting
sana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
sana mateen
 
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
sana mateen
 

More from sana mateen (20)

Files
FilesFiles
Files
 
PHP Variables and scopes
PHP Variables and scopesPHP Variables and scopes
PHP Variables and scopes
 
Php intro
Php introPhp intro
Php intro
 
Php and web forms
Php and web formsPhp and web forms
Php and web forms
 
Mail
MailMail
Mail
 
Files in php
Files in phpFiles in php
Files in php
 
File upload php
File upload phpFile upload php
File upload php
 
Regex posix
Regex posixRegex posix
Regex posix
 
Encryption in php
Encryption in phpEncryption in php
Encryption in php
 
Authentication methods
Authentication methodsAuthentication methods
Authentication methods
 
Xml schema
Xml schemaXml schema
Xml schema
 
Xml dtd
Xml dtdXml dtd
Xml dtd
 
Xml dom
Xml domXml dom
Xml dom
 
Xhtml
XhtmlXhtml
Xhtml
 
Intro xml
Intro xmlIntro xml
Intro xml
 
Dom parser
Dom parserDom parser
Dom parser
 
Unit 1-subroutines in perl
Unit 1-subroutines in perlUnit 1-subroutines in perl
Unit 1-subroutines in perl
 
Unit 1-uses for scripting languages,web scripting
Unit 1-uses for scripting languages,web scriptingUnit 1-uses for scripting languages,web scripting
Unit 1-uses for scripting languages,web scripting
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
 
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
 

Recently uploaded

4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
Gino153088
 
Zener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and ApplicationsZener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and Applications
Shiny Christobel
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
ijaia
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
um7474492
 
Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
Dwarkadas J Sanghvi College of Engineering
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
q30122000
 
Open Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surfaceOpen Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surface
Indrajeet sahu
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
CVCSOfficial
 
P5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civilP5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civil
AnasAhmadNoor
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
Paris Salesforce Developer Group
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...
cannyengineerings
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
mahaffeycheryld
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
Prakhyath Rai
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
uqyfuc
 
Accident detection system project report.pdf
Accident detection system project report.pdfAccident detection system project report.pdf
Accident detection system project report.pdf
Kamal Acharya
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
Kamal Acharya
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
Addu25809
 
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICSUNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
vmspraneeth
 

Recently uploaded (20)

4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
4. Mosca vol I -Fisica-Tipler-5ta-Edicion-Vol-1.pdf
 
Zener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and ApplicationsZener Diode and its V-I Characteristics and Applications
Zener Diode and its V-I Characteristics and Applications
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...smart pill dispenser is designed to improve medication adherence and safety f...
smart pill dispenser is designed to improve medication adherence and safety f...
 
Introduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.pptIntroduction to Computer Networks & OSI MODEL.ppt
Introduction to Computer Networks & OSI MODEL.ppt
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
 
Open Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surfaceOpen Channel Flow: fluid flow with a free surface
Open Channel Flow: fluid flow with a free surface
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
 
P5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civilP5 Working Drawings.pdf floor plan, civil
P5 Working Drawings.pdf floor plan, civil
 
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
AI + Data Community Tour - Build the Next Generation of Apps with the Einstei...
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...Pressure Relief valve used in flow line to release the over pressure at our d...
Pressure Relief valve used in flow line to release the over pressure at our d...
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
 
Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...Software Engineering and Project Management - Introduction, Modeling Concepts...
Software Engineering and Project Management - Introduction, Modeling Concepts...
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Accident detection system project report.pdf
Accident detection system project report.pdfAccident detection system project report.pdf
Accident detection system project report.pdf
 
Supermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdfSupermarket Management System Project Report.pdf
Supermarket Management System Project Report.pdf
 
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENTNATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
NATURAL DEEP EUTECTIC SOLVENTS AS ANTI-FREEZING AGENT
 
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICSUNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
UNIT 4 LINEAR INTEGRATED CIRCUITS-DIGITAL ICS
 

Array,lists and hashes in perl

  • 1. ARRAYS, LISTS AND HASHES By SANA MATEEN
  • 2. ARRAYS  It is collections of scalar data items which have an assigned storage space in memory, and can therefore be accessed using a variable name.  The difference between arrays and hashes is that the constituent elements of an array are identified by a numerical index, which starts at zero for the first element.  array always starts with @, eg: @days_of_week.  An array stores a collection, and list is a collection, so it is natural to assign a list to an array. eg. @rainfall=(1.2, 0.4, 0.3, 0.1, 0, 0 , 0); This creates an array of seven elements. These can be accessed like $rainfall[0], $rainfall[1], .... $rainfall[6]. A list can also occur as elements of other list. @foo=(1,2,3, “string”); @foobar= (4, 5, @foo, 6); This gives foobar the value (4,5,1,2,3, “string”,6).
  • 3. MANIPULATING ARRAYS  Elements of an array are selected using C like square bracket syntax, eg: $bar=$foo[2].  The $ and [ ] make it clear that this instance foo is an element of the array foo, not the scalar variable foo.  A group of contiguous elements is called a slice, and is accessed using simple syntax.  @foo[1..3]  Is the same as the list ($foo[1],$foo[2],$foo[3])  The slice can be used as the destination of the assignment eg:@foo[1..3]= (“hop”, “skip”, “jump”);  Array variables and lists can be used interchangeably in almost any sensible situation: $front=(“bob”, “carol”, “ted”, “alice”)[0]; @rest=(“bob”, “carol”, “ted”, “alice”) [1..3]; or even @rest=qw/bob carol ted alice/[1..3]; Elements of an array can be selected by using another array selector.  @foo =(7, “fred”, 9);  @bar=(2,1,0); then @foo=@foo[@bar];
  • 4. LISTS  List is a collection of variables , constants or expressions which is to be treated as a whole . It is written as comma separated sequence of values. Eg: “red”, “green”, “blue”  A list often appears in a script enclosed in round brackets. (“red”, “green”, “blue”)  Short hand used in lists: (1..8) and (“A”.. “H”, “O”.. “Z”).  To save the tedious typing, qw(the quick brown fox)  Is short hand for : (“the”, “quick”, “brown”, “fox”).  qw- quote words operator, is an obvious extension of the q and qq operator.  qw/the quick brown fox/ (or) qw|the quick brown fox|  The list containing variables can appear as the target of an assignment and/or as the value to be assigned. ($a , $b , $c)= (1,2,3);
  • 5. MANIPULATING LISTS  Perl provides several built-in functions for list manipulation. Three useful ones are a)shift LIST b)unshift LIST c)push LIST  a) returns the first item of the list and moves remaining items down reducing the size of the list by 1.  b) the opposite of shift: puts the items in LIST at the beginning of ARRAY, moving the original contents up by the required amount.  c) push LIST: It is similar to unshift but adds the values in LIST to the end of ARRAY  ITERATING OVER LISTS Perl provides a number of mechanisms to achieve this. I. foreach II. map III. grep  foreach loop: It performs a simple iteration over all the elements of a list. foreach $item (list){ } This blocks takes each value from the list and repeats execution. foreach (@array){ .... #process $_
  • 6.  map: perl provides an inbuilt function map to create plural forms of words.  @p1=map $_. ‘s’ , @s;  general form of map is: map expression, list;  and map BLOCK list;  we can also use foreach loop to achieve the same. @s=qw/cat, dog, rabbit, hamster, rat/; @p1=(); foreach (@s){ push @p1, $_. ‘s’ }  grep : In unix grep is used to print all lines of the file which contains an instance of pattern. grep pattern file The perl grep function takes a pattern and a list and returns new list containing all the elements of the original list that match the pattern. Eg: @things = (car, bus, cardigan, jumper, carrot); grep /car/ @things returns the list (car,cardigan,carrot)
  • 7. HASHES  A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To refer to a single element of a hash, you will use the hash variable name preceded by a "$" sign and followed by the "key" associated with the value in curly brackets.  Here is a simple example of using the hash variables −  #!/usr/bin/perl  %data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);  print "$data{'John Paul'} = $data{'John Paul'}n";  print "$data{'Lisa'} = $data{'Lisa'}n";  print "$data{'Kumar'} = $data{'Kumar'}n";  This will produce the following result −  $data{'John Paul'} = 45  $data{'Lisa'} = 30  $data{'Kumar'} = 40  CREATING HASHES we can assign a list to an array, so it is not surprising that we can assign a list of key-value pairs to a hash. for example: %foo= (key1, value1, key2, value2,.....); alternative syntax is provided using the => operator to associate key-value pairs %foo =(banana => ‘yellow’ , apple=>’green’ , ...) Key Value(age) John Paul 45 Lisa 30 Kumar 40
  • 8. MANIPULATING HASHES  Perl provides a number of built-in functions to facilitate manipulation of hashes. If we have a hash called magic. keys %magic Returns a list of the keys of the elements in the hash. values %magic These functions provide way to iterate over the elements of hash using foreach: foreach $key(keys %magic) { do something with $magic($key) } Explicit loop variable is omitted, in which case the anonymous variable $_ will be assumed. foreach(keys %magic) { process $magic($_); } An alternative is to use “each” operator which delivers successive key-value pairs from a hash. while(($key,$value)=each %magic){ ... }
  • 9.  Other useful operators for manipulating hashes are delete and exists.  delete $magic($key)  Removes the elements whose key matches $key from the hash %magic, and  exists $magic($key)  Returns true if the hash %magic contains an element whose key matches $key.common idiomis exists($h{‘key’})&&do(statements) To avoid using an if statement.