Pemrosesan Teks Herika Hayurani Sept 19, 2006

Slides:



Advertisements
Similar presentations
A Guide to Unix Using Linux Fourth Edition
Advertisements

Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX – The Master Manipulator perl Perl is.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
Practical Extraction & Report Language Picture taken from
Perl Basics A Perl Tutorial NLP Course What is Perl?  Practical Extraction and Report Language  Interpreted Language Optimized for String Manipulation.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
Practical Extraction & Report Language PERL Joseph Beltran.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Introduction to Perl & BioPerl Dr G. P. S. Raghava Bioinformatics Centre Bioinformatics Centre IMTECH, Chandigarh Web:
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Introduction to programming in Perl. What is Perl ? Perl : Practical Extraction and Report Language by Larry Wall in 1987 Text-processing language Glue.
Introduction To Perl Susan Lukose. Introduction to Perl Practical Extraction and Report Language Easy to learn and use.
Lecture 10A Perl: Programming Freedom Fundamentals of Engineering For Honors – H192 By Robert Mohr, Ted Pavlic, and Joe Ryan.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
Linux+ Guide to Linux Certification, Third Edition
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
1 Perl Syntax: control structures Learning Perl, Schwartz.
Books. Perl Perl (Practical Extraction and Report Language) by Larry Wall Perl 1.0 was released to usenet's alt.comp.sources in 1987 Perl 5 was released.
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.
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 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
Introduction to Unix – CS 21
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
Introduction to Perl “Practical Extraction and Report Language” “Pathologically Eclectic Rubbish Lister”
5 1 Data Files CGI/Perl Programming By Diane Zak.
©Colin Jamison 2004 Shell scripting in Linux Colin Jamison.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
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.
Week Four Agenda Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems Upcoming deadlines.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
The Scripting Programming Language
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
Chapter 18 The HTML Tag
Input from STDIN STDIN, standard input, comes from the keyboard.
Chapter 2 - Introduction to C Programming
Perl Programming Language Design and Implementation (4th Edition)
Shell Scripting March 1st, 2004 Class Meeting 7.
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 2 - Introduction to C Programming
Chapter 14 Introduction to Ruby.
Perl Variables: Array Web Programming.
Chapter 2 - Introduction to C Programming
Topics Introduction to File Input and Output
Control Structures: if Conditional
Chapter 2 - Introduction to C Programming
Control Structures: for & while Loops
PHP.
PERL: part II hashes, foreach control statements, and the split function By: Kevin Walton.
Perl I/O Learning Objectives:
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Topics Introduction to File Input and Output
CIS 136 Building Mobile Apps
Introduction to C Programming
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Pemrosesan Teks Herika Hayurani Sept 19, 2006 PERL TUTORIAL Pemrosesan Teks Herika Hayurani Sept 19, 2006

Perl Tutorial / Herika Hayurani Outline What is Perl? Running Perl Perl Variables (Scalar, Array, Hash) Decision & Looping constructs Regular Expression Basic I/O Popular Functions 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani What is Perl? Practical Extraction and Reporting Language Interpreted programming language with a huge number of uses, libraries and resources. Invented by Larry Wall in 1987 as a general purpose Unix scripting language to make his programming work simpler 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Running Perl Extension file: .pl Every statement is ended by ; (semicolon) symbol Tells the file to run through Perl (written in first line of program): #!/usr/local/bin/perl Run the program: perl programName Run using debugger: perl -d programName Run with warnings: perl -w programName 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Perl Variables Variable names: Contains only letters, or the underscore character. May contains numbers, but not begin with a number. Easy identifier, or at least in what context they're being used. Scalar: preceded by the $ symbol Array / list: preceded by the @ symbol Hash: preceded by the % symbol To place data inside the variable, use an operator, specifically the assignment operator (an equal sign) 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Scalar Variables Store only a single thing or one value A string is a sequence of characters that is contained within single or double quotes Syntax: $scalarName Automatic conversion between numeric and text data types when appropriate Assignment example: $fruit = "orange"; Access method: $fruit; #return "orange" 12/5/2018 Perl Tutorial / Herika Hayurani

List / Array Variables [1] Considered as a group of scalar variables Index starts from 0 Syntax: @array_name refers to an entire array $array_name[index] refers to an element of an array Assignment examples: @fruits = ("orange", "grape", $others); @names = ("betty", "veronica", "tom"); 12/5/2018 Perl Tutorial / Herika Hayurani

List / Array Variables [2] Return the last index of an array: $#fruits; Truncate the entire array: @fruits = (); Access example: $fruits[1]; #access "grape" $#names; #return 2 or the last index of @names 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Hash Variables [1] Contains pairs of (keys and values) Assignment syntax: %hashName = (key1,value1, key2,value2, ...); Assignment examples: %grade = ("A",4, "B",3, "C",2); Access syntax: $hashName{key}; #return value of key in hashName Access examples: $grade{"A"}; #return 4 (the grade of "A") 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Hash Variables [2] Returns a list of all the keys of the given hash: keys(%hashName); Returns the list of all the values in a given hash values(%hashName); Example: keys(%grade); #return "A", "B", "C" values(%grade); #return 4, 3, 2 12/5/2018 Perl Tutorial / Herika Hayurani

Decision & Looping constructs Decision-making constructs contain a control expression that determines whether a bloc of statements will be executed Looping constructs allow the program to repetitively execute a statement block until some condition is satisfied 12/5/2018 Perl Tutorial / Herika Hayurani

Decision constructs: if elsif else Syntax: if(Expression1) { Block; } elsif (Expression2) { } else { } 12/5/2018 Perl Tutorial / Herika Hayurani

Decision constructs: unless Similar to the if statement, except that the control expression after the unless is tested for the reverse condition; that is if conditional expression following the unless is false, the statement block is executed Syntax: unless(Expression) {Block;} unless(Expression) {Block;} else {Block;} unless(Expression) {Block;} elsif(Expression) else {Block;} 12/5/2018 Perl Tutorial / Herika Hayurani

Comparison Operators [1] ‘Equality’ operators: == (numeric values), eq (string values) If the values are equal, the test returns true and if the values are not equal, the test returns false. ‘Not Equality’ operators: != (numeric values), ne (string values) If the values are not equal, the test returns true and if the values are equal, the test returns false. 12/5/2018 Perl Tutorial / Herika Hayurani

Comparison Operators [2] <= - less than or equal >= - greater than or equal < - less than > - greater than 12/5/2018 Perl Tutorial / Herika Hayurani

Looping Structures: for loop [1] Three parts - the initial expression, the test expression, and a re-initialization of the initial expression Syntax: for (initial; test; re-initialization) { ... } 12/5/2018 Perl Tutorial / Herika Hayurani

Looping Structures: for loop [2] Example: @myNames = ('Larry', 'Curly', 'Moe'); for ($i=0; $i<=$#myNames; $i++) { #print one-by-one elements print $myNames[$i]; } Note: $#myNames – returns the last index of @myNames 12/5/2018 Perl Tutorial / Herika Hayurani

Looping Structures: foreach [1] Access one by one elements in lists / arrays and hashes Example 1 (array): @myNames = ('Larry', 'Curly', 'Moe'); foreach (@myNames) { print $_; #print one-by-one elements } Example 2 (hash): %grades = ("A",4, "B",3, "C",2); foreach (%grades) { print $_; #print one-by-one keys and values 12/5/2018 Perl Tutorial / Herika Hayurani

Looping Structures: foreach [2] A cleaner foreach loop (not use $_) Example 1 (array): @myNames = ('Larry', 'Curly', 'Moe'); foreach $name(@myNames) { print $name; #print one-by-one elements } Example 2 (hash): %grades = ("A",4, "B",3, "C",2); foreach $key(keys %grades) { print $key.":".$grades{$key}; #print one-by-one keys:values 12/5/2018 Perl Tutorial / Herika Hayurani

Looping Structures: while loop The steps: Evaluate the initial expression. Does the test evaluate to true? If so, continue, otherwise exit the while loop. Execute the code block inside the while loop. Return to step 2. Syntax: while (expression) { ... } 12/5/2018 Perl Tutorial / Herika Hayurani

Looping Structures: do while loop It is used to step through a block of code while a specific condition is true, but executes the code before evaluating the expression. Syntax: do { ... } while (expression); 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Regular Expression [1] Special characters can be used to match regex: . - Any single character except a newline ^ - The beginning of the line or string $ - The end of the line or string * - Zero or more of the last character + - One or more of the last character ? - Zero or one of the last character \t - matches a tab \n - matches a new line \b - matches a word boundary 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Regular Expression [2] Special characters can be used to match regex: \w - matches a "word" character (alphanumeric, "_") \W - matches a non-word character \s - matches a whitespace character \S - matches a non-whitespace character \d - matches a digit \D - matches a non-digit \s+ - matches one or more spaces \d+ - matches one or more digits 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Regex Matching Example: $var = "tomat tomatoes stomache"; if ($var =~ /tom/) { print "There is a pattern \"tom\" in the variable"; } else { print "There is no pattern \"tom\" in the variable"; } #Print "There is a pattern \"tom\" in the variable" to the screen 12/5/2018 Perl Tutorial / Herika Hayurani

Matching and Replacing [1] Replace (substitute) a pattern in scalar variable with another pattern Format: s/<pattern>/<new pattern> 12/5/2018 Perl Tutorial / Herika Hayurani

Matching and Replacing [2] Example: $var = "tomat tomatoes stomache"; $var =~ s/tom//gi; #$var="at atoes sache " $var1 = "tomat tomatoes stomache"; $var1 =~ s/tom//i; #$var="at tomatoes stomache" Note: the g suffix causes a global substitution the i suffix causes case-insensitive matching 12/5/2018 Perl Tutorial / Herika Hayurani

Basic I/O: Standard I/O Print to standard output (screen) print "text"; print STDOUT "text"; Read from standard input (keyboard) $line = <STDIN>; The diamond operator (<>) (used very frequently!) 12/5/2018 Perl Tutorial / Herika Hayurani

Basic I/O: Reading a File Reading a file or opening a filehandle to a specific resource (a file): open(FILEHANDLE, fileName); Closing the filehandle: close(FILEHANDLE); Example: open(IN, 'data.txt'); #open 'data.txt' using IN filehandle close(IN); #close 'data.txt' using IN filehandle 12/5/2018 Perl Tutorial / Herika Hayurani

Basic I/O: Writing a File [1] Check the file permissions to see if Perl script is allowed to write to the data file Writing to a file or opening a filehandle to a specific resource (a file) with append mode: open (FILEHANDLE, '>>fileName'); Writing to a file or opening a filehandle to a specific resource (a file) with overwrite mode: open (FILEHANDLE, '>fileName'); Closing the filehandle: close(FILEHANDLE); 12/5/2018 Perl Tutorial / Herika Hayurani

Basic I/O: Writing a File [2] Print data to a file: print FILEHANDLE "The printed data"; #print "The printed data" to a file using filehandle Example: open(OUT, '>>data.txt'); #append 'data.txt' using OUT filehandle open(OUT, '>data.txt'); #overwrite 'data.txt' using OUT filehandle close(OUT); #close 'data.txt' using OUT filehandle print OUT "I am studying Perl"; #print "I am studying Perl" to 'data.txt' file 12/5/2018 Perl Tutorial / Herika Hayurani

Basic I/O: Accessing Directory Opening a directory using filehandle: opendir(DIRHANDLE,dirName) Reading a directory and store to an array: @isiDir = readdir(DIRHANDLE); Closing a directory using filehandle: closedir(DIRHANDLE); 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Functions: split [1] Splits up a string (EXPR) by some delimiter and returns an array. LIMIT specifies the number of fields that can be split. Format: @array = split("DELIMITER", EXPR, LIMIT); @array = split(/DELIMITER/, EXPR, LIMIT); @array = split("DELIMITER", EXPR); @array = split(/DELIMITER/, EXPR); ($var1, $var2) = split(/DELIMITER/, EXPR); 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Functions: split [2] Example: $test = "I am studying Perl"; @ar = split(/\s+/,$test); print($ar[3]); #Print "Perl" to screen @ar = split(/\s+/,$test,3); print($ar[0]); #Print "I" to screen print($ar[1]); #Print "am" to screen print($ar[2]); #Print "studying Perl" to screen 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Functions: join [1] Joins the elements of an array (LIST) into a single string and separates each elements of the array with a given delimiter Format: $var = join("DELIMITER", LIST); $var = join(/DELIMITER/, LIST); 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Functions: join [2] Example: $name = "I am studying Perl"; $today = "Sept 19, 2006"; $var = join(" : ", $name, $today); print($var); #Print "I am studying Perl : Sept 19, 2006" to screen 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Functions: sort [1] Sorts and returns a sorted array If SUBROUTINE is specified, the first argument to sort is the name of the subroutine followed by a list of integers. Subroutine returns an integer less than, equal to, or greater than 0. The values are passed to the subroutine by reference and are received by the special variables, $a and $b. 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Functions: sort [2] Format: sort(SUBROUTINE LIST); sort(LIST); sort SUBROUTINE LIST; sort LIST; Example: @str = (1, 9.5, 5, 10, a, B, z); @sortedStr = sort(@str); print @sortedStr; #print "11059.5Baz" to screen 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Functions: sort [3] Example (sort numerically): @numbers = (1, 9.5, 5, 10); sub angka{$a<=>$b}; #make subroutine angka @sortedNumbers = sort angka(@numbers); print @sortedNumbers; #print "159.510" to screen @sorted_numbers = sort {$a<=>$b}(3,4,1,2); #sort with unnamed subroutine print @sorted_numbers; #print "1234" to screen 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Functions: reverse Reverses the elements in an array If the values appeared in descending order, now they are in ascending order Format: reverse(LIST); reverse LIST; Example: @angka = (3,4,1,2); @reversed = reverse(@angka); print @reversed; #print "2143" to the screen 12/5/2018 Perl Tutorial / Herika Hayurani

Functions: chop and chomp chop function removes the last character in a scalar variable or the last character of each word in an array chomp function removes the last character in a scalar variable or the last character of each word in an array only if the last character is the newline Example: $text = "I am studying Perl\n"; chomp($text); #$name = "I am studying Perl" chop($text); #$name = "I am studying Per" 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Functions: exists Returns true if an array index (or hash key) has been defined, and false if it has not Format: exists $arrayName[index]; #check value of array index exists $hashName{key}; #check hash key Example: @names = qw(Tom Paul Anton Steve); exists $names[3]; #return true %grades = ("A",4, "B",3, "C",2); exists $grades{"D"}; #return false 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani References Books: Christiansen, Tom and Nathan Torkington. Perl Cookbook. O’Reilly. Quigley, Ellie. Perl by Example Third Edition. Schwartz, Randal and Christiansen Tom and Larry Wall. Learning Perl. O’Reilly. Slides in Internet: Bixby, Mark. Perl Programming. Pradeepsunder. Perl Tutorial 12/5/2018 Perl Tutorial / Herika Hayurani

Perl Tutorial / Herika Hayurani Sekian ~Terima kasih~ 12/5/2018 Perl Tutorial / Herika Hayurani