PERL P ractical E xtraction R ecording L anguage.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
Algorithms Series of mathematical or variable manipulations Integer a,b,c a = 12 b = 22 c = a * b (what is c?) 12 * 22 = 264.
Logging In Go to web site:
COMP234 Perl Printing Special Quotes File Handling.
Realtime Equipment Database F.R.E.D. stands for Fastline’s Realtime Equipment Database. F.R.E.D. will allow you to list all your inventory online. F.R.E.D.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
How to Create a Java program CS115 Fall George Koutsogiannakis.
Tutorial Week 5 USING RefWorks EBP, Summer RefWorks RefWorks is an academic product that SFU offers its students. It helps students produce correctly-formatted.
Introduction to a Programming Environment
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Microsoft ® Office Word 2007 Training Mail Merge II: Use the Ribbon and perform a complex mail merge [Your company name] presents:
MBAC 611.  We have been using MS Access to query and modify our databases.  MS Access provides a GUI (Graphical User Interface) that hides much of the.
Microsoft Office Word 2013 Expert Microsoft Office Word 2013 Expert Courseware # 3251 Lesson 4: Working with Forms.
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
Create a Website on the CWU network Find “How to Post a Web Page with a PC”
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Google Training By: Amy Shannon and Dave Auwerda.
SAS Workshop Lecture 1 Lecturer: Annie N. Simpson, MSc.
Introduction to Shell Script Programming
MySQL + PHP.  Introduction Before you actually start building your database scripts, you must have a database to place information into and read it from.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Mail merge I: Use mail merge for mass mailings Perform a complete mail merge Now you’ll walk through the process of performing a mail merge by using the.
MCB 5472 Assignment #6: HMMER and using perl to perform repetitive tasks February 26, 2014.
An Introduction to Designing and Executing Workflows with Taverna Katy Wolstencroft University of Manchester.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
Teacher’s Assessment Assistant Worksheet Builder Starting the Program
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Downloading and Installing Autodesk Revit 2016
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
Lesson 11: Looking at Files and Folders what a file or folder is on the computer how to recognize a file or folder on the desktop how to recognize the.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Downloading and Installing Autodesk Inventor Professional 2015 This is a 4 step process 1.Register with the Autodesk Student Community 2.Downloading the.
XP New Perspectives on Microsoft Office FrontPage 2003 Tutorial 7 1 Microsoft Office FrontPage 2003 Tutorial 7 – Creating and Using Templates in a Web.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
5 1 Data Files CGI/Perl Programming By Diane Zak.
XP Tutorial 8 Adding Interactivity with ActionScript.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
MySQL Getting Started BCIS 3680 Enterprise Programming.
C++ LANGUAGE TUTORIAL LESSON 1 –WRITING YOUR FIRST PROGRAM.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
Unit 6 Repetition Processing Instructor: Brent Presley.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
Introduction to Programming the WWW I CMSC Winter 2003 Lecture 17.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
MySQL Getting Started BCIS 3680 Enterprise Programming.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Perform a complete mail merge Lesson 14 By the end of this lesson you will be able to complete the following: Use the Mail Merge Wizard to perform a basic.
COPA Rollover How to successfully complete the COPA School Year End Rollover from to
Excel Tutorial 8 Developing an Excel Application
Development Environment
Intro to PHP & Variables
Engineering Innovation Center
File Handling Programming Guides.
Topics Introduction to File Input and Output
Conditions and Ifs BIS1523 – Lecture 8.
Encryption and Decryption
Introduction to TouchDevelop
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Introduction to RefWorks
Topics Introduction to File Input and Output
Presentation transcript:

PERL P ractical E xtraction R ecording L anguage

PART I

Follow the link below to go to the Perl Reference guide. This is has also been provided for you in the back of your workbook. As we go along, we will ask you questions. Guess where you can find the answers… Learning the Language!

Download the Software This software is provided FREE for all of your Practical-Extraction-Recording needs. Retrieve Perl from ActiveState This is the spot for the PC specific software! Save this on the desktop, and double-click to begin the installer. Now follow the directions and let us know if you have any question.

#!/usr/bin/perl #Lindsay Husted #password.pl #July 10, 2003 #this program generates a random password # #Generate a random number print "Enter a seed number: "; $seed = ; chomp $seed; srand($seed ^ time); Let’s walk through a script that will generate a password!

#Set up a list of consonants and split(/ */, split (/ */, #Loop through the generation of a password for ($i=1; $i<=4; $i +=1) { print $consonants[int(rand(21))], $vowels[int(rand(5))]; } #EOF

Running Your Script Be sure to save you script in your Perl folder. What working directory are you in? –Once you open your terminal or command prompt, type the UNIX command “dir”. –Change your directory to your Perl folder. To run your script type “perl password.pl” –This calls on the Perl (ActiveState) program to open the specified file.

PART II

Filtering Script Template #!/usr/bin/perl –w #name #date #description of program #Define and open source file, stop the program if there is an error #Define and open output file, stop the program if there is an error #Loop through the entire file, line by line #Read in a line #Process the line #Output the newly edited line to a new file #End of script

#!/usr/bin/perl -w The first line must be entered exactly as it is (including the pound sign) at the top of each Perl program. This line should always be the very first line in the file (not even comments should precede it). Unfortunately, an explanation of what this line does is beyond our present scope. For now, just recognize that it designates this file as a Perl script, and that it is essential.

#name #date #description of program The first few lines are comments. They should include the author of the script's name, the date the script was written (and possibly the dates it is modified as well), and a very brief description of what the program does (at most a short paragraph). No code or commands are necessary for this section of the file, only comments.

#Define and open source file, stop the program if there is an error The next step will involve the first Perl commands. First, we must define a variable that identifies the location of the data file. Let's suppose that our file was named SampleData.txt and was located in the main directory of the C drive. The first command would look a lot like this: $SourceFile = '<C:\SampleData.txt'; The dollar sign tells the computer that this variable is a string (a set of characters), and the semicolon at the end of the line signals the end of this command. If your finished program doesn't run on the first try, often it is a result of forgetting to include a semicolon. The sign )

#opening source file cont’d Now that you have identified the source file, you have to have Perl try an open it. Use the open command to do this: open(INFILE, $SourceFile) or die "The file $SourceFile could not be found.\n"; This command tries to open the file specified by $SourceFile. Now don't be alarmed, the die command merely reports the message that follows it if there is an error and stops the program. So if $SourceFile does not point to a valid file, the program stops. Note: The \n character simply represents a new line.

#Define and open output file, stop the program if there is an error Opening an output file is very similar. First you specify a destination file for the program's output. Let's define our output file $OutputFile= '>C:\FilteredData.txt'; Now we have the location of our output file specified in a string variable called $OutputFile. Note the $ designates a string variable and the semicolon marks the end of the command. Also, in contrast to the read only marker to tell the computer this file will be for output. Don't worry if the file has not been created before the program runs; Perl will create the file for you. (Cont’d ->)

#opening otuput file cont’d Like the input file, the output file has to be opened. Try: open(OUTFILE, $OutputFile) or die "The output file could not be opened.\n"; This command works exactly the same as it did when opening the input file.

#Loop through the entire file, line by line With the input and ouput files open, we are ready to begin filtering the data. A convenient way to do this is to have Perl cycle through the data line by line. Try using a while loop like: while ( ) { #add commands here } This code executes all commands within the curly brackets until the entire source file has been read. Note that while is a special sort of command that does not require a semicolon after it. The rest of our commands will go inside the curly brackets so that they are executed while looping through the file.

#read in a line To read in the next line of the file, add the command: $Line = $_; This line creates a variable named $Line and assigns it the contents of the variable $_. $_ is a special Perl variable, and a full explanation of its contents is beyond our scope. For now, it suffices to know that the command above assigns to the variable $Line the current line of text from the source file. Do not forget the semicolon at the end of the line.

#process the line Now that we have a line of text from the source file, we can edit its contents before sending it to the output file. However, if the current line is empty, we don't want to do anything to it, so we add the command: if($Line eq "\n") { next } This command looks at the text stored in $Line. If $Line is blank (if it only contains a next line character) then Perl executes the next command. 'next' skips the rest of the commands within the while loop and returns to the top of the loop. Note that this command only has an effect if $Line equals something other than \n. Also observe that no semicolon is necessary after this line of code.

#more processing If the line in question is not blank, the real fun begins. Let's change any commas we find into tabs. Don't be intimated by the appearance of the next command, it's actually not that complicated. Try: $Line =~ s/,/\t/g;

#still processing $Line =~ s/,/\t/g; The s identifies a search and subsitute action. The character between the first two slashes (/,/) is what the command searches for, in this case a comma. The character between the second and third slashes (/\t/) is what will be substituted whenever a match is found. In this case, \t is the code for a tab character. So each a tab character is substituted for each comma. Finally, the g stands for global. This ensures that all commas found are replaced. If you leave out the g then only the first comma on the line will be changed. To summarize, this command looks at the current line and replaces each and every comma with a tab character (denoted here by \t).

#output the newly edited line to a new file At this point we are almost done. All we have to do is output the filtered line to our new data file. This can be easily done with the command: print OUTIFLE $Line; As you might guess, this command prints the contents of $Line to the OUTFILE. Now the program is ready to handle a new line of output, so if no more commands are added the program returns to the top of loop.

#End of Script The loop will continue until every line of the INFILE has been read. After the loop has finished running, the program is done. At the end of the program your source file will remain unchanged, but you should also have a newly filtered data file. Congratulations! You used Perl to filter data! You are now ready to try the optional filtering components available throughout the workbook.

Your final output~ #David Hillman #8/01/03 #This program changes a comma-delimited data file to a tab- delimited one #Define source file and open it, stop the program if there is an error $SourceFile = '<C:\SampleData.txt'; open(INFILE, $SourceFile) or die "The database $SourceFile could not be found.\n"; #Define output file and open it, stop the program if there is an error $OutputFile = '>C:\FilteredData.txt'; open(OUTFILE, $OutputFile) or die "The output file could not be opened.\n";

Final cont’d #loop through the entire source file while( ) { #read in the next line of text from the source file $Line = $_; #if the line is blank, do nothing if($Line eq "\n") { next } #replace all commas with tabs $Line =~ s/,/\t/g; #add the newly edited line to the output file print OUTFILE $Line; }

PART III

* Let’s look at the Sea Surface Temperatures Case Study – Page 90 * Find the data! If you look at the website to see an explanation of the data, you will find out what each columnrepresents. Time to use Perl for Data Filtering!

In your workbook… Now follow along with the instructions on Data Filtering – Page 92 Use the Perl Reference guide provided Work slowly and pay careful attention to each line of script Ask us questions if you need help!