5 1 Data Files CGI/Perl Programming By Diane Zak.

Slides:



Advertisements
Similar presentations
Programming Logic and Design, Third Edition Comprehensive
Advertisements

Files Organisation sequential files. Readings u Schneider Chapter 8 u Shelly Cashman to 9.14; to 9.11 u Meyer to 2-37; 1995.
Programming Logic and Design Fourth Edition, Introductory
Chapter 5: Loops and Files.
Perl File I/O and Arrays. File I/O Perl allows to open a file to read, write, or append As well as pipe input or output to another program. —We get to.
Guide To UNIX Using Linux Third Edition
Guide To UNIX Using Linux Third Edition
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
 2004 Prentice Hall, Inc. All rights reserved. Chapter 25 – Perl and CGI (Common Gateway Interface) Outline 25.1 Introduction 25.2 Perl 25.3 String Processing.
Introduction to Perl Part III By: Cedric Notredame Adapted from (BT McInnes)
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Introduction to Perl Practical Extraction and Report Language or Pathologically Eclectic Rubbish Lister or …
An Introduction to Unix Shell Scripting
Builtins, namespaces, functions. There are objects that are predefined in Python Python built-ins When you use something without defining it, it means.
2 1 Sending Data Using a Hyperlink CGI/Perl Programming By Diane Zak.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Input, Output, and Processing
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
9 1 DBM Databases CGI/Perl Programming By Diane Zak.
Meet Perl, Part 2 Flow of Control and I/O. Perl Statements Lots of different ways to write similar statements –Can make your code look more like natural.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
Chapter 10: BASH Shell Scripting Fun with fi. In this chapter … Control structures File descriptors Variables.
Introduction to Perl Part III By: Bridget Thomson McInnes 6 Feburary 2004.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Chapter 9: Perl (continue) Advanced Perl Programming Some materials are taken from Sams Teach Yourself Perl 5 in 21 Days, Second Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
Introduction to Unix – CS 21
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormick 3rd floor 607 Office Hours – Tuesday and.
Introduction to Programming the WWW I CMSC Winter 2003.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
Searching and Sorting. Why Use Data Files? There are many cases where the input to the program may come from a data file.Using data files in your programs.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
CPTG286K Programming - Perl Chapter 1: A Stroll Through Perl Instructor: Denny Lin.
Lesson 3-Touring Utilities and System Features. Overview Employing fundamental utilities. Linux terminal sessions. Managing input and output. Using special.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Introduction to Perl. What is Perl Perl is an interpreted language. This means you run it through an interpreter, not a compiler. Similar to shell script.
Loops and Files. 5.1 The Increment and Decrement Operators.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
An Introduction to Programming with C++ Sixth Edition Chapter 13 Strings.
Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.
PERL By C. Shing ITEC Dept Radford University. Objectives Understand the history Understand constants and variables Understand operators Understand control.
Dept. of Animal Breeding and Genetics Programming basics & introduction to PERL Mats Pettersson.
CSC 4630 Perl 3 adapted from R. E. Beck. Problem But we worked on it first: Input: Read from a text file named in a command line argument Output: List.
CPTG286K Programming - Perl Chapter 9: Misc. Control Structures Chapter 10: Filehandles & File tests.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 14: Sequential Access Files
Introduction To Repetition The for loop
Miscellaneous Items Loop control, block labels, unless/until, backwards syntax for “if” statements, split, join, substring, length, logical operators,
Perl Variables: Array Web Programming.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
File IO and Strings CIS 40 – Introduction to Programming in Python
Linux Shell Script Programming
CIS16 Application Development and Programming using Visual Basic.net
Topics Introduction to File Input and Output
The Selection Structure
Topics Introduction to File Input and Output
Presentation transcript:

5 1 Data Files CGI/Perl Programming By Diane Zak

5 2 Objectives In this chapter, you will: Open a file Use the die function to exit a script when a file cannot be opened Write records to a file using the print function Read records from a file Remove the newline character using the chomp function

5 3 Objectives In this chapter, you will: Separate fields using the split function Initialize and update counters Learn how to use the keys and sort functions

5 4 Introduction Most data from a form should be saved to a file for future reference –Keep track of information –Calculate statistics

5 5 The SuperBowl Form and Acknowledgment

5 6 Planning and Coding the Super Bowl Script

5 7 Saving Data to a file Most form data saved to a file is organized into: – Fields Single item of information about a person, place, or thing – Records One or more related fields that contain all of the necessary data about a specific person, place, or thing Data file: –Collection of related records

5 8 Creating and Opening a File open function: –Used to create and/or open a file –Syntax: open (filehandle, mode, filename) –filehandle – name assigned to data file, typically uppercase –mode – how the data file is to be opened (optional) –filename – name of file to open or create ModeDescription <- Open existing file for input or reading, default mode >- Open file for output - Creates new file for writing data or write over, if file exists >>- Open file for append - Write data to end of existing file or create file if necessary

5 9 open function examples: Location of files to create or open if no path is used: –UNIX – same directory as script –Windows – default directory, typically cgi-bin It is good practice to begin filehandle names with IN or OUT, depending on the filehandle is used Creating and Opening a File

5 10 Using the die function If the open function fails, by default, the script continues to run die function: –Displays message and exits script if there is an error while accessing a file –Syntax: die message

5 11 Using the die function open (filehandle, mode, filename) or die message; –Default die message: “Died at scriptname line linenumber” The default die message will be appended to the message unless a newline character (\n) is used –$! is a special variable that holds the reason that the open function failed

5 12 Using the die function

5 13 Writing Records to a File Can use print to write to the filehandle representing an open file –print filehandle data\n; Typically, each record in a datafile is written to a separate line

5 14 Writing Records to a File If a record contains more than 1 field, then a field separator (delimiter) is used: –Examples: comma (,) colon (:) ampersand (&) tab (\t) pipe (|) –Field separator must not be part of the data in the fields Otherwise, when reading the data from the file, there will be problems confusing the separator in the data and the actual field separator

5 15 Writing Records to a File

5 16 Closing a File To make sure that no data is lost, any opened files should be closed before the script ends Syntax: –close (filehandle); A file is automatically closed when the script ends, but it is good practice to close the file when finished using it

5 17 Modifying the SuperBowl script The file survey.txt is opened for appending data, to the OUTFILE filehandle Each record is written to OUTFILE using a comma as a field separator OUTFILE is closed when the print command has completed

5 18 Reading Records into an Array Syntax: –arrayname = ; –Reads records from a file into an array –<> = angle operator –Angle operator instructs to read record from filehandle and store each in a scalar variable in arrayname Example: = ;

5 19 Calculating Survey Statistics for the SuperBowl array is used INFILE filehandle is used

5 20 More on foreach Can declare the loop variable in the foreach statement –The variable can be used within the loop, and will be removed from memory when the loop has completed running

5 21 Removing the Newline Character Chomp function can be used to remove the newline character (\n) from the end of a record Syntax: –chomp (variable); Example: –chomp ($rec);

5 22 Using the split function The split function can be used to divide a string of characters into separate parts based on a pattern Syntax: –split (/pattern/, string); Example: –split (/,/, “John,Jane”); Splits the string “John,Jane” into 2 parts – John and Jane

5 23 Using the split function

5 24 Using Counters Counter: –variable for counting –2 tasks associated with counter: Initializing –Assigning a beginning value to the counter Updating –Incrementing or adding a number to the value in the counter –Incremented by a constant value, typically 1 Accumulator: –Used for adding numbers together, like counter, except an accumulator can be incremented by a varying amount

5 25 Updating the SuperBowl Script split is used to split on the field separator of a comma (,) chomp is used to remove newlines from $rec An array and hash of counters are used

5 26 The keys and sort functions keys function: –Returns an unordered list of keys stored in a hash –Syntax: keys (hash); values function: –A similar function to keys which returns an unordered list of values stored in a hash –Syntax: values (hash);

5 27 The keys and sort functions sort function: –temporarily sorts a list of values –Syntax: sort (list); –Can be used to sort a comma- separated list of values, array, or keys of a hash

5 28 The keys and sort functions keys and sort can be used together, as in Example 3 –sort function will alphabetize the keys in a hash

5 29 Completed SuperBowl Script

5 30 Completed SuperBowl Script

5 31 Summary A field is a single item of information about a person, place, or thing. A record is one or more related fields containing all necessary data about a specific person, place, or thing. Data file = collection of related records. open function can be used to create and/or open a data file. –Syntax: open (filehandle, mode, filename); Modes: = output, >> = append

5 32 Summary die function can be used to display a message and exit a script if an error occurs with the open function –Syntax: die message; –Actual error message during a failed open function is stored in $! variable To write a record to an open file: –Syntax: print filehandle data\n; –Use field separator if there are multiple fields per record close function is used to close an open file. –Syntax: close (filehandle);

5 33 Summary Use the angle operator (<>) to read records from a file into an array. –Syntax: arrayname = ; chomp function can be used to remove newline characters from the end of records. –Syntax: chomp (variable); split function can be used to divide a string into separate parts based on a pattern. –Syntax: split (/pattern/, string); –The values can be returned to an array, hash, or comma-separated list of scalar variables

5 34 Summary Counter and accumulator variables: –Counter is for counting numbers. –An accumulator is used to add together numbers. –Both must be initialized and updated. The keys function can be used to return an unordered list of keys stored in a hash. –Syntax: keys (hash); The values function can be used to return an unordered list of values stored in a hash. –Syntax: values (hash); The sort function can be used to temporarily sort values in a list, in alphabetical order. –Syntax: sort (list);