Download presentation
Presentation is loading. Please wait.
Published byBrittney Greer Modified over 9 years ago
1
Prof. Alfred J Bird, Ph.D., NBCT http://www.cs.umb.edu/~abird abird@cs.umb.edu http://it441-f11-campbell.wikispaces.umb.edu/ Door Code for IT441 Students – 142864* Office – Wheatly 2nd floor 096-03 Office Hours – MW 3:00PM to 4:00PM
2
Perl normally treats lines beginning with a # as a comment. Get in the habit of including comments with your code. Put a comment block at the beginning of your code which includes your name, the name of the module, date written and the purpose of the code.
3
#!/usr/bin/perl -w # #Module Name: helloWorld.pl # #Written by Alfred J Bird, Ph.D., NBCT #Date Written – 21 September 2011 #Purpose: To print out the string “Hello world!” # #Date Modified – 25 September, 2011 #Purpose of modification: To fix spelling errors. #Modified by: Al Bird # print “Hello world! \n”;
4
To find out how many elements are in an array: my $lastIndex = $#array push() and pop() Adds and removes elements from the high end of the array shift() and unshift() Adds and removes elements from the low end of the array
5
Write a program to create an array with the names of the first six months in it and then print them out. Modify the program using push to add the next six months one at a time to the array. Print out the array each time including the index of the last element. Experiment with the pop, shift and unshift functions.
6
Given a text file (i.e. gettysburg.txt in my home directory) write a program to find if the a given word is used in the file. Use the split function as such: my $word (split) This will break up the line into words separated by whitespace characters If the word is found, print out a message indicating such.
7
Given a text file (i.e. gettysburg.txt in my home directory) write a program to find if the a given word is used in the file. Use the split function as such: my $word (split) This will break up the line into words separated by whitespace characters If the word is found, print out a message indicating such. Go to readAndPrint1.pl in my directory for a solution.
8
Often called regex One of the most useful features of Perl Available in most programming languages You used it in BASH What does this do? s/abc/ABC/ Welcome to the world of regular expresions
9
Given a text file (i.e. gettysburg.txt in my home directory) write a program to find if the a given word is used in the file. Use a regular expression such as /and/ If the word is found, print out a message indicating such.
10
Given a text file (i.e. gettysburg.txt in my home directory) write a program to find if the a given word is used in the file. Use a regular expression such as /and/ If the word is found, print out a message indicating such. Go to readAndPrint.pl in my directory for a solution.
11
What is different between the two methods of searching? What if I want to ignore the case of the letters? How would you modify the readAndPrint1.pl program to check without regard to capitalization? It is not very easy is it! With regular expressions it is much simpler. /$word/i
12
What if I wanted to find a pattern only at the start of a line or at the end of a line? In a Regex we can use anchors. To indicate the pattern must be at the start of a line we use the ^ i.e. /^The/ To indicate the pattern must be at the end of the line we use the anchor $ i.e./end$/
13
We can make our Regex more general by using metacharacters. See the table on page 167 in the text Four of the more common metacharacters are: .Matches any character except newline ? Preceding character or group may be present 0 or 1 time +Preceding character or group is present 1 or more times *Preceding character or group may be present 0 or more times
14
Write a program that: Reads the file alice.txt in my home directory Checks to see if any given word is present without regard to capitalization Modify the program to see if there are any numbers in the file
15
Modify the earlier program to accept a file name as input and check for a word entered from the keyboard in the file.
16
Read pages 153 to 177 in the textbook.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.