Download presentation
Presentation is loading. Please wait.
Published byBertram Davis Modified over 9 years ago
1
5 1 Data Files CGI/Perl Programming By Diane Zak
2
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
3
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
4
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 5 The SuperBowl Form and Acknowledgment
6
5 6 Planning and Coding the Super Bowl Script
7
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
8
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
9
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
10
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
11
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
12
5 12 Using the die function
13
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
14
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
15
5 15 Writing Records to a File
16
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
17
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
18
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: –@records = ;
19
5 19 Calculating Survey Statistics for the SuperBowl page @records array is used INFILE filehandle is used
20
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
21
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);
22
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
23
5 23 Using the split function
24
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
25
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
26
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);
27
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
28
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
29
5 29 Completed SuperBowl Script
30
5 30 Completed SuperBowl Script
31
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
32
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);
33
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
34
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);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.