Introduction to Perl Pawel Sirotkin 28.11-01.12.2008, Riga.

Slides:



Advertisements
Similar presentations
Python Basics: Statements Expressions Loops Strings Functions.
Advertisements

Computer Programming w/ Eng. Applications
Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Introduction to C Programming
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Fundamentals of Python: From First Programs Through Data Structures
An Introduction to Textual Programming
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
Introduction to Python
Nael Alian Introduction to PHP
General Programming Introduction to Computing Science and Programming I.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
Input, Output, and Processing
Course websites CS201 page link at my website: Lecture slides Assistant’s Information Recitations Office Hours Make-up.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Strings, output, quotes and comments
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Scripting Languages Course 3 Diana Trandab ă ț Master in Computational Linguistics - 1 st year
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Python Lesson 1 1. Starter Create the following Excel spreadsheet and complete the calculations using formulae: 2 Add A1 and B1 A2 minus B2 A3 times B3.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
CS201 Introduction to Sabancı University 1 Chapter 2 Writing and Understanding C++ l Writing programs in any language requires understanding.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Perl for Bioinformatics Part 2 Stuart Brown NYU School of Medicine.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Linux Administration Working with the BASH Shell.
Shell scripts – part 1 Cs 302. Shell scripts  What is Shell Script? “Shell Script is series of command written in plain text file. “  Why to Write Shell.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Scripting Languages Course 5 Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Scripting Languages Course 7 Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Scripting Languages Perl – course 3
Chapter 6 JavaScript: Introduction to Scripting
Intro to PHP & Variables
Engineering Innovation Center
Introduction to C++ Programming
T. Jumana Abu Shmais – AOU - Riyadh
Introduction to Primitive Data types
Course websites CS201 page link at my website: Lecture slides
Introduction to TouchDevelop
Chapter 3: Selection Structures: Making Decisions
Chapter 3: Selection Structures: Making Decisions
Introduction to Primitive Data types
Presentation transcript:

Introduction to Perl Pawel Sirotkin , Riga

Overview Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 2  About programming  Why Perl?  How to write, how to run  Variables  Operations  Basic input and output  Conditionals and loops  Regular expressions

About programming Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 3  Working with algorithms  Program needs to contain exact commands  (Mostly) not: Go buy some bread  But: Put on your coat and shoes, open the door, go through it, close the door, go down the stairs…  Has a certain input  Processes it  Produces a certain output

Why Perl? Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 4  Easy to learn  Simple syntax  Good at manipulating text  Good at dealing with regular expressions

How to write a Perl program Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 5  Perl programs can be written in any text editor  Notepad, vim, even Word…  Recommended: A simple text editor with syntax highlighting  Write the program code  Save the file as xxx.pl .pl extension not necessary, but useful

What is a Perl program like? Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 6 # This *very* simple program prints "Hello World!“ print "Hello World!";

What is a Perl program like? Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 7  The content of a line after the # is commentary. It is ignored by the program  What are commentaries for, then?  They are for you, and others who will have to read the code  Imaging looking at a complex program in a few months and trying to figure out what it does  Write as much commentary as you can # This *very* simple program prints "Hello World!“ print "Hello World!";

What is a Perl program like? Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 8  This is a Perl command  In this case, for printing text on the screen  Every command should start at a new line  Not a Perl requirement, but crucial for readability  Every command should end with a semicolon;  Many commands take arguments  Here: “Hello World!” # This *very* simple program prints "Hello World!“ print "Hello World!";

What to do with the program? Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 9  Perl works from the command line  Windows: „Start“  „Run…“  Go to the directory where you saved the program  E.g.: cd C:\Perl\MyPrograms  Run the program:  perl myprogram.pl  See the results of your labours!

Exercise (1) Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 10  Create a folder for your Perl programs  Open the editor of your choice and write the „Hello World“ program  The command is print „Hello World!“;  Don‘t forget the commentary!  Save the program  Run it!  What happens if you misprint the print command?

Variables Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 11  The „Hello World“ program always has the same output  Not a very useful program, as such  We need to be able to change the output  Variables are objects that can hold different values

Defining variables Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 12  To define a variable, write a dollar sign followed by the variable’s name  Names should consist of letters, numbers and the underscore  They should start with a letter  Variable names are case-sensitive!  $a and $A are different variables!  Generally, a variable’s name should tell you what the variable does # We define a variable „a“ and assign it a value of „42“ $a = 42;

Defining variables Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 13  Variables can be assigned values  String: text (character sequence) in quotes/double quotes  Numbers  $a = 42;  $a = “some text”; # We define a variable „a“ and assign it a value of „42“ $a = 42;

Changing variables Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 14  Arithmetic operations  $a = 42 / 2;# division  $a = ;# addition  $a = $b * 2;# multiplication  $a = $a - $b;# subtraction  Also useful:  $a += 42;# the same as $a = $a + 42;  The same for +, -, /  String operations  $a = “some“. “ text“;# concatenation  $a = $a. “ more text“;

Basic output Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 15  We have already seen an output command  print “text“;  print $a;  print “text $a“;  print “text “. $a+$b. “ more text.“;  Special characters:  \n – new line  \t – tabulator

Exercise (2) Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 16  Define a variable  Assign it a value of 15  Print it  Double the value  Print it again  Define another variable with the string „apples“  Print both variables  Change the first variable to its square and the second to „pears“  Print both variables

Basic input Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 17  The <> operator returns input from the standard source (usually, the keyboard)  Syntax:  $a = <>;  Don’t forget to tell the user what he’s supposed to enter!  Try the following program: # This program asks the user for his name and greets him print "What is your name? "; $name = <>; print "Hello $name!";

Input, output and new lines Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 18  As the user input is followed by the [Enter] key, the string in $name ends in a new line  The chomp function deletes the new line at the end of a string  Try the following, modified program: # This program asks the user for his name and greets him print "What is your name? "; $name = <>; chomp($name); print "Hello $name!";

Exercise (3) Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 19  Let the user enter the radius of a circle  Tell him the diameter (2r), circumference (2 π r) and area ( π r²) of the circle  Try doing this using one variable for each measure  Try doing this using only one variable

If, else Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 20  Until now, the course the program runs is fixed  The if clause allows us to take different actions in different circumstances # Let‘s try out a conditional clause print "Please enter password: "; $password = <>; if ($password == 42) { print "Correct password! Welcome."; } else { print "Wrong password! Access denied."; }

If, else Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 21  Note: = is the assignment operator, == is the comparison operator  Else is an optional operator triggering if the if condition fails # Let‘s try out a conditional clause print "Please enter password: "; $password = <>; if ($password == 42) { print "Correct password! Welcome."; } else { print "Wrong password! Access denied."; }

Exercise (4) Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 22  Try out the password program.  Why doesn‘t it work correctly? Fix it.  Tell the user if the number he entered is too large or too small  Hint: The comparison operators you’ll need are  Ask the user for a geometrical form (circle or square), and then for a radius or side length. Return the area and perimeter.

While Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 23  What if we want to do checks until something happens?  The while loop repeats commands until its criteria are met  Note: in the example below, $password has no value, so it specifically doesn’t have the value 42 # Now on to a "while" loop while ($password != 42) { print "Access denied.\n"; print "Please enter password: "; $password = <>; chomp($password); } print "Correct password! Welcome.";

Exercise (5) Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 24  Write a small game: take a number, and make the user guess it. Tell him if it‘s too high or too low. If the user gets it right, the program terminates.  If you like, you can take a random number: $random = int (rand(10) );

Perl regular expressions Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 25  Regular expressions very useful for text processing  Perl matching character: =~  Perl non-matching character: !~  The regular expression must be in backslashes: /regex/  The program below accepts any password that contains the characters „42“ anywhere # A "while" loop with regular expressions while ($password !~ /42/) { # While the entered line doesn’t contain “42” print "Access denied.\n"; print "Please enter password: "; $password = <>; chomp($password); } print "Correct password! Welcome.";

Perl regular expressions Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 26  Simple string: some text  One of a number of symbols: [aA]  Matches a or A  Also possible: [tT]he, matching the or The  One of a continuous string of symbols: [a-h][1-8]  Matches any two-character string from a1 to h8  Special characters  ^ matches the beginning of a line  $ matches the end of a line

Perl regular expressions Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 27  More special characters  Wildcard: the dot. Matches any single character  b.d matches bad, bed, bid, bud…  Don‘t forget: it also matches forbid, badly…  + matches one or more of the previous character  re+d matches red and reed (and also reeed and so on!)  * matches zero or more occurrences of the previous character  bel* matches be, bel and bell (and belll…)  ? matches zero or one occurrences of the previous character  soo?n Matches son or soon

Perl regular expressions Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 28  Character classes  \d: digits  Rule \d+ matches Rule 1, Rule 2,..., Rule  \w: “word characters” – letters, digits, _  \w \w – any two “words” separated by a blank  \s: any whitespace (blanks, tabs)  ^\s+\d – any line where the first character is a digit  Capitalize the symbols to get the opposite  \S is anything but whitespace, \D are non-digits…

Exercise (6) Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 29  Write a program which asks the user for his address.  Check if the address is syntactically correct.  Possible rules:  Must contain character  At least one symbol before it  Must contain a dot  At least two symbols and.  At least two symbols after.  No fancy symbols like {§*  Do you accept addresses with more than one dot?

Perl regular expressions Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 30  Switches  Tell Perl how to deal with the regular expression  /regex/i: ignore lower/upper case  /wiebke/i matches Wiebke and wiebke  s/regex/regex2/: substitute regex with regex2  $text =~ s/Mark/Euro/  /regex/g: repeat match until end of the line # What the //g switch does $text = “The meat costs 10 Mark, the fish costs 15 Mark.”; $text2 = $text1; $text =~ s/Mark/Euro/; # “The meat costs 10 Euro, the fish costs 15 Mark.” $text2 =~ s/Mark/Euro/g; # “The meat costs 10 Euro, the fish costs 15 Euro.”

Perl regular expressions Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 31  Grouping  Allows us to use matched string  /(text)/ matches text and stores it in a variable  The first group is stored in $1, the second in $2... # Substitution and grouping $sum = 0; # initializing the variable with zero $text = “The meat costs 10 Mark, the fish costs 15 Mark.” while ($text =~ s/(\d+) Mark/$1 Euro/) { # numbers-spaces-”Mark” $sum = $sum + $1; # adding amount to $sum value } print “Substituted $sum Mark for Euro!”;

Reading files Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 32  What if we want to have input from a file, not from the user?  Open file for reading:  open(INPUT, "<file.ext");  Read a line:  $line = ;  $line = <>; # is just a special case

Writing files Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 33  What if we want to print to a file, not to the screen?  Open file for writing:  open(OUTPUT, “>file.ext");  Write:  print OUTPUT “Some text...”;

Reading files Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 34  A program for testing addresses  Note: If we want to use a special character literally, we need to escape it with a backslash  In strings : "  In regular expressions:. + * ^ $ and the backslash \ itself open(INPUT, "<test.txt"); while ($line = ) { chomp($line); if ($line =~ { # testing for print "\"$line\" is a valid address.\n"; } else { print " address \" $line\" not valid.\n"; }

Exercise (7) Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 35  Make a text file and fill it with a Wikipedia article  Count the number of definite and indefinite articles  Count the number of numbers and digits  Insert a tag before every number

Arrays Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 36  Arrays contain lists of variables  Syntax: = [“Monday“, “Tuesday“, “Friday“];  $days[0] = “Saturday“;  $day = $days[2];  Useful for storing linear sequences of variables  for whole lists, $ for single variables

Arrays Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 37  Useful array commands  “element“);  Adds a new element to the end of the array  Creates the array if necessary  $element =  Moves the last value to $element # Trying out = (“N”, “V”, “Adj”); $tag1 = $tag1 is now is (“N”, “V”) $tag2 = $tag2 is now is (“N”) „V“, is now again (“N”, “V”, “Adj”)

Hashes Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 38  Hashes are associative arrays  They are lists where the elements are not ordered, but identified by a „name“  Syntax:  %probability = (”verb“, 0.32, “adjective“, 0.02, “adverb“, 0);  $probability{“noun”} = 0.52;

Exercise (7) Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 39  What happens if you try to print an array?  What about a hash?  What happens if you convert an array into a hash, or the other way round?

Practical: Tokenizer Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 40  Take a Wikipedia article and put it into a text file  Clean it up if necessary  Tokenize it!  We only want one word per line  Insert a „sentence boundary“ symbol where appropriate  The output should be another file  Think about what choices you make and why!

Practical: Tagger Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 41  Take the POS-annotated corpus from treebank.txt  Clean and tokenize it  Count the tag-token probabilities  Count the transition probabilities  For the first time, I strongly recommend bigrams  Apply the Viterbi algorithm and tag an input file of your choice!

Practical: Tagger++ Introduction to Perl, NLL Riga 2008, by Pawel Sirotkin 42  If it‘s still too easy, or if you want a long-term aim:  Implement smoothing: words can have tags you haven‘t seen them with, or appear in contexts you never saw them before  Try to figure out a way to guess the tags for unknown words better  Write a program to train on 9/10 of the corpus, and test it on the rest.  Compare your results to the actual annotations  Do this 10 times for every 9/10  Still too easy? Implement trigrams and compare the results.