1 perl regular expressions. things perl can do easily with regular expression 2 pattern matching...
TRANSCRIPT
![Page 1: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/1.jpg)
1
Perl Regular Expressions
![Page 2: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/2.jpg)
Things Perl Can Do Easily with Regular Expression
2
Pattern matchingFind out if a string contains some specific
pattern.Substitution
Replace parts of a string with other strings.Translation
Replace one character with another character.
Split functionSplit a string into substrings
![Page 3: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/3.jpg)
Pattern Matchingif ($string =~ m/pattern/)
The result is TRUE ($string contains substring pattern)The result is false ($string does not contains substring
pattern)
$message=“I like C, C++, Perl, Java, and Python.”
if ($message =~ m/Perl/){
print “I see Perl\n”;}else{
print “I did not see Perl\n”;};
![Page 4: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/4.jpg)
Pattern Matching to Verify Email@mail = ('[email protected]‘,'hab&cnn.com’);
foreach $address (@mail){if($address =~ m/@/){
print “$address seems to be a good eMail address\n";}else{
print "$address is a bad address\n"; }}
OUTPUT:[email protected] seems to be a good eMail addresshab&cnn.com is a bad address
![Page 5: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/5.jpg)
SubstitutionString substitionIn string $string, replace every "Bill Clinton"
with an “Bush”:$string =~ s/Bill Clinton/Bush/;
![Page 6: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/6.jpg)
TranslationReplace one character with another
characterIn $string, make all vowels upper case:
$string =~ tr/[a,e,i,o,u,y]/[A,E,I,O,U,Y]/;
Change every letter in $string to upper case:$string =~ tr/[a-z]/[A-Z]/;
Change every letter in $string to lower case$string =~ tr/[A-Z]/[a-z]/;
![Page 7: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/7.jpg)
Split functionSplit a string into substrings Split on character: $data = 'Becky Lincoln,25,female,South Bend';@values = split(/,/, $data);Split on string: $data = ‘Care
Bears~~~10:30am~~~Saturday~~~CBS'; @values = split(/~~~/, $data);
![Page 8: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/8.jpg)
Wildcard Character and RepetitionWildcard character
\w Match "word" character (alphanumeric plus "_")
\W Match non-word character\s Match whitespace character (tab or
space key)\d Match digit character\t Match tab\n Match newline
Repitition+ Match 1 or more times* Match 0 or more times{n} Match exactly n times
![Page 9: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/9.jpg)
Task 1Write a Perl program that can
Convert the following string to an array of word
Convert upper case letters into lower caseDisplay each word in one line
“Hello There Nice to SEE Everyone”
![Page 10: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/10.jpg)
Perl Program for Task 1#!/usr/bin/perl
$data="Hello There Nice to SEE Everyone";$data =~ tr/[A-Z]/[a-z]/;@words=split(/\s/, $data); #
@words=split(/ /, $data);foreach $word (@words){
print "$word\n";}
![Page 11: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/11.jpg)
Split function (continued)Split a string into wordsDepending on how you define a wordWe define that words are separated by
non-word characters“Hi there, Java---API!” is considered as
four words
string = “Hi there, Java---API!” ; @words = split(/\W+/, $string);
@words now: (Hi, there, Java, API)
![Page 12: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/12.jpg)
Task 2Write a Perl program that can
Convert the following string to an array of word
Convert upper case letters into lower caseDisplay each word in one line
“Hello There, (It is) Nice to SEE Everyone!”
![Page 13: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/13.jpg)
Perl Program for Task 2#!/usr/bin/perl
$data="Hello There, (It is) Nice to SEE Everyone!";
$data =~ tr/[A-Z]/[a-z]/;@words=split(/\W+/, $data);foreach $word (@words){
print "$word\n";}
![Page 14: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/14.jpg)
HashKey ValueKey must be uniqueExamples:
Student-ID Student GPABank_account_num BalanceName AgeName Birthday
1001 3.5
4004 3.8
3003 3.5
Student_ID Student_GPA
![Page 15: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/15.jpg)
Fill the hash%C151 = (1001 => 3.5, 4004 => 3.8,
3003 => 3.5);
Copy a hash%CSCI_C151_ = %C151;
Lookup value through key$my_gpa = $C151{4004};
Update hash value through key$C151{4004} = 4.0;
Insert a new entry (key->value)$C151{2002} = 3.0;
Hash (red is key, blue is value)
1001 3.5
4004 4.0
3003 3.5
2002 3.0
1001 3.5
4004 3.8
3003 3.5
![Page 16: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/16.jpg)
keys HASH returns an array with only the keys in the hash.
values HASH returns an array with only the values in the hash, in the same order as the keys returned by keys.
%C151 = (1001 => 3.5, 4004 => 3.8, 3003 => 3.5);
@k = keys %C151; #@k now: (1001, 4004, 3003)@v = values %C151; #@v now: (3.5, 3.8, 3.5)
Operations on Hash
![Page 17: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/17.jpg)
Display all pairs of keyvalue
foreach $key (keys %hash){
print “$key ---> $hash{$key}\n"; }
Hash operations
![Page 18: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/18.jpg)
Display the all the pairs of keyvalue in ascending order of keys (alphabetically).
foreach $key (sort keys %hash){
print “$key ---> $hash{$key}\n"; }
Hash operations
![Page 19: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/19.jpg)
Task 3 (What Will Be Displayed)#!/usr/bin/perl%C151 = (1001=>3.5, 4004=>3.8,
3003=>3.5); print "the GPA of ID(3003) is
$C151{3003}\n";
$C151{4004} = 4.0;$C151{2002} = 3.0;
foreach $ID (keys %C151){
print "$ID-->$C151{$ID}\n";}
print "\nSort By ID:\n";foreach $ID (sort (keys %C151)){
print "$ID-->$C151{$ID}\n";}
![Page 20: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/20.jpg)
Task 3#!/usr/bin/perl%C151 = (1001=>3.5, 4004=>3.8,
3003=>3.5); print "the GPA of ID(3003) is
$C151{3003}\n";
$C151{4004} = 4.0;$C151{2002} = 3.0;
foreach $ID (keys %C151){
print "$ID-->$C151{$ID}\n";}
print "\nSort By ID:\n";foreach $ID (sort (keys %C151)){
print "$ID-->$C151{$ID}\n";}
![Page 21: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/21.jpg)
Mission Statement of IUSB“Indiana University South Bend is the comprehensive
undergraduate and graduate regional campus of Indiana University that is committed to serving north central Indiana and southwestern Michigan. Its mission is to create, disseminate, preserve, and apply knowledge. The campus is committed to excellence in teaching, learning, research, and creative activity; to strong liberal arts and sciences programs and professional disciplines; to acclaimed programs in the arts and nursing/health professions; and to diversity, civic engagement, and a global perspective. IU South Bend supports student learning, access and success for a diverse residential and non-residential student body that includes under-represented and international students. The campus fosters student-faculty collaboration in research and learning. Committed to the economic development of its region and state, Indiana University South Bend meets the changing educational and research needs of the community and serves as a vibrant cultural resource.”
(http://www.chancellor.iusb.edu/missionstatement.shtml)
![Page 22: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/22.jpg)
Task 4Analyze the Mission Statement of IUSB and
displayNumber of words (ignore case differences)Number of unique words (ignore case
differences)Count of each unique word in ascending
order of words (alphabetically)
![Page 23: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/23.jpg)
Script for Task 4#!/usr/bin/perl$statement = “….” #replace …. With mission
statementprint "$statement\n";
$statement =~ tr/A-Z/a-z/;@words = split(/\W+/, $statement);
# count the words, %wct is a hashtableforeach $word (@words){
$wct{$word} = $wct{$word} + 1; }
@unique_word = keys %wct;
![Page 24: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/24.jpg)
Script for Task 4 (continued)$num_words = $#words + 1; # number of words$uni_words = $#unique_word + 1; # number of
unique words
print "The number of words is $num_words\n";print "The number of unique words is $uni_words\n";print "The occurrence of each words is listed below\
n";print "****************************************\n";
foreach $word (sort (keys %wct)){
print "$word\t$wct{$word}\n";}
![Page 25: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/25.jpg)
In Previous ScriptKeys are sorted alphabetically in ascending
order, which is same as:
foreach $word (sort {$a cmp $b} (keys %wct)){
print "$word\t$wct{$word}\n";}In descending order of words:
foreach $word (sort {$b cmp $a} (keys %wct)){
print "$word\t$wct{$word}\n";}
![Page 26: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/26.jpg)
How to Sort Hash by Valuesforeach $word (sort {$wct{$a} <=>
$wct{$b}} (keys %wct)){
print "$word\t$wct{$word}\n";}
Note: Switch a and b to change sorting order from ascending to descending
![Page 27: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/27.jpg)
Task 5Analyze the Mission Statement of IUSB and
displayNumber of words (ignore case differences)Number of unique words (ignore case differences)Count of each unique words in ascending order of
counts
![Page 28: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/28.jpg)
Script for Task 5#!/usr/bin/perl$statement = “….” #replace …. With mission
statementprint "$statement\n";
$statement =~ tr/A-Z/a-z/;@words = split(/\W+/, $statement);
# count the words, %wct is a hashtableforeach $word (@words){
$wct{$word} = $wct{$word} + 1; }
@unique_word = keys %wct;
![Page 29: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/29.jpg)
Script for Task 5 (continued)$num_words = $#words + 1; # number of words$uni_words = $#unique_word + 1; # number of
unique words
print "The number of words is $num_words\n";print "The number of unique words is $uni_words\n";print "The occurrence of each words is listed below\n";print "****************************************\n";
foreach $word (sort {$wct{$a} <=>$wxt{$b}} (keys %wct))
{print "$word\t$wct{$word}\n";
}
![Page 30: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/30.jpg)
How about in Descending Order of Count?
Change the sort function:foreach $word (sort {$wct{$b}
<=>$wxt{$a}} (keys %wct)){
print "$word\t$wct{$word}\n";}
![Page 31: 1 Perl Regular Expressions. Things Perl Can Do Easily with Regular Expression 2 Pattern matching Find out if a string contains some specific pattern](https://reader035.vdocument.in/reader035/viewer/2022081603/5697bfe21a28abf838cb49f8/html5/thumbnails/31.jpg)
Reading AssignmentChapter 11