Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pemrosesan Teks Herika Hayurani Sept 19, 2006

Similar presentations


Presentation on theme: "Pemrosesan Teks Herika Hayurani Sept 19, 2006"— Presentation transcript:

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

2 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

3 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

4 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

5 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 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

6 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

7 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

8 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 12/5/2018 Perl Tutorial / Herika Hayurani

9 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

10 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

11 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

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

13 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

14 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

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

16 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

17 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 12/5/2018 Perl Tutorial / Herika Hayurani

18 Looping Structures: foreach [1]
Access one by one elements in lists / arrays and hashes Example 1 (array): @myNames = ('Larry', 'Curly', 'Moe'); foreach { 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

19 Looping Structures: foreach [2]
A cleaner foreach loop (not use $_) Example 1 (array): @myNames = ('Larry', 'Curly', 'Moe'); foreach { 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

34 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

35 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

36 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

37 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 = #print " Baz" to screen 12/5/2018 Perl Tutorial / Herika Hayurani

38 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 #print " " to screen @sorted_numbers = sort {$a<=>$b}(3,4,1,2); #sort with unnamed subroutine #print "1234" to screen 12/5/2018 Perl Tutorial / Herika Hayurani

39 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 = #print "2143" to the screen 12/5/2018 Perl Tutorial / Herika Hayurani

40 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

41 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

42 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

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


Download ppt "Pemrosesan Teks Herika Hayurani Sept 19, 2006"

Similar presentations


Ads by Google