1 / 11

Perl (Tutorial II)

Perl (Tutorial II). NLP Course 06 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

Download Presentation

Perl (Tutorial II)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl(Tutorial II) NLP Course 06 Examples

  2. 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=<IN>; #"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 { @list=split(/\s+/,$rdln); print ($list[$s_counter],"\n"); $rdln=<IN>; #"load-read" the next sentence of input file $s_counter++; } close (IN); #close the input file

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

  4. 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=<IN>; #"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 { @list=split(/\s+/,$rdln); $num=scalar(@list); print OUT ("The ",$s_counter+1," line of input file consists of ",$num," words.\n"); $rdln=<IN>; #"load-read" the next sentence of input file $s_counter++; } close (IN); #close the input file close (OUT); #close the output file

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

  6. 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.

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

  8. 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

  9. 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=<IN>; #"load-read" the 1st sentence of input file @list=split(/\s+/,$rdln); print ("The time is: ",$list[3],"\n"); close (IN); #close the input file

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

  11. 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=<IN>; #"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

More Related