Download presentation
Presentation is loading. Please wait.
Published byCaitlin Ferguson Modified over 9 years ago
1
Online Counseling Resource YCMOU ELearning Drive… School of Architecture, Science and Technology Yashwantrao Chavan Maharashtra Open University, Nashik – 422222, India
2
OC-SBI083-CP2-02 Introduction Programmes and Courses SEP – SBI083-CP2-02
3
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.3 Credits Academic Inputs by Sonali Alkari MSc (Botany), P.G. D.C. Bio-Informatics sonalisa_alkari@yahoo.com
4
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.4 How to Use This Resource Counselor at each study center should use this presentation to deliver lecture of 40-60 minutes during Face-To-Face counseling. Discussion about students difficulties or tutorial with assignments should follow the lecture for about 40-60 minutes. Handouts (with 6 slides on each A4 size page) of this presentation should be provided to each student. Each student should discuss on the discussion forum all the terms which could not be understood. This will improve his writing skills and enhance knowledge level about topics, which shall be immensely useful for end exam. Appear several times, for all the Self-Tests, available for this course. Student can use handouts for last minutes preparation just before end exam.
5
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.5 Learning Objectives After studying this module, you should be able to: Define HASH and its Literal Representation Describe key and value function of HASH Describe The each and delete Function Describe Hash slices
6
School of Science and Technology, Online Counseling Resource… What Is a Hash - 1 A hash is like the array, in that it is a collection of scalar data, with individual elements selected by some index value. Unlike a list array, the index values of a hash are not small non negative integers, but instead are arbitrary scalars. These scalars (called keys) are used later to retrieve the values from the array. In older documentation, hashes were called "associative arrays," but we got tired of a seven-syllable word for such a common item, so we replaced it with a much nicer one-syllable word. © 2007, YCMOU. All Rights Reserved.6
7
School of Science and Technology, Online Counseling Resource… What Is a Hash - 2 The elements of a hash have no particular order. Consider them instead like a deck of filing cards. The top half of each card is the key, and the bottom half is the value. Each time you put a value into the hash, a new card is created. Later, when you want to modify the value, you give the key, and Perl finds the right card. So, really, the order of the cards is immaterial. In fact, Perl stores the cards (the key-value pairs) in a special internal order that makes it easy to find a specific card, so Perl doesn't have to look through all the pairs to find the right one. © 2007, YCMOU. All Rights Reserved.7
8
School of Science and Technology, Online Counseling Resource… What Is a Hash - 3 A hash is a collection of zero or more pairs of scalar values, called keys and values. The values are indexed by the keys. For example $baseball_stadiums {‘phillies’}=‘veterans stadium’; A hash called %baseball_stadiums and key phillies to which you want to assign the value Veterans stadium. Note that a single hash value is referenced by a $ instead of a % at the beginning of hash name. You can assign several keys and values to a hash by placing their scalar value in a lisr, separated by commas and surrounded by a pair parentheses. © 2007, YCMOU. All Rights Reserved.8
9
School of Science and Technology, Online Counseling Resource… What Is a Hash - 4 For instance, you can assign a hash the empty list: % hash= ( ); You can also assign one or more scalar key-value pairs. % gene_by_name=(‘gene1’, ‘AACCGGTT’,’gene2’, ’CCTTAAGG’); This is another way to do the same thing, which makes the key-value pair more readily apparent. % gene_by_name=( ‘gene1’=> ‘AACCGGTT’, ’gene2’ => ’CCTTAAGG’ ); To get the value associated with a particular key, precede the hash name with a $ and follow it with a pair of curly braces { } containing the scalar value of the key: $genes_by_name{‘gene’} This returns the value‘AACCGGTT’ © 2007, YCMOU. All Rights Reserved.9
10
School of Science and Technology, Online Counseling Resource… What Is a Hash - 5 Hashes can be used in lot of different situations, especially when your data is in the form of key-value or you need to look up the value of a key fast. © 2007, YCMOU. All Rights Reserved.10
11
School of Science and Technology, Online Counseling Resource… Literal Representation of a Hash-1 You may wish to access the hash as a whole, either to initialize it or to copy it to another hash. Perl doesn't really have a literal representation for a hash, so instead it unwinds the hash as a list. Each pair of elements in the list (which should always have an even number of elements) defines a key and its corresponding value. This unwound representation can be assigned into another hash, which will then recreate the same hash. © 2007, YCMOU. All Rights Reserved.11
12
School of Science and Technology, Online Counseling Resource… Literal Representation of a Hash-2 In other words: @fred_list = %fred; # @fred_list gets ("aaa","bbb","234.5",456.7) %barney = @fred_list; # create %barney like %fred %barney = %fred; # a faster way to do the same %smooth = ("aaa","bbb","234.5",456.7); # create %smooth like %fred, from literal values The order of the key-value pairs is arbitrary in this unwound representation and cannot be controlled. Even if you swap some of the values around and create the hash as a whole, the returned unwound list is still in whatever order Perl has created for efficient access to the individual elements. © 2007, YCMOU. All Rights Reserved.12
13
School of Science and Technology, Online Counseling Resource… Hash Function: key function-1 The keys(%hashname) function yields a list of all the current keys in the hash %hashname. In other words, it's like the odd-numbered (first, third, fifth, and so on) elements of the list returned by unwinding %hashname in an array context, and in fact, returns them in that order. If there are no elements to the hash, then keys returns an empty list. For example, using the hash from the previous examples: $fred{"aaa"} = "bbb"; $fred{234.5} = 456.7; @list = keys(%fred); # @list gets ("aaa",234.5) or # (234.5,"aaa") © 2007, YCMOU. All Rights Reserved.13
14
School of Science and Technology, Online Counseling Resource… Hash Functions : key function -2 foreach $key (keys (%fred)) { # once for each key of %fred print "at $key we have $fred {$key}\n"; # show key and value} This example also shows that individual hash elements can be interpolated into double- quoted strings. You cannot interpolate the entire hash, however. In a scalar context, the keys function gives the number of elements (key-value pairs) in the hash. For example, you can find out whether a hash is empty: © 2007, YCMOU. All Rights Reserved.14
15
School of Science and Technology, Online Counseling Resource… Hash Function: key function-3 if (keys(%somehash)) { # if keys() not zero:...; # array is non empty } #... or...while (keys(%somehash) < 10) {...; # keep looping while we have fewer than 10 elements } In fact, merely using %somehash in a scalar context will reveal whether the hash is empty or not: if (%somehash) { # if true, then something's in it # do something with it } © 2007, YCMOU. All Rights Reserved.15
16
School of Science and Technology, Online Counseling Resource… Hash Function: Value function The values(%hashname) function returns a list of all the current values of the %hashname, in the same order as the keys returned by the keys(%hashname) function. As always, the parentheses are optional. For example: %lastname = (); # force %lastname empty $lastname{"fred"} = "flintstone"; $lastname{"barney"} = "rubble"; @lastnames = values(%lastname); # grab the values At this point @lastnames contains either ("flintstone", "rubble") or ("rubble", "flintstone"). © 2007, YCMOU. All Rights Reserved.16
17
School of Science and Technology, Online Counseling Resource… The each Function-1 To iterate over (that is, examine every element of) an entire hash, use keys, looking up each returned key to get the corresponding value. Although this method is frequently used, a more efficient way is to use each(%hashname), which returns a key-value pair as a two-element list. On each evaluation of this function for the same hash, the next successive key-value pair is returned until all the elements have been accessed. © 2007, YCMOU. All Rights Reserved.17
18
School of Science and Technology, Online Counseling Resource… The each Function-2 When there are no more pairs, each returns an empty list. So, for example, to step through the %lastname hash from the previous example, do something like this: while (($first,$last) = each(%lastname)) { print "The last name of $first is $last\n"; } Assigning a new value to the entire hash resets the each function to the beginning. Adding or deleting elements of the hash is quite likely to confuse each (and possibly you as well). © 2007, YCMOU. All Rights Reserved.18
19
School of Science and Technology, Online Counseling Resource… The delete Function So far, with what you know, you can add elements to a hash, but you cannot remove them (other than by assigning a new value to the entire hash). Perl provides the delete function to remove hash elements. The operand of delete is a hash reference, just as if you were merely looking at a particular value. Perl removes the key-value pair from the hash. For example: %fred = ("aaa","bbb",234.5,34.56); # give %fred two elements delete $fred{"aaa"}; # %fred is now just one key-value pair © 2007, YCMOU. All Rights Reserved.19
20
School of Science and Technology, Online Counseling Resource… Hash Slices-1 Like an array variable (or list literal), a hash can be sliced to access a collection of elements instead of just one element at a time. For example, consider the bowling scores set individually: $score{"fred"} = 205; $score{"barney"} = 195; $score{"dino"} = 30; This seems rather redundant, and in fact can be shortened to: ($score{"fred"},$score{"barney"},$score{"dino"}) = (205,195,30); But even these seems redundant. Let's use a hash slice: @score{"fred","barney","dino"} = (205,195,30); © 2007, YCMOU. All Rights Reserved.20
21
School of Science and Technology, Online Counseling Resource… Hash Slices-2 Much shorter. We can use a hash slice with variable interpolation as well: @players = qw(fred barney dino); print "scores are: @score{@players}\n"; Hash slices can also be used to merge a smaller hash into a larger one. In this example, the smaller hash takes precedence in the sense that if there are duplicate keys, the value from the smaller hash is used: %league{keys %score} = values %score; Here, the values of %score are merged into the %league hash. This is equivalent to the much slower operation: %league = (%league, %score); # merge %score into %league © 2007, YCMOU. All Rights Reserved.21
22
School of Science and Technology, Online Counseling Resource… Hash Slice-3 In a scalar context, the keys function gives the number of elements (key-value pairs) in the hash. For example, you can find out whether a hash is empty: if (keys(%somehash)) { # if keys() not zero:...; # array is non empty}#... or...while (keys(%somehash) < 10) {...; # keep looping while we have fewer than 10 elements} In fact, merely using %somehash in a scalar context will reveal whether the hash is empty or not: if (%somehash) { # if true, then something's in it # do something with it} © 2007, YCMOU. All Rights Reserved.22
23
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.23 What You Learn-1… You have learnt : A hash is like the array, in that it is a collection of scalar data, with individual elements selected by some index value. A hash is a collection of zero or more pairs of scalar values, called keys and values. The keys(%hashname) function yields a list of all the current keys in the hash %hashname. The values(%hashname) function returns a list of all the current values of the %hashname, in the same order as the keys returned by the keys(%hashname) function.
24
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.24 What You Learn-2… You have learnt : To iterate over (that is, examine every element of) an entire hash, a more efficient way is to use each(%hashname), which returns a key-value pair as a two-element list. Perl provides the delete function to remove hash elements. Like an array variable (or list literal), a hash can be sliced to access a collection of elements instead of just one element at a time.
25
School of Science and Technology, Online Counseling Resource… Critical Thinking Questions 1.Describe hashes in details emphasizing key and value function? 1.Write a short not on hash slices. 1.Describe each and delete function in connection to perl hashes © 2007, YCMOU. All Rights Reserved.25
26
School of Science and Technology, Online Counseling Resource… Hints For Critical Thinking Question 1.A hash is a collection of zero or more pairs of scalar values, called keys and values. 2.A hash can be sliced to access a collection of elements instead of just one element at a time. 3.To iterate over (that is, examine every element of) an entire hash, a more efficient way is to use each(%hashname), which returns a key- value pair as a two-element list. Perl provides the delete function to remove hash elements. © 2007, YCMOU. All Rights Reserved.26
27
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.27 Study Tips:Books Beginning Perl Simon Cozens, Peter Wainwright. Wrox Press Inc. (May 25, 2000). Beginning Perl Impatient Perl Greg London (Feb 7, 2004). Impatient Perl Extreme Perl Robert Nagler Extreme Perl MacPerl: Power & Ease Vicky Brown and Chris Nandor. (1998). MacPerl: Power & Ease Picking Up Perl Bradley M. Kuhn and Neil Smyth. self published. second edition. (July 2005). Picking Up Perl
28
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.28 Study Tips:Web Resources www.en.wikipedia.org Microsoft Encarta Encyclopedia http://www.perl.org/ http://en.wikibooks.org/wiki/Programming:Perl_Websites http://cpan.perl.org/
29
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.29 Community Web Sites Planet Perl - an aggregation of selected perl journals Planet Perl use Perl; - read community news and personal journals, or start your own journal use Perl; Perl Monks - the wisdom of the Monks can guide you on your Perl Quests Perl Monks perl.org - your current location perl.org the Perl Apprenticeship Site the Perl Apprenticeship Site
30
School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.30 End of the Presentation Thank You
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.