Title: Introduction to Programming the WWW I
1Introduction to Programming the WWW I
- CMSC 10100-1
- Winter 2004
- Lecture 9
2Todays Topics
- List (array) and hash
- Loop statements (contd)
- Working with files
3List Data
- A list is an ordered collection of scalar values
- Represented as a comma-separated list of values
within parentheses - Example (a,2,3,red)
- Use qw() function to generate a list
- A list value usually stored in an array variable
- An array variable is prefixed with a _at_ symbol
4Why use array variable?
- Using array variables enable programs to
- Include a flexible number of list elements. You
can add items to and delete items from lists on
the fly in your program - Examine each element more concisely. Can use
looping constructs (described later) with array
variables to work with list items in a very
concise manner - Use special list operators and functions. Can use
to determine list length, output your entire
list, and sort your list, other things
5Creating Array Variables
- Suppose wanted to create an array variable to
hold 4 student names - Creates array variable _at_students with values
Johnson, Jones, Jackson, and Jefferson
6Creating Array Variables Of Scalars
- Suppose wanted to create an array variable to
hold 4 student grades (numerical values) - _at_grades ( 66, 75, 85, 80 )
- Creates array variable _at_grades with values 66,
75, 85, 80.
7Referencing Array Items
- Items within an array variable are referenced by
a set of related scalar variables - For example,
- students0, students1, students2, and
students3 - Reference in a variable name/subscript pair
8Referencing Array Items - II
- Subscripts can be whole numbers, another
variable, or even expressions enclosed within the
square brackets. - Consider the following example
- i3
- _at_preferences (ketchup , mustard ,
- pickles , lettuce )
- print preferencesi preferencesi-1
- preferencesi-2 preferences0
- Outputs the list in reverse order
- lettuce pickles mustard ketchup
9Changing Items In An Array Variable
- Change values in an array variable and use them
in expressions like other scalar variables. For
example - _at_scores ( 75, 65, 85, 90)
- scores3 95
- average ( scores0 scores1
- scores2 scores3 ) / 4
- The third line sets average equal to (75 65
85 95 ) / 4, that is, to 80.
10 A Complete Array Example Program
- 1. !/usr/local/bin/perl
- 2. _at_menu ('Meat Loaf','Meat Pie','Minced
Meat', 'Meat Surprise') - 3. print "What do you want to eat for
dinner?\n" - 4. print 1. menu0\n"
- 5. print 2. menu1\n"
- 6. print 3. menu2\n"
- 7. print 4. menu3\n"
- http//people.cs.uchicago.edu/hai/hw4/array1.cgi
11Outputting the Entire Array Variable
- Output all of the elements of an array variable
by using the array variable with print - For example,
- _at_workWeek ('Monday', 'Tuesday', 'Wednesday',
- 'Thursday', 'Friday' )
- print "My work week is _at_workWeek"
- Would output the following
- My work week is Monday Tuesday Wednesday Thursday
Friday
12Getting the Number in an Array Variable
- Use Range operator to find last element of list
- For example
- _at_grades ( 66, 75, 85, 80 )
- last_one gradesgrades
grades3
13Using Range Operator for list length
- Ranger operator is always 1 less than the total
number in the list - (since list start counting at 0 rather than 1).
- _at_workWeek (Monday, Tuesday, Wednesday,
- Thursday, Friday
) - daysLong workWeek 1
- print My work week is daysLong days long
- Would output the following message
- My work week is 5 days long.
14A Better Way to Get List Length
- You can also find the length of an array variable
by assigning the array variable name to a scalar
variable - For example, the following code assigns to size
the number of elements in the array variable
_at_grades - size_at_grades
15Adding and Removing List Items
- shift() and unshift() add/remove elements from
the beginning of a list. - shift() removes an item from the beginning of a
list. For example, - _at_workWeek (Monday, Tuesday,
Wednesday,Thursday, Friday ) - dayOff shift(_at_workWeek)
- print "dayOff dayOff workWeek_at_workWeek"
- Would output the following
- dayOff Monday workWeekTuesday Wednesday
Thursday Friday
16Adding and Removing List Items
- unshift() adds an element to the beginning of
the list For example, - _at_workWeek (Monday, Tuesday, Wednesday,
- Thursday, Friday )
- unshift(_at_workWeek, Sunday)
- print workWeek is now _at_workWeek
- would output the following
- workWeek is now Sunday Monday Tuesday Wednesday
Thursday Friday
17Adding and Removing List Items
- pop() and push() add/remove elements from the
end of a list. - pop() removes an item from the end of a list.
For example, - _at_workWeek (Monday, Tuesday, Wednesday,
Thursday, Friday ) - dayOff pop(_at_workWeek)
- Would output the following
- dayOff Friday workWeekMonday Tuesday Wednesday
Thursday
18Adding and Removing List Items
- push() adds an element to the end of the list
For example, - _at_workWeek (Monday, Tuesday, Wednesday,
- Thursday, Friday )
- push(_at_workWeek, Saturday)
- print workWeek is now _at_workWeek
- would output the following
- workWeek is now Monday Tuesday Wednesday
Thursday Friday Saturday
19splice() function remove or replace array items
- splice(_at_array, offsetindex, length, list)
- _at_arraythe array that will have elements removed
or replaced - offset the index of the first element to be
removed or replaced - length the number of elements to remove
- list a list of elements to replace the removed
elements - If no length is given, everything beginning with
the offset element will be removed - If list is given, the removed elements will be
replaced by the list - http//www.classes.cs.uchicago.edu/classes/archive
/2004/winter/10100-1/02/perl/perl_splice.html
20Extracting Multiple List Values
- If you use multiple subscripts for a list
variable, you will extract a sub-list with the
matching list items. - For example,
- _at_myList ( 'hot dogs, 'ketchup', 'lettuce',
'celery') - _at_essentials _at_myList 2, 3
- print "essentials_at_essentials"
- The output of this code is
- essentialslettuce celery
21Lists of Lists (or multidimensional lists)
- Some data are best represented by a list of lists
22Accessing Individual Items
- Use multiple subsripts to access individual items
- The first subscript indicates the row in which
the item appears, and - the second subscript identifies the column where
it is found. - In the preceding example,
- Inventory00 points to AC1000,
- Inventory10 points to AC1001, and
- Inventory20 points to AC1002
23A Partial Example ...
- _at_Inventory (
- 'AC1000', 'Hammer', 122, 12.50 ,
- 'AC1001', 'Wrench', 344, 5.50 ,
- 'AC1002', 'Hand Saw', 150, 10.00
- )
- numHammers Inventory02
- firstPartNo Inventory00
- Inventory03 15
- print numHammers, firstPartNo,Inventory03
- This would output
- 122, AC1000, 15
24Review for loop and while loop
- for loop
- sum 0
- for (count1 countlt6 count)
- sum count
-
-
while loop sum 0 count1 while (count lt
6) sum count count
25The until Loop
- Operates just like the while loop except that it
loops as long as its test condition is false and
continues until it is true
26Example Program
- Calculation of sum from 1 to 5
- 1. !/usr/local/bin/perl
- 2. print 'The sum from 1 to 5 is '
- 3. sum 0 count1
- 4. do
- sum count
- count
- 7. until (count gt 6)
- 8. print "sum\n"
until loop must end with a
27The foreach Loop
- The foreach loop is typically used to repeat a
set of statements for each item in an array - If _at_items_array (A, B, C)
- Then item would A then B then C.
28foreach Example
- 1. !/usr/local/bin/perl
- 2. _at_secretNums ( 3, 6, 9 )
- 3.uinput 6
- 4. ctr0 found 0
- 5. foreach item ( _at_secretNums )
- 6. ctrctr1
- 7. if ( item uinput )
- print "Number item. Item found was number
ctrltBRgt" - found1
- last
- 10.
- 11.
- 12.if (!found)
- print Could not find the number/n
The last statement will force an exit of the loop
29Would Output The Following ...
30Loop Control
- last statement
- To end a loop before the initial test expression
evaluates to false - while (expression)
- last if (input eq "Run for your life!")
- statement block
-
- next statement
- To skip an iteration of a loop (loop NOT end!)
- for (ctrl_var10 ctrl_var gt 0 --ctrl_var)
- next if (input eq "You don\'t need this.")
- statement block
31Logical Operators(Compound Conditions)
- logical conditional operators can test more than
one test condition at once when used with if
statements, while loops, and until loops - For example,
- while ( x gt max found ne TRUE )
- will test if x greater than max AND found is
not equal to TRUE -
32Some Basic Logical Operators
- the AND operator - True if both tests are
true - while ( ctr lt max flag 0 )
- the OR operator. True if any test is true
- if ( name eq SAM name eq MITCH )
- ! the NOT operator. True if test is false
- if ( !(FLAG 0) )
33Consider the following ...
- 1. !/usr/local/bin/perl
- 2. _at_safe (1, 7)
- 3. print ('My Personal Safe')
- 4. in1 1
- 5. in2 6
- 6. if (( in1 safe0 ) ( in2
safe1)) - 7. print "Congrats you got the combo"
- 8. elsif(( in1 safe0 ) ( in2
safe1)) - 9. print You got half the combo"
- 10.else
- 11. print "Sorry you are wrong! "
- 12. print "You guessed in1 and in2 "
- 13.
34Hash Lists (or associated arrays)
- Items not stored sequentially but stored in pairs
of values - the first item the key, the second the data
- The key is used to look up or provide a
cross-reference to the data value. - Instead of using sequential subscripts to refer
to data in a list, you use keys.
35Advantages of Hash Lists
- Need to cross-reference one piece of data with
another. - Perl supports some convenient functions that use
hash lists for this purpose. (E.g,. You cross
reference a part number with a product
description.) - Concerned about the access time required for
looking up data. - Hash lists provide quicker access to
cross-referenced data. E.g, have a large list of
product numbers and product description, cost,
and size, pictures).
36Using Hash Lists
- General Format to define a hash list
- Alternate syntax
- months ( "Jan" gt 31, "Feb" gt 28, "Mar" gt
31, "Apr" gt 30, "May" gt 31, "Jun" gt 30, "Jul"
gt 31, "Aug" gt 31, "Sep" gt 30, "Oct" gt
31,"Nov" gt 30, "Dec" gt 31 )
37Accessing Hash List Item
- When access an individual item, use the following
syntax - Note You Cannot Fetch Keys by Data Values. This
is NOT valid - mon months 28
38Hash Keys and Values Access Functions
- The keys() function returns a list of all keys in
the hash list - Inventory ( 'Nuts', 33, 'Bolts', 55,
'Screws', 12) - _at_keyitems keys(Inventory)
- print keyitems _at_keyitems
- The values() function returns a list of all
values Example, - Inventory ( 'Nuts', 33, 'Bolts', 55,
'Screws', 12) - _at_keyitems keys(Inventory)
- print keyitems _at_keyitems
- Perl outputs hash keys and values according to
how they are stored internally. So, a possible
output order - keyitems Screws Bolts Nuts
39Using keys() and values()
- Keys() values() are often used to output the
contents of a hash list. For example, - Inventory ( 'Nuts', 33, 'Bolts', 55, 'Screws',
12) - foreach item ( keys (Inventory) )
- print "Itemitem ValueInventoryitem
" -
- The following is one possible output order
- ItemScrews Value12 ItemBolts Value55
ItemNuts Value33
40Changing a Hash Element
- You can change the value of a hash list item by
giving it a new value in an assignment statement.
For example, - Inventory ( 'Nuts', 33, 'Bolts', 55, 'Screws',
12) - InventoryNuts 34
- This line changes the value of the value
associated with Nuts to 34.
41Adding a Hash Element
- You can add items to the hash list by assigning a
new key a value. For example, - Inventory ( 'Nuts', 33, 'Bolts', 55, 'Screws',
12) - InventoryNails 23
- These lines add the key Nails with a value of 23
to the hash list.
42Deleting a Hash Element
- You can delete an item from the hash list by
using the delete() function. For example, - Inventory ( 'Nuts', 33, 'Bolts', 55, 'Screws',
12) - delete InventoryBolts
- These lines delete the Bolts key and its value of
55 from the hash list.
43Verifying an Elements Existence
- Use the exists() function verifies if a
particular key exists in the hash list. - It returns true ( or 1) if the key exists.
(False if not). For example - Inventory ( 'Nuts', 33, 'Bolts', 55, 'Screws',
12) - if ( exists( InventoryNuts ) )
- print ( Nuts are in the list )
- else
- print ( No Nuts in this list )
-
- This code outputs
- Nuts are in the list.
44Environmental Hash Lists
- When your Perl program starts from a Web browser,
a special environmental hash list is made
available to it. - Comprises a hash list that describe the
environment (state) when your program was called.
(called the ENV hash). - Can be used just like any other hash lists. For
example,
45Some environmental variables
- HTTP_USER_AGENT. defines the browser name,
browser version, and computer platform of the
user who is starting your program. - For example, its value might be Mozilla/4.7
en (Win 98, I) for Netscape. - You may find this value useful if you need to
output browser-specific HTML code.
46Some Environmental Variables
- HTTP_ACCEPT_LANGUAGE - defines the language that
the browser is using. - E.g., might be en for English for Netscape or
en-us for English for Internet Explorer. - REMOTE_ADDR - indicates the TCP/IP address of the
computer that is accessing your site. - (ie., the physical network addresses of computers
on the Internet for example, 65.186.8.8.) - May have value if log visitor information
47Some environmental variables
- REMOTE_HOST - set to the domain name of the
computer connecting to your Web site. - It is a logical name that maps to a TCP/IP
addressfor example, www.yahoo.com. - It is empty if the Web server cannot translate
the TCP/IP address into a domain name. - Display all environmental variables
- http//people.cs.uchicago.edu/hai/hw4/env.cgi
48Example Checking Language
- !/usr/local/bin/perl
- print "content-type text/html\n\n"
- print "lthtmlgt\nltheadgt\n"
- print "lttitlegtCheck Environmentlt/titlegt"
- print "lt/headgt\nltbodygt\n"
- langENV'HTTP_ACCEPT_LANGUAGE'
- if ( lang eq 'en' lang eq 'en-us' )
- print "LanguageENV'HTTP_ACCEPT_LANGUAGE'"
- print "ltBRgtBrowser ENV'HTTP_USER_AGENT'"
- else
- print 'Sorry I do not speak your language'
-
- print "\nlt/bodygt\nlt/htmlgt"
- http//people.cs.uchicago.edu/hai/hw4/check_lan.c
gi
49Printing Text Blocks
- In Perl there are easier ways to print out large
blocks of code instead of line-by-line - Solution 1
- print ltlt"CodeBlock" some word(s) to indicate
the beginning of a - block(no " " are needed)
if it's a simple word - ..
- ..
- CodeBlock same word(s) to mark the end of
block. - Notice no quotes or semicolon here!
- Solution 2
- print qq qq followed by some character not used
in - the following text to start the block
- ..
- ..
- the same character to mark the end of the
block. - Notice semicolon here!
- Examples old_version solution1 solution2
50Creating a Hash List of List Items
- Hash tables use a key to cross-reference the key
with a list of values (instead of using simple
key/value pairs). - For example consider the following table
51Creating Hash Tables
- Access items from a hash table much like you
access a hash list - InventoryAC10000 is Hammer,
- InventoryAC10010 is Wrench,
- InventoryAC10021 is 150.
52Changing Hash Table Items
- Changing Hash table items
- Inventory (
- AC1000 gt 'Hammer', 122, 12, 'hammer.gif',
- AC1001 gt 'Wrench', 344, 5, 'wrench.gif',
- AC1002 gt 'Hand Saw', 150, 10, 'saw.gif'
- )
- numHammers InventoryAC10001
- InventoryAC10011 InventoryAC10011
1 - partName InventoryAC10020
- print numHammers, partName, InventoryAC1001
1 - This code would output
- 122, Hand Saw, 345
53Adding/Deleting Hash Table Items
- When you want to add a hash table row, you must
specify a key and list of items. - For example, the following item adds an entry
line to the Inventory hash table - Inventory (
- AC1000 gt 'Hammers', 122, 12,
'hammer.gif', - AC1001 gt 'Wrenches', 344, 5,
'wrench.gif', - AC1002 gt 'Hand Saws', 150, 10,
'saw.gif' - )
- InventoryAC1003 Screw Drivers, 222, 3,
- sdriver.gif
54Adding/Deleting Hash Table Items
- Because the exists() and delete() hash functions
both work on a single key,specify them just as
you did before - For example, the following code checks whether a
key exists before deleting it - if ( exists InventoryAC1003 )
- delete InventoryAC1003
- else
- print Sorry we do not have the key
-
- Check if the record exists before you try to
delete it.
55Working with Files (contd in Lecture 10)
- So far programs cannot store data values
in-between times when they are started. - Working with files enable programs to store data,
which can then be used at some future time. - Will describe ways to work with files in CGI/Perl
programs, including - opening files,
- closing files,
- and reading from and writing to files
56Using the open() Function
- Use to connect a program to a physical file on a
Web server. It has the following format - file handle - Starts with a letter or numbernot
with , _at_, or . (Specify in all capital
letters. (By Perl convention.) - filename - name of file to connect to. If resides
in the same file system directory then just
specify the filename (and not the entire full
file path).
57More On open() function
- open() returns 1 (true) when it successfully
opens and returns 0 (false) when this attempt
fails. - A common way to use open()
- infile mydata.txt
- open (INFILE, infile ) die Cannot open
infile !
Perl special variable, containing the error string
Connect to mydata.txt.
Execute die only when open fails
Output system message
58Specifying Filenames
- So far need to keep file in same directory
- You can specify a full directory path name for
the file to be opened.
59File Handles
- Use the file handle to refer to the file once
opened - Combine with the file handle with the file input
operator (ltgt) to read a file into your program - Perl automatically opens 3 file handles upon
starting a program - STDIN -- standard in, usually the keyboard
- Empty input operator (ltgt) is the same as ltSTDINgt
- STDOUT -- standard out, usually the monitor
screen - print() by default it print to STDOUT
- STDERR -- standard error, usually the monitor
screen
60Using the File Handle to Read Files
- You can read all the content of a file into an
array variable - Each line turns to an array item
- Or you can read the file line by line
- Using the special variable of Perl, _
- The mydata.txt file used in the following 2
examples - Apples are red
- Bananas are yellow
- Carrots are orange
- Dates are brown
61Reading File into Array
- infile"mydata.txt"
- open (INFILE, infile ) die "Cannot
- open infile !"
- _at_infile ltINFILEgt
- print infile0
- print infile2
- close (INFILE)
- Then the output of this program would be
- Apples are red
- Carrots are orange
- http//people.cs.uchicago.edu/hai/hw4/readFile1.c
gi
62Reading One Line At a Time
- Reading a very large file into the list variable
_at_infile consumes a lot of computer memory. - Better is to read one line at a time. For example
the following would print each line of the input
file. - infilemydata.txt
- open (INFILE, infile ) die Cannot open
infile ! - while ( ltINFILEgt )
- inline _
- print inline
-
- close (INFILE)
Automatically set to the next input line.
http//people.cs.uchicago.edu/hai/hw4/readFile2.c
gi
63Two String Functions
- split(/pattern/, string)
- Search the string for occurrence of the pattern.
it encounters one, it puts the string section
before the pattern into an array and continues to
search - Return the array
- Example (run in command line) http//people.cs.uc
hicago.edu/hai/hw4/split.cgi - join(pattern, array)
- Taking an array or list of values and putting the
into a single string separated by the pattern - Return the string
- Reverse of split()
- Example http//people.cs.uchicago.edu/hai/hw4/jo
in.cgi
64Working with split() Function
- Data usually stored as records in a file
- Each line is a record
- Multiple record members usually separated by a
certain delimiter - The record in the following input file part.txt
has the format - part_nopart_namestock_numberprice
- AC1000Hammers12212
- AC1001Wrenches3445
- AC1002Hand Saws15010
- AC1003Screw Drivers2223
65Example Program
-
- infile"infile.txt"
- open (INFILE, infile ) die "Cannot open
- infile!"
- while ( ltINFILEgt )
- inline_
- (ptno, ptname, num, price )
- split ( //, inline )
- print "We have num ptname (ptno). "
- print "The cost is price dollars.", br
-
- close (INFILE)
http//people.cs.uchicago.edu/hai/hw4/readFile3.c
gi
66Using Data Files
- Do not store your data files in a location that
is viewable by people over the Internet - too much potential for tampering by people you do
not know. - Make sure permissions are set correctly (644)
-
67Open Modes
- Three open modes are commonly used to open a
file - read-only (default mode) To explicitly specify
it, put the character lt before the filename. - open(INFILE, ltmyfile.txt) die Cannot open
! - write-only-overwrite allows you to write to a
file. - If the file exists, it overwrites the existing
file with the output of the program. - To specify this mode, use gt at the beginning of
the filename used in the open() function. For
example, - open(INFILE, gtmyfile.txt) die Cannot open
!
68Open Modes(contd)
- write-only-append allows you to write and append
data to the end of a file. - If the file exists, it will write to the end of
the existing file. Otherwise will create it. - To specify this mode, use gtgt before the filename
in the open() function. - open(OFILE, gtgtmyfile.txt) die Cannot open
! - Example way to write to
- print OFILE My program was here
69Locking Before Writing
- If two programs try to write to the same file at
the same time, can corrupt a file. - an unintelligible, usefully mixture of file
contents - provides a flock() function that ensures only one
Perl program at a time can write data. - flock(OFILE, 2)
-
Change 2 to 8 To unlock the file
Exclusive access to file.
File handle.
http//www.icewalkers.com/Perl/5.8.0/pod/func/floc
k.html
70Example of flock()
- outfile"gtgt/home/perlpgm/data/mydata.txt"
- open (OFILE, outfile ) die "Cannot open
outfile !" - flock( OFILE, 2 )
- print OFILE "AC1003Screw Drivers2223\n"
- close (OFILE)
- Appends the following to part.txt
- AC1003Screw Drivers2223
71Reading and Writing Files
- !/usr/bin/perl
- print "Content-type text/html\n\n"
- print qq
- lthtmlgtltheadgtlttitlegtMy Pagelt/titlegtlt/headgt
- ltbodygtltFONT SIZE5gtWELCOME TO MY SITE lt/FONTgt
-
- ctfile"counter.txt"
- open (CTFILE, "lt" . ctfile ) die "Cannot open
infile !" - _at_inline ltCTFILEgt
- countinline0 1
- close (CTFILE)
- open (CTFILE, "gtctfile" ) die "Cannot open
infile !" - flock (CTFILE, 2)
- print CTFILE "count"
- close (CTFILE)
- print qq
- ltbrgtltFONT COLORBLUEgtYou Are Visitor count
lt/FONTgt - lt/bodygtlt/htmlgt
counter.txt permission must be RW
http//people.cs.uchicago.edu/hai/hw4/readWriteFi
le1.cgi
72Read Directory Content
- Not much different from reading a file
- Use opendir(), readdir(), and closedir()
functions - Example
- dir "/home/hai/html/hw4/"
- opendir(DIR, dir)
- _at_content readdir(DIR)
- foreach entry (_at_content)
- print entry . "\n"
-
- closedir(DIR)
http//people.cs.uchicago.edu/hai/hw4/readDir1.cg
i
73File Test
- Example of file testing
- -e file exists
- -z file zero file
- -s file non-zero file, returns size
- -r file readable
- -w file writeable
- -x file executable
- -f file plain file
- -d file directory
- -T file text file
- -B file binary file
- -M file age in days since modified
- -A file age in days since last accessed
74File Test
- _at_content readdir(DIR)
- if (-d entry ) test if it is a directory
- print entry . " is a directory\n"
- elsif (-f entry ) test if it is a file
- print entry . " is a "
- if (-T entry ) test if it is a text file
- print "text file,"
- elsif (-B entry ) test if it is a
binary file - print "binary file,"
- else
- print "file of unknown format,"
-
- print " ". (-s entry) . " bytes\n" get
the file size -
http//people.cs.uchicago.edu/hai/hw4/readDir2.cg
i