perl (tutorial ii)
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
Perl
Perl(Tutorial II)
NLP Course 06Examples
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{@list=split(/\s+/,$rdln); 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 sentencea second sentenceanother simple third sentence
Output:
firstsecondthird
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 fileopen (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{@list=split(/\s+/,$rdln); $num=scalar(@list);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 fileclose (OUT); #close the output file
EXAMPLE 2
Input: a file containing the following sentences:
first sentencea second sentenceanother 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 callprint ("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
definitionmy $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 argumentsmy $c;$c=$a+$b;return $c;}$res = &add(5,10);
#subroutine call with 2 argumentsprint ("Result:
",$res,"\n");#Output:#---------#Result: 15
EXAMPLE 5#!/usr/bin/perl
sub concat { my ($a,$b,$c) = @_; #the list of argumentsmy $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 screensystem ("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@list=split(/\s+/,$rdln); 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 fileif
($rdln =~ /(Mar|Apr|May)/){ print ("Here is
Spring!!!\n");}else{print ("Sorry, the Spring is not
here...\n");}close (IN); #close the input file