DOC PREVIEW
DREXEL CS 265 - ct_perl_2

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

An Introduction to Perl – Part IIHashesExamples of HashesInversing a HashForeach Control StructureExample of a Foreach CommandSplit FunctionsExample of a Split FunctionReferencesAn Introduction to Perl – An Introduction to Perl – Part IIPart IIBy: Christopher ToddBy: Christopher ToddHashesHashesHashes are variables that hold lists.Hashes are variables that hold lists.Arrays are also variables that hold lists, Arrays are also variables that hold lists, but the fundamental difference is how that but the fundamental difference is how that lists are indexed.lists are indexed.•Arrays use numbers for indexingArrays use numbers for indexing•Hashes use strings for indexingHashes use strings for indexingIn a hash, you must declare the index with In a hash, you must declare the index with the variable. There is no range operator the variable. There is no range operator for hashes.for hashes.Indexes in a Hash are known as “Keys”Indexes in a Hash are known as “Keys”Examples of HashesExamples of HashesTo define a Hash, we place a ‘%’ before the To define a Hash, we place a ‘%’ before the name of the Hash.name of the Hash.•Ex. If we wish to make a hash named Ex. If we wish to make a hash named ‘Example’, we declare %Example.‘Example’, we declare %Example.We must also declare the index when we We must also declare the index when we declare the variable.declare the variable.•Ex. We wish to make a hash combining the Ex. We wish to make a hash combining the month name (index/key) and number (data):month name (index/key) and number (data):•%Month (“January”, 1, “February”, 2,…, %Month (“January”, 1, “February”, 2,…, “December”, 12)“December”, 12)Inversing a HashInversing a HashBecause a Hash’s indexes and data Because a Hash’s indexes and data are both assignable, we can take are both assignable, we can take data from the indexes and switch it data from the indexes and switch it with the data. with the data. •As in the example before, we can switch As in the example before, we can switch the %Month hash so that the indexes the %Month hash so that the indexes are the number representing the month are the number representing the month and the names are the data.and the names are the data.•%Month_Numbers = reverse %Month%Month_Numbers = reverse %MonthForeach Control StructureForeach Control StructureThe Foreach Control Structure is a The Foreach Control Structure is a form of a loopform of a loopThe loop will go through a array or The loop will go through a array or list, executing one iteration for every list, executing one iteration for every value of the list. value of the list. The list one at a time until the list is The list one at a time until the list is exhaustedexhaustedExample of a Foreach CommandExample of a Foreach CommandEx.Ex.foreach (1..10){ print " $_\n"; }}The command above will go through the list The command above will go through the list of 1-10 until the list is done. The example of 1-10 until the list is done. The example above will print the list 1-10, with each above will print the list 1-10, with each number on a separate line.number on a separate line.Split FunctionsSplit FunctionsSplit is a function that is used to Split is a function that is used to separate data.separate data.A split can be created using a letter, A split can be created using a letter, number, symbol, or any character number, symbol, or any character that can be part of a string.that can be part of a string.The split function can be used to The split function can be used to break a function into parts and insert break a function into parts and insert them into a list or an array.them into a list or an array.Example of a Split FunctionExample of a Split FunctionEx.Ex.$Example_String = “This is an example of $Example_String = “This is an example of a split function”;a split function”;@Example_Array = split “ “, @Example_Array = split “ “, $Example_String;$Example_String;In the Example above, we make a string, In the Example above, we make a string, and then split the string into an array. and then split the string into an array. Every space in the string serves a a split to Every space in the string serves a a split to the string and part of the string is inserted the string and part of the string is inserted into the array.into the array.ReferencesReferences““Perl Basics”Perl Basics”•http://www.cs.drexel.edu/~knowak/cs265_http://www.cs.drexel.edu/~knowak/cs265_fall_2009/perl_basics.pdffall_2009/perl_basics.pdf““Introduction to Perl”Introduction to Perl”•http://www.tizag.com/perlT/index.phphttp://www.tizag.com/perlT/index.php““How To Perl”How To Perl”•http://www.perlmeme.org/http://www.perlmeme.org/““Perl Tutorial”Perl


View Full Document

DREXEL CS 265 - ct_perl_2

Download ct_perl_2
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view ct_perl_2 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view ct_perl_2 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?