Professional Documents
Culture Documents
3/7/2011
PART-I
HASHES
INTRO
3/7/2011
One of the most powerful and important feature of Perl Known as Associative Arrays in olden days Lazy Programmers Community thought---this was too many letters to type and too many syllables to say In about 1995--- name changed to hashes
3/7/2011
SCENARIO
One limitation of an array=>information contained within it can be difficult to get to. e.g., imagine a list of people & their corresponding ages. @names = qw/ Shreya Mohit Gulshan Micky /; @ages = (20,35,23,41); To get Shreyas age, we just access index 0 of the @ages array But how will you get Mickys age if his name is known rather than his location in the @ages aray?
3/7/2011
SCENARIO CONTD
only way step through @names until we found Micky, & check corresp. age in the @ages array Fine for 4-element array, but for larger(say 40 or 400 or 4000 elements long) ??? If the person was at the end of the list step through 4000 items before we got to the information we wanted The hash solves this problem, very neatly by allowing us to access that @ages array not by an index, but by a scalar key
3/7/2011
HASH SOLUTION
%ages = (Shreya' => 20, Mohit' => 35, Gulshan' => 23, Micky => 41); Now want to print out Mickys age,access value within the hash using Mickys name as the key: print Micky is $ages{Micky} years old\n";
3/7/2011
3/7/2011
3/7/2011
3/7/2011
3/7/2011
HASH ASSIGNMENT
A hash may be copied using the obvious syntax: %new_hash = %old_hash; This line of code tells Perl to unwind the %old_hash into a list of key-value pairs, then assign those to %new_hash, building it up one key-value pair at a time. Make an inverse hash: %inverse_hash = reverse %any_hash;
3/7/2011
3/7/2011
EXAMPLE
"Roger" => "Bini, "Sandeep" => "Patil", ); foreach $person(qw < Anil Kapil Roger Sandeep >) { print "I've heard of $person $last_name{$person}.\n"; }
3/7/2011
OUTPUT
o o o o
Ive heard of Anil Kumble. Ive heard of Kapil Dev. Ive heard of Roger Bini. Ive heard of Sandeep Patil.
3/7/2011
HASH FUNCTIONS
The keys Function: keys(%hashname) function yields a list of all the current keys in the hash %hashname. The values Function: values(%hashname) function returns a list of all the current values of the %hashname. The each Function: each(%hashname) returns a key-value pair as a two-element list
3/7/2011
3/7/2011
Perl Program runs in an environment. Perl stores this information in the %ENV hash. probably see a PATH key in %ENV: print "PATH is $ENV{PATH}\n"; Depending on OS & platform output may be:
o o
OR
o
PATH is /usr/local/bin:/usr/bin:/sbin:/usr/sbin
Part II
I/O REVISITED
3/7/2011
Reading from standard input (via the Perl filehandle called STDIN) is easy $a = <STDIN>; # read the next line up to a newline, or whatever $/ set to. Perl automatically copies the line that is read into the $_ variable. while (<STDIN>) { chomp; # like "chomp($_)" # other operations with $_ here }
3/7/2011
INPUT FROM THE DIAMOND OPERATOR Unlike <STDIN>, the diamond operator gets its data from the file or files specified on the command line Technically, the diamond operator works from the @ARGV array @ARGV = ("aaa","bbb","ccc"); while (< >) { # process files aaa, bbb, and ccc print "this line is: $_"; }
3/7/2011
OUTPUT TO STDOUT
Using print for Normal Output: The print function takes a list of strings and sends each string to standard output in turn print (2+3),"hello"; # wrong! prints 5, ignores "hello" print ((2+3),"hello"); # right, prints 5hello print 2+3,"hello"; # also right, prints 5hello
3/7/2011
Using printf for Formatted Output: The printf function takes a list of arguments (enclosed in optional parentheses) The first argument is a format control string
o
Part III
INTRODUCTION TO FILEHANDLES
3/7/2011
WHAT IS A FILEHANDLE?
A filehandle in a Perl program is the name for an I/O connection between your Perl process and the outside world. STDIN is a filehandle, naming the connection between the Perl process and the UNIX standard input(or DOS prompt?) filehandles are used without a special prefix character Recommendation: use ALL UPPERCASE letters in your filehandle to avoid confusion with other similar terms in the same namespace.
3/7/2011
OPENING A FILEHANDLE
Perl provides three filehandles, STDIN, STDOUT, and STDERR automatically open to files or devices established by the program's parent process (probably the shell) open function to open additional filehandles. The syntax looks like this: open(FILEHANDLE,"somename"); where FILEHANDLE is the new filehandle and somename is the external filename
3/7/2011
CLOSING A FILEHANDLE
All forms of open return true for success and false for failure
When you are finished with a filehandle, you may close it with the close operator, like: close(FILEHANDLE);
3/7/2011
USING FILEHANDLES
Once a filehandle is open for reading, ONE can READ lines from it just as ONE can read from standard input with STDIN. e.g., to read lines from the password file: open (EP,"/etc/passwd"); while (<EP>) {chomp; print "I saw $_ in the password file!\n"; }
3/7/2011
THANK YOU