Perl (Tutorial II) NLP Course 07 Examples. EXAMPLE 1 #!/usr/bin/perl #Opens and reads the file "sentences" (input file) line by line. #For the i-th sentence.

Slides:



Advertisements
Similar presentations
LPDS MTA SZTAKI Equation Solver A Grid solution Important hint: A follow up example demonstrating the Parameter Study solution for the mass calculations.
Advertisements

Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Adding Dynamic Content to your Web Site
Adv. UNIX:Perl/81 Advanced UNIX v Objectives of these slides: –introduce Perl (version ) –mostly based on Chapter 1, Learning Perl
Computer Programming for Biologists Class 9 Dec 4 th, 2014 Karsten Hokamp
Loops (Part 1) Computer Science Erwin High School Fall 2014.
4.1 Reading and writing files. 4.2 Open a file for reading, and link it to a filehandle: open(IN, "
Introduction to FSM Toolkit Examples: Part I NLP Course 07.
CS311 – Today's class Perl – Practical Extraction Report Language. Assignment 2 discussion Lecture 071CS Operating Systems I.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
9.1 Subroutines and sorting. 9.2 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { STATEMENT1; STATEMENT2;... } Subroutine.
PERL P ractical E xtraction R ecording L anguage.
PERL Part 3 1.Subroutines 2.Pattern matching and regular expressions.
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.
Practical Extraction & Report Language Picture taken from
CSC3530 Software Technology Tutorial Two PERL Basics.
10.1 Sorting and Modules בשבועות הקרובים יתקיים סקר ההוראה (באתר מידע אישי לתלמיד)באתר מידע אישי לתלמיד סקר הוראה.
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
Computer Science A 1: 3/2. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated.
3ex.1 Note: use strict on the first line Because of a bug in the Perl Express debugger you have to put “use strict;” on the first line of your scripts.
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.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
2 $ command Command Line Options ls –a –l hello hi Command Arguments.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Subroutines Just like C, PERL offers the ability to use subroutines for all the same reasons – Code that you will use over and over again – Breaking large.
Fortran- Subprograms Chapters 6, 7 in your Fortran book.
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.
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly.
Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox.
1 CS 101 Lecture 2. 2 Input from the Keyboard Here is a program to accept input from the keyboard and put into into two variables. #include main(){ cout.
Shell Script & Perl Programming Lesson 9. Shell Programming A shell script is a text file that contains Linux (UNIX) commands, which you enter using any.
LPDS MTA SZTAKI Equation Solver Light A Grid solution.
How to start Visual Studio 2008 or 2010 (command-line program)
Perl Challenge Lecturer: Prof. Andrzej (AJ) Bieszczad Phone: “UNIX for Programmers and Users” Third Edition, Prentice-Hall,
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
Introduction to Perl Yupu Liang cbio at MSKCC
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
3D Puzzle Assignment #1 Programming Language, Spring 2003.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Introduction to Perl October 4, 2004 Class Meeting 7 * Notes on Perl by Lenwood Heath, Virginia Tech © 2004.
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related 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.
Just a Little PHP Programming PHP on the Server. Common Programming Language Features Comments Data Types Variable Declarations Expressions Flow of Control.
 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.
Function Basics. Function In this chapter, we will move on to explore a set of additional statements that create functions of our own function (subroutine,
1 More Perl CIS*2450 Advanced Programming Concepts.
1 PERL Functions. 2 Functions Functions also called subroutines are “free flowing”. The returned value from a function can be interpreted in many different.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
2-6 Special Functions Objectives Students will be able to: 1) identify and graph step, constant, and identity functions 2) Identify and graph absolute.
2000 Copyrights, Danielle S. Lahmani Foreach example = ( 3, 5, 7, 9) foreach $one ) { $one*=3; } is now (9,15,21,27)
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
1 Using Perl Modules. 2 What are Perl modules?  Modules are collections of subroutines  Encapsulate code for a related set of processes  End in.pm.
Perl Subroutines User Input Perl on linux Forks and Pipes.
Linux Administration Working with the BASH Shell.
Week Four Agenda Announcements Link of the week Review week three lab assignment This week’s expected outcomes Next lab assignment Break-out problems.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
Shell Script & Perl Programming
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Arrays & Functions Lesson xx
Please use speaker notes for additional information!
PROBLEM SOLVING CSC 111.
Command Line Parameters
CS 101 First Exam Review.
Presentation transcript:

Perl (Tutorial II) NLP Course 07 Examples

EXAMPLE 1 #!/usr/bin/perl #Opens and reads the file "sentences" (input file) line by line. #For the i-th sentence of input file, finds the i-th word and prints it to the screen open (IN,"sentences") || die ("can not open the input file!\n"); #open the input file $rdln= ; #"load-read" the 1st sentence of input file $s_counter=0; #sentence counter while ($rdln ne "") #while the end of input file is not reached print ($list[$s_counter],"\n"); $rdln= ; #"load-read" the next sentence of input file $s_counter++; } close (IN); #close the input file

EXAMPLE 1 Input: a file containing the following sentences: first sentence a second sentence another simple third sentence Output: first second third

EXAMPLE 2 #!/usr/bin/perl #Open and reads the file "sentences" (input file) line by line. #Writes to the output file, "num_words" the following information: #" The i-th line of input file consists of X words." open (IN,"sentences") || die ("can not open the input file!\n"); #open the input file open (OUT,">num_words") || die ("can not open the output file!\n"); #open the output file $rdln= ; #"load-read" the 1st sentence of input file $s_counter=0; #sentence counter while ($rdln ne "") #while the end of input file is not reached print OUT ("The ",$s_counter+1," line of input file consists of ",$num," words.\n"); $rdln= ; #"load-read" the next sentence of input file $s_counter++; } close (IN); #close the input file close (OUT); #close the output file

EXAMPLE 2 Input: a file containing the following sentences: first sentence a second sentence another simple third sentence Output: The 1 line of input file consists of 2 words. The 2 line of input file consists of 3 words. The 3 line of input file consists of 4 words.

EXAMPLE 3 #!/usr/bin/perl #How "my" affects the scope of a variable. $a=5; print ("Initial value of a: ",$a,"\n"); sub add { #subroutine definition #$a=$a+1; my $a=$a+1; print ("Value of a inside the subroutine: ",$a,"\n"); } &add; #subroutine call print ("Final value of a: ",$a,"\n"); #Output: # #Without the use of "my" ($a=$a+1;): #initially a=5, and is increneted by 1 (a=6)! #BUT #Using "my" (my $a=$a+1;): #a remains always constant!! #a equals to 6 ONLY inside the subroutine.

EXAMPLE 4 #!/usr/bin/perl #Definition of a subroutine with return value, #that it takes two arguments. sub add { #subroutine definition my $a = $_[0]; #1st arg. my $b = $_[1]; #2nd arg. #in order to take the 2 arguments, we can also write: #my ($a,$b) #the list of arguments my $c; $c=$a+$b; return $c; } $res = &add(5,10); #subroutine call with 2 arguments print ("Result: ",$res,"\n"); #Output: # #Result: 15

EXAMPLE 5 #!/usr/bin/perl sub concat { my ($a,$b,$c) #the list of arguments my $d; $d=$a.$b.$c; return $d; } $course = &concat("Natural","Language","Processing"); print ("Our course name is: ",$course,"\n"); #Output: # #Our course name is: NaturalLanguageProcessing

EXAMPLE 6 #!/usr/bin/perl #Executes the Unix command "date". #The result is written to a file named "out_date". #This file is opened, the time is extracted and printed to the screen system ("date > out_date"); open (IN,"out_date") || die ("can not open the input file!\n"); #open the input file $rdln= ; #"load-read" the 1st sentence of input print ("The time is: ",$list[3],"\n"); close (IN); #close the input file

EXAMPLE 6 “out_date” file contains: Mon Mar 20 18:34:41 EST 2006 Output: The time is: 18:34:41

EXAMPLE 7 #!/usr/bin/perl #Executes the Unix command "date". #The result is written to a file named "out_date". #This file is opened and the month is extracted. # IF the month is March or April or May, a "spring " message is printed to the screen, ELSE #"Sorry, the Spring is not here..." system ("date > out_date"); open (IN,"out_date") || die ("can not open the input file!\n"); #open the input file $rdln= ; #"load-read" the 1st sentence of input file if ($rdln =~ /(Mar|Apr|May)/) { print ("Here is Spring!!!\n"); } else { print ("Sorry, the Spring is not here...\n"); } close (IN); #close the input file