Next: , Up: (dir)

Scripting Languages G22.3033-002 Summer 2008


Previous: Top, Up: Top

Scripting Languages G22.3033-002 Summer 2008 Example solutions for quiz2

Thursday 6/26/2008. 30 points.

http://www.cs.nyu.edu/courses/summer08/G22.3033-002/


Next: , Up: solutions-quiz2

Instructions for example solutions

These are example solutions. Please keep in mind that often, there is not just one correct solution to a question. If you come up with different answers, then it may be that both your answers and these answers here are correct. Of course, these answers here may also contain mistakes. If you spot a mistake, please let me know so I can correct it.


Previous: Instructions, Up: solutions-quiz2

Example solutions for Quiz 2


Next: , Up: Solutions-quiz2

solutions-quiz2-1 Regular expressions

  1. "3.141", "1000"
  2. "3.1a", "12.34.56"
  3. ^(0|[1-9][0-9]*)(\.[0-9]*)?$


Next: , Previous: solutions-quiz2-1, Up: Solutions-quiz2

solutions-quiz2-2 Server-side scripting

  1. The user enters input in an HTML form. When the form is submitted, the input turns into arguments of an HTTP GET or POST message. When the server runs the PHP script in response to the HTTP method, it makes the arguments available in the PHP superglobals $_GET or $_POST.
  2. When the server runs the PHP script, it replaces each snippet of PHP code embedded in HTML with the output (echo, print()) of running the PHP snippet.
  3. A self-processing page is a PHP script that serves both as input (via a form) and output. In other words, when the form is submitted, the same PHP script runs again, this time showing the output as well as the form. A good example is the Google search engine: the query input box appears again alongside the search results.


Previous: solutions-quiz2-2, Up: Solutions-quiz2

solutions-quiz2-3 Context

  1. 3 7 8 9 9
  2. A list literal in scalar context gets converted to its last element. For example, the assignment $y = (7, 8, 9) provides a scalar context to the list literal (7, 8, 9), and therefore assigns 9.
  3. An array in scalar context gets converted to its length. For example, the addition @x + 0 provides a scalar context to the array @x, and therefore returns 3.
  4. A scalar in list context gets converted to a list of one element. For example, the loop for $i ($y) { print $i } provides a list context to the scalar $y, and therefore uses it as a list of one element, 9.