1 / 14

LING/C SC/PSYC 438/538

LING/C SC/PSYC 438/538. Lecture 4 9 /5 Sandiway Fong. Continuing with Perl. Learn Perl Books… Online resources http://learn.perl.org/ w e begin with ... http://perldoc.perl.org/perlintro.html. O'Reilly's Safari library. UA free access:

emery
Download Presentation

LING/C SC/PSYC 438/538

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. LING/C SC/PSYC 438/538 Lecture 4 9/5 Sandiway Fong

  2. Continuing with Perl • Learn Perl • Books… • Online resources • http://learn.perl.org/ • we begin with ... • http://perldoc.perl.org/perlintro.html

  3. O'Reilly's Safari library • UA free access: • http://proquest.safaribooksonline.com.ezproxy2.library.arizona.edu/search?q=perl

  4. Perl arrays and hashes • Scalars: • strings, numbers (integers, floating point numbers), references • Arrays: • Idea: list of scalars • Access by index: 0,1,2,… • Hash: • Like an array except access not through a numeric index • Use user-specified keys $variable @variable %variable different namespaces: $apple @apple %apple are different data structures and can co-exist simultaneously

  5. Perl Arrays • Arrays: • Idea: list of scalars • Access by index: 0,1,2,… • Notes on output • print @a zeroonetwothreefour • print “@a” zero one two three four • negative indices count from the end.. • $#name (index of last element) • @name (can have scalar interpretation) controlled by system variable $” default value: a space

  6. Perl arrays • List operations:

  7. Perl Arrays • Last time: • by default sort works according to the ASCII chart, character by character asciitable.com

  8. Perl Arrays • Numeric sort? • Idea: to pass an inline comparison function as parameter…

  9. Perl Arrays • Getting user input into your program: @ARGV

  10. Perl Hashes

  11. Perl Hashes • Notes on arrays and hashes • arrays are indexed from 0,1,2,3… • hashes are like arrays with user-defined indexing (aka associative array or hash table) • initialization (use list notation (or shortcut): round brackets and commas) • @a = (“zero”, “one”, “two”, “three”, “four”); • %h = (“zero”, 0, “one”, 1, “two”, 2, “three”, 3, “four”, 4}; (key/value pairs) • access to individual elements (square brackets vs. curly braces) • $a[1] “one” • $h{zero} 0 Shortcut:

  12. Perl Hashes • Notes on arrays and hashes • output • print @a zeroonetwothreefour • print “@a” zero one two three four • print %h three3one1zero0two2four4 (note: different order) • print “%h” %h (literal, no interpolation done) What happens here? • %pos = ("apple", "n", "speak", "v", "happy", "a", "walk", "n", "walk", "v"); • print $pos{"walk"}, "\n"; • (hash keys are unique)

  13. Perl Hashes • Conditionals • if ( @a < 10 ) { print “Small array\n” } else {print “Big array\n” } • Note: @a here is a scalar = size of array • unless (@a > 10) { print “@a\n” } • Note: if size of array a is ≤ 10, it prints the contents of array a • Looping • %fruits = ("apple", "green", "orange", "orange", "lemon", "yellow"); • foreach $fruit (keys %fruits) { print $fruit, " => ", $fruits{$fruit}, "\n” } gives output: • lemon => yellow • apple => green • orange => orange • Note:keys %fruits = (“lemon” “apple” “orange”) produces a list

  14. Perl Hashes • Sorting sort byage keys %age

More Related