1 / 9

More Perl Data Types

More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference) List: an ordered collection of scalars (a variable that holds a list is called an array).

Download Presentation

More Perl Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. More Perl Data Types • Scalar: it may be a number, a character string, or a reference to another data type. • -the sigil $ is used to denote a scalar(or reference) • List: an ordered collection of scalars (a variable that holds a list is called an array). • -the sigil @ is used to denote a list(or array) • Hash: a map from strings to scalars. The strings are called keys and the • scalars are called values. ‘key/value’ pairs. • -the sigil % is used to denote a hash. • Perl Variables • Variables hold the datatypes described above. • Variables use special sigils to indicate what type they are.

  2. Data Types and Variables: Lists* *an array holds a list. my @scores = (“32”, “45”, “16”, “5”); print “The first score: $scores[0]\n”; print “The third score: $scores[2]\n”;

  3. Perl Data Types and Variables: Hash my %fruit_prices = { apple => ‘1.00’, orange => ‘2.50’ } print “An apple costs:” . $fruit_prices{‘apple’} . “\n”;

  4. List(Array) & Hash Functions: Hashes and Arrays have several native functions which work with them. For a complete listing of these functions, see: http://perldoc.perl.org/index-functions-by-cat.html

  5. Variable Scope: By declaring your variables with ‘my’, you are making that variable accesible to the current block of code you are in (defined by {…}), AND to any blocks contained within your current block. Examples: SEE: scope.pl AND scope2.pl

  6. More On Operators http://www.pageresource.com/cgirec/ptut6.htm http://www.pageresource.com/cgirec/ptut7.htm

  7. Control Structures: if/else- This takes a control expression and a block of code. The control expression is evaluated for truth. If the expression returns ‘true’ the block of code is executed. If it fails, the optional ‘else’ code block is executed, or the program continues to run skipping the ‘if’ code block. see: if_else.pl for examples. while- While loops test one expression for truth, and will keep running the loop as long as the expression returns true. While loops are good for iterating over arrays or through lines in a file. see while.pl for examples. for- for loops are also iterators which has a bit more complex expression testing syntax. see: for.pl for examples. foreach- foreach loops iterate over the items in a list assigning the current value to a scalar for use in the loop. This is a great construct to use in processing arrays or lists. see: foreach.pl for examples.

  8. File Handles: When we read or write files in perl, we use FILEHANDLES to access these files. The FILEHANDLE acts as a kind of variable we can use to read from a file, or write to a file. The function we use to access a file is called open(). Examples: read_file.pl write_file.pl append_file.pl

  9. Exercises

More Related