Built-In Functions. Notations For each function, I will give its name and prototype. –prototype = number and type of arguments ARRAY means an actual named.

Slides:



Advertisements
Similar presentations
Arrays A list is an ordered collection of scalars. An array is a variable that holds a list. Arrays have a minimum size of 0 and a very large maximum size.
Advertisements

Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Scalar Data Types and Basic I/O
Objected Oriented Perl An introduction – because I don’t have the time or patience for an in- depth OOP lecture series…
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
COMP234 Perl Printing Special Quotes File Handling.
Programming Perls* Objective: To introduce students to the perl language. –Perl is a language for getting your job done. –Making Easy Things Easy & Hard.
CSET4100 – Fall 2009 Perl Introduction Scalar Data, Operators & Control Blocks Acknowledgements: Slides adapted from NYU Computer Science course on UNIX.
COS 381 Day 19. Agenda  Assignment 5 Posted Due April 7  Exam 3 which was originally scheduled for Apr 4 is going to on April 13 XML & Perl (Chap 8-10)
Introduction to Perl. How to run perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Your program/script.
Sorting. Simple Sorting As you are probably aware, there are many different sorting algorithms: selection sort, insertion sort, bubble sort, heap sort,
1ex.1 Perl Programming for Biology Exercise 1 The Bioinformatics Unit G.S. Wise Faculty of Life Science Tel Aviv University, Israel March 2009 Eyal Privman.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Prog.Perl Most operators have numeric and string version –remember Perl will convert variable.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
Perl Programming WeeSan Lee
Computer Programming for Biologists Class 2 Oct 31 st, 2014 Karsten Hokamp
Ruby (on Rails) CSE 190M, Spring 2009 Week 2. Arrays Similar to PHP, Ruby arrays… – Are indexed by zero-based integer values – Store an assortment of.
Mathcad Variable Names A string of characters (including numbers and some “special” characters (e.g. #, %, _, and a few more) Cannot start with a number.
2 Alerts and the If/Else Conditional Statement CONTINUED There's No Right Way to Do It There are, literally, a million ways to write any given script.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 12.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Subroutines and Files Bioinformatics Ellen Walker Hiram College.
Control Structures. Important Semantic Difference In all of these loops we are going to discuss, the braces are ALWAYS REQUIRED. Even if your loop/block.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Perl Practical(?)‏ Extraction and Report Language.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Perl Language Yize Chen CS354. History Perl was designed by Larry Wall in 1987 as a text processing language Perl has revised several times and becomes.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Perl: Lecture 1 The language. What Perl is Merger of Unix tools – Very popular under UNIX – shell, sed, awk Programming language – C syntax Scripting.
Chapter 9: Perl Programming Practical Extraction and Report Language Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
CS 105 Perl: Data Types Nathan Clement 15 May 2014.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Perl Chapter 5 Hashes. Outside of world of Perl, know as associative arrays Also called hash tables Perl one of few languages that has hashes built-in.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
– Introduction to Perl 12/13/ Introduction to Perl - Strings, Truth and Regex Introduction to Perl Session 2 · manipulating.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
A Few More Functions. One more quoting operator qw// Takes a space separated sequence of words, and returns a list of single-quoted words. –no interpolation.
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)
Topic 2: Working with scalars CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 2, pages 19-38, Programming Perl 3rd edition chapter.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Arrays and Strings in Assembly
2.1 Scalar data - revision numeric e-14 ( = 6.35 × )‏ operators: + (addition) - (subtraction) * (multiplication) / (division)
Computer Programming for Biologists Class 4 Nov 14 th, 2014 Karsten Hokamp
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
Operators. Perl has MANY operators. –Covered in Chapter 3 of Camel –perldoc perlop Many operators have numeric and string version –remember Perl will.
Chapter 16 Introduction to Perl Perl (Practical Extraction and Report Language) Developed by Larry Wall as a Unix scripting language. Popular server-side.
COMP234-Perl Variables, Literals Context, Operators Command Line Input Regex Program template.
Perl Backticks Hashes printf Variables Pragmas. Backticks.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Perl Variables: Array Web Programming.
Context.
Subroutines.
Useful functions.
Presentation transcript:

Built-In Functions

Notations For each function, I will give its name and prototype. –prototype = number and type of arguments ARRAY means an actual named array (i.e., variable starting LIST means any list of elements (i.e., a list literal, a named array, or an expression that returns a list) –Recall that a LIST can have 0, 1, or many values. HASH means a named hash variable any other type is a scalar –notation identifies the purpose of the argument For more info on any of these functions, run: –perldoc –f

lc STRING; uc STRING; lcfirst STRING; ucfirst STRING lc and uc return a lower- or upper- cased version of STRING lcfirst and ucfirst return the STRING with the first character lower- or upper- cased $str = 'HELLO WORLD'; $str = lc $str; #hello world $str = ucfirst $str; #Hello world $hola = ucfirst lc 'HOLA'; #Hola Used internally by \L, \U, \l, and \u –$hola = "\u\LHOLA";

Mathematical Functions abs NUM –absolute value of NUM sqrt NUM –square root of NUM cos RADIANS, sin RADIANS –cosine or sine of value in radians atan2 RADIANS –arctangent of value in radians –tangent(X) = sin(X) / cos(X) exp POWER –e to the power of POWER log NUM –natural logarithm of NUM –log in base X of N = log(N) / log(X);

keys HASH; values HASH keys  return list of all keys from HASH –seemingly random order values  return list of all values from HASH –same 'random' order as keys produces my %abbr_of = ('Jan' => 'January', 'Feb' => 'February', 'Mar' => 'March', …); keys (%abbr_of)  ('Nov', 'Jan', 'Oct', …) values (%abbr_of)  ('November', 'January', 'October', …) In scalar context, both return number of key/value pairs in the hash –my $months = keys %abbr_of; #$months = 12 –my $months = values %abbr_of; #$months = 12

length EXPR return number of characters in EXPR $a = "Hello\n"; $b = length $a; $b  6 if string is omitted, returns number of characters in $_ Do Not use to find size of array or hash –What do you use? –What will happen if you try?

index STR, SUBSTR, OFFSET Look for first occurrence of SUBSTR within STR (starting at OFFSET) –OFFSET defaults to 0 if omitted Return first position within STR that SUBSTR is found. my $x = index "Hello World\n", "o"; my $y = index "Hello World\n", "o", $x+1; –$x  4, $y  7 Returns -1 if SUBSTR not found. rindex  return last position found

substr EXPR, START, LENGTH returns the substring of EXPR starting at character START of length LENGTH my $s = 'Hello World'; my $ss = substr($s, 3, 5); #'lo Wo' Negative start: start that many from the end –$ss = substr ($s, -4, 3); #'orl' Omit length: return all remaining chars –$ss = substr($s, 6); #'World'; Negative length: leave that many off the end –$ss = substr($s, 6, -2) #'Wor'

Changing Substrings substr is one of the rare functions that you can assign to directly: my $s = 'Hello World'; substr($s, 6, 5) = 'Everybody'; $s now: 'Hello Everybody'; String will automatically grow or shrink accordingly. Alternatively, use 4-arg substring: my $s = 'Fred Flintstone'; substr($s, 0, 4, 'Wilma'); –$s => 'Wilma Flintstone'

reverse LIST in list context, return a list consisting of elements in LIST, in opposite order = (1.. 10); =  (1, 2, 3, 4, 5, 6, 7, 8, 9,  (10, 9, 8, 7, 6, 5, 4, 3, 2, 1) in scalar context, concatenate all elements of LIST into a string, and return reverse of that string –my $rev = –$rev  ' ' Useful for reversing a single string –my $rstr = reverse 'abcd'; $rstr  'dcba';

-X FILE A series of "file test" operators. Take either a filename or an open filehandle –If no argument given, uses $_ -f 'file.txt'  true if file.txt is a plain file -d $path  true if $path is a directory -e $path  true if $path exists -r $file  true if the file is readable -w $file  true if the file is writable -s $file  size (in bytes) of $file -M $file  days since $file last modified For the full list, Camel Chapter 29 –perldoc –f –X

-X Common Error Argument passed to file tests must be full relative path to the file or directory. tests will fail if file does not exist in the current working directory opendir(my $dh, 'temp') or die...; while (my $file = readdir($dh)) { if (-r $file){... } # Wrong! No $file in current # directory! if (-r "temp/$file") {... } # Correct }

–X efficiency -X actually performs a stat() on the filehandle, and stores 13 pieces of information. stat() is rather costly, so it's best to do it as infrequently as possible. After a stat(), the information is stored in memory, and accessible via the special filehandle: _ if (-e $file and –r _) { print "$file exists and is readable\n"; } Note this is NOT the same as –r $_ !!

sort LIST returns LIST sorted in "ASCIIbetical" order. –Empty string first, then sort by ASCII chart –does not affect LIST that is passed in = ('Banana','Apple','Carrot'); =  ('Apple', 'Banana', 'Carrot') By ASCII chart, "100" comes before "99" –and A-Z all come before any of a-z = sort ( );  (100, 101, 102, 97, 98, 99)

Advanced Sorting You can tell sort how you want a list sorted –Write a small subroutine describing the sort order –Perl will call this subroutine repeatedly, each time assigning $a and $b to be two elements from the list to sort In your subroutine, compare $a and $b however you need to. If $a should come before $b in sort order, return –1. If $b should be first, return 1. if order of $a and $b doesn't matter, return 0 sub by_number { if ($a < $b) {return –1;} elsif ($a > $b) {return 1;} else {return 0;} }

Using Your Own Sort Now that we have that function, use it in sort: = (4, 2, 9, 10, 14, 11); = sort  (2, 4, 9, 10, 11, 14); Look at that function again… if ($a $b) {return 1;} else {return 0;} This can be simplified quite a bit. –return ($a $b);

Simplifying Further We now have: sub by_number{ return ($a $b); } All Perl blocks return the last value evaluated. Therefore, return keyword is optional When sort function is that simple, don't even need to declare it: = sort {$a Excellent description of sorting in Llama chapter 15 –perldoc -f sort –perldoc -q sort

More Examples Sort a series of people based on their ages my %age_of = (Jess=>22, Megan=>21, Mike=>24, Paul=>31); = sort { $age_of{$b} $age_of{$a} } keys  ('Paul', 'Mike', 'Jess', 'Megan') Sort a series of strings based on their last letter : = ('Foo', 'Bar', 'Baz', 'Biff', 'Bam'); sub by_last { my $last_a = substr($a, -1, 1); my $last_b = substr($b, -1, 1); return $last_a cmp $last_b; } =  ('Biff', 'Bam', 'Foo', 'Bar', 'Baz')