Section for Week 2, Thursday, 1/21/99 EXAMPLES OF PERL PROGRAMMING ---------------------------- 1) Go over solutions to la1: 1.1: $total=0; $num=0; while (<>) { chomp; $total+=$_; $num++; } if ($num == 0) { print "No numbers read. What's up with that?\n"; } else { print "Read $num numbers, and they averaged ", $total/$num, "\n"; } 1.2: $length=0; while(<>) { chomp; if (length($_) > $length) { $length=length($_); $longword=$_; } } if (!defined($longword)) { print "No words read. What's up with that?\n"; } else { print "Longest length was $length, and such a word was: $longword\n"; } 2) Q's? 3) Go over content-type line for alpha.cgi business (and www.PIC.ucla.edu). 4) Read a number ($n) and a list of strings and then prints out the n'th string in the list: $n=<>; @v=<>; print $v[$n-1]; 5) Same as 4), but with a random string: srand; @v=<>; print $v[int(rand($#v+1))]; NOTE: srand!!! 6) Convert a raw telephone number such as 3105551212 to (310)555-1212: $_=<>; s/(\d{3,3})(\d{3,3})(\d{4,4})/($1)$2-$3/; print; Explain alternative of using a seperate string and the =~ op. THIS IS IMPORTANT FOR LA2!!! 7) Read a list of strings and print in reverse order: print reverse <>; will do the job, but without using 'reverse': @v=<>; for($i=$#v; $i>=0; $i--) { print $v[$i]; }